Kohana/PHP records getting replaced if it is run inside the loop -
i have following snippet in code, problem when try add multiple items. getting added repalcing added row , eg:- in following code snippet 1 record there id 150, , record 13. can please tell me problem is.
public function storeskills($skills,$id) { $skills = "16,13"; $id = "150"; if(!empty($skills)){ $userskillsobj = orm::factory('userskill'); $userskills = explode(',',$skills); foreach($userskills $skill) { // $userskillsobj = orm::factory('userskill'); if statement here, //records getting added fine. $userskillsobj->user_id = $id; $userskillsobj->skills_id = $skill; $ids = $userskillsobj->save(); } } }
move line creates object within foreach loop. having outside means you're referencing same object, therefore final (second) loop saves on first one, making 13.
example:
foreach($userskills $skill) { $userskillsobj = orm::factory('userskill'); $userskillsobj->user_id = $id; $userskillsobj->skills_id = $skill; $ids = $userskillsobj->save(); }
Comments
Post a Comment