![]() |
DMZ confusion - 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: DMZ confusion (/showthread.php?tid=32429) |
DMZ confusion - El Forum - 07-22-2010 [eluser]Lyon[/eluser] Hi all, I have just started using DMZ for ORM and am having a little bit of trouble understanding the relationships. I have a table users : Code: CREATE TABLE IF NOT EXISTS `ci_dmz`.`users` and a table posts : Code: CREATE TABLE IF NOT EXISTS `ci_dmz`.`posts` I have the relationship on the author id matching the user id using : In the User model : Code: var $has_many = array( Code: var $has_one = array( I retrieve the posts as : Code: $u = new User(); Now this works, but I am unsure as to why it works. I am hesitant to continue without understanding fully what part(s) of the $has_many and $has_one arrays is relating to the class (model) and what part(s) are relating to the database fields. If someone who understands this would be able to take a couple of minutes to break this down for me I would greatly appreciate it. Thanks, Lyon. DMZ confusion - El Forum - 07-23-2010 [eluser]WanWizard[/eluser] You have defined a one-to-many between user and post, on the user field author. What happens is that your 'get_by_username' gets the user record. When you then requests the post, DMZ knows from your definition that the posts table has a field called 'author' which is the foreign key to users. It basically does a "SELECT * FROM posts WHERE author = <userid>". You can do Code: $u->check_last_query(); DMZ confusion - El Forum - 07-23-2010 [eluser]Lyon[/eluser] [quote author="WanWizard" date="1279884473"]You have defined a one-to-many between user and post, on the user field author. What happens is that your 'get_by_username' gets the user record. When you then requests the post, DMZ knows from your definition that the posts table has a field called 'author' which is the foreign key to users. It basically does a "SELECT * FROM posts WHERE author = <userid>". You can do Code: $u->check_last_query(); Thanks :-) You've helped me trace down the problem of why it didn't look right, it wasn't lol. It would work by getting the posts from a user, but no the user from a post. The solution was : In user : Code: var $has_many = array( Code: var $has_one = array( I understand it now. The first array contains the property you use to access the data from the relationship: e.g. $post->author or $user->posts The class is literally the class that the other_field belongs to. e.g. the author field can be found in class post in the has_one or has_many array or the posts reference can be found in the user class. The rules for mapping from the class to the database take care of the rest. Thanks a lot for your help WanWizard, that check last query will save me a ton of hassle, I honestly would never have thought to look in the 'Utility Methods' section of the documentation lol. Thanks again! Edit : If anyone comes across this thread just to let you know if you use : Code: echo $u->check_last_query(); More information can be found here. |