problems using magic methods with datamapper - 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: problems using magic methods with datamapper (/showthread.php?tid=40002) |
problems using magic methods with datamapper - El Forum - 03-26-2011 [eluser]johnmerlino[/eluser] Hey all, Because I have magic methods __set and __get defined in a model which inherits from DataMapper, I get the following exception: Call to a member function field_data() on a non-object in /Users/jmerlino/Sites/hbla/system/application/libraries/Datamapper.php on line 622 All I am doing is creating a new user: Code: //users controller Note that there is no property called password. But there are attributes called encrypted_password and password_salt. So I do the following in my model: Code: //user model - inherits from DataMapper Is there way to address the coexistance of magic methods declared in the same model which inherits from DataMapper? Thanks for response. problems using magic methods with datamapper - El Forum - 03-26-2011 [eluser]johnmerlino[/eluser] Obviously this would resolve the issue: Code: $u->setPassword($this->input->post('password')); But I like using magic methods to make the code look more clean. And if it's possible to use magic methods in model while still inheriting from DataMapper, that would be nice. Thanks for response. problems using magic methods with datamapper - El Forum - 03-27-2011 [eluser]WanWizard[/eluser] field_data() is a CI database library method. This error suggests there's something wrong with the database instance ($this->db). The reason this fails is that Datamapper already uses the __get() magic method. And by defining your own, you disable Datamappers, which is why $this->db is not assigned properly. Try changing your method to Code: public function __get($property) problems using magic methods with datamapper - El Forum - 03-27-2011 [eluser]johnmerlino[/eluser] Thanks for response but it still generates the same error. If I change __set as well like this: Code: public function __get($property){ it tells me Uncaught exception 'Exception' with message 'Unable to call the method "__set" on the class User'. I guess it says that because the __set method is not declared in DataMapper? If not, then why does it give me error: Code: Fatal error: Call to a member function field_data() on a non-object in /Users/jmerlino/Sites/hbla/system/application/libraries/Datamapper.php on line 622 with this: Code: public function __get($property){ The __set method above was working prior to inheriting from DataMapper. Thanks for response. problems using magic methods with datamapper - El Forum - 03-27-2011 [eluser]johnmerlino[/eluser] Thanks for your responses. problems using magic methods with datamapper - El Forum - 03-27-2011 [eluser]WanWizard[/eluser] Datamapper doesn't use __set(), so no need to call the parent method. And the error is my bad, I should really find the time to test what I type... You should return the result of parent::__get()... |