![]() |
changing object in loop in Datamapper ORM to save - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: changing object in loop in Datamapper ORM to save (/showthread.php?tid=41578) |
changing object in loop in Datamapper ORM to save - El Forum - 05-11-2011 [eluser]mak.gnu[/eluser] I'm trying to save a long form in Codeigniter's Datamapper. I'm able to save the form if I pass the value like this Code: $t->brandName = $this->input->post('brandName'); Now if I call the save method it works fine. $t->save(); Since the form is big I tried to add object values in foreach Code: $a = get_object_vars($t); however if I call the $t->save() it doesn't work. Any input into this. changing object in loop in Datamapper ORM to save - El Forum - 05-12-2011 [eluser]toopay[/eluser] Maybe there is something wrong in foreach statement. How your forms looks like? changing object in loop in Datamapper ORM to save - El Forum - 05-13-2011 [eluser]mak.gnu[/eluser] My form is simple with many fields in it. the name of each field is same as column name. it works when I do foreach($_POST as $k=>$val){ $t->$k = $this->input->post($k); } Can't I just iterate through the object ? changing object in loop in Datamapper ORM to save - El Forum - 05-13-2011 [eluser]WanWizard[/eluser] For starters, not every Datamapper object property is a table column, so using get_object_vars() doesn't work. If you need a list of table columns, use the $object->fields array. And second, a statement like "save() doesn't work" isn't really informative. What doesn't work? Any error messages? What have you tried to debug it? changing object in loop in Datamapper ORM to save - El Forum - 05-13-2011 [eluser]mak.gnu[/eluser] thanks for reply Code: function save() { Code: SELECT * FROM `trucks` LIMIT 1 If I use $_POST instead of $t->fields it saves and also gives insert query changing object in loop in Datamapper ORM to save - El Forum - 05-13-2011 [eluser]WanWizard[/eluser] Try to use code blocks, that would make your posts much cleaner. Also, you're answer doesn't answer any of my questions. Check for errors first Code: if ( ! $t->save() ) If you say that using $_POST['field'] works, but $this->input->post("$val") doesn't, wouldn't it be obvious that that's where the problem is, and not in the Datamapper code? For starters, try $this->input->post($var), because I haven't got a clue why you would use double quotes there. Also, you should check if that actually returns a value. If it doesn't exist, the post() method returns false, and setting an 'id' to false is not something Datamapper likes. changing object in loop in Datamapper ORM to save - El Forum - 05-13-2011 [eluser]mak.gnu[/eluser] When I run the following code it gives error stating Code: The id field must contain an integer. Code: $t = new Truck(); And whenI run following code it works like a charm. and saves the data. Code: $t = new Truck(); What is wrong in using $t->fields changing object in loop in Datamapper ORM to save - El Forum - 05-13-2011 [eluser]WanWizard[/eluser] There is nothing wrong with the Datamapper code, nor with the fields array. The error is in your code and logic. This error suggests you have a form field called 'id'. You can not set a Datamapper id manually. Every Datamapper model validates the 'id' field before attempting a save operation. Learn to do some debugging before you ask for help. If you had done that here, you would have spotted the mistake. changing object in loop in Datamapper ORM to save - El Forum - 05-13-2011 [eluser]mak.gnu[/eluser] Thanks for suggestion will do that in future |