How to get related object Propel ORM

好久不见. 提交于 2019-12-11 18:20:12

问题


Working on a project with the Zend Framework, I am using Propel ORM with many related database objects. I am new to both, but I have the object models created and working. I am just trying to get my head around object access now.

I have a method for creating a new user, its working good and updates related rows (Not every related, but it's a start). Now I want to present the 'New' user data to the system user (user creater). I get the username back and I now want to get the 'UserLevel' among a few other properties.

After user creation I call my findUserByUserName method, passing the new user's username, I get back all in the Users table, but I also need properties from the 'AgenciesAndUsers' table that are related to that new user.

This is what I am trying, but it dosent' seem to work, plus it don't seem right anyway...I should be able to pass the 'Users->' into the AgenciesAndUsers to get the related properties for that user:

         $User = UsersQuery::create()->findOneByUserName($UserName);
         $UserId = $User->getUserId();

         $UserData = AgenciesAndUsersQuery::create()->findOneByUserId($UserId);

         $data = // merge the two objects

         return $data;

This is where I stopped, because I realized that if this works, I have to pass two objects back to my controller (or whatever called my method). Is there, wait...I know there is...so "How Do I" access the properties from related object? I know this is half..or maybe the whole...reason for using an ORM system anyway :)

I attempted to reverse what I did when creating my user, but I just can't seem to do it. Plus it may be better to just pass the new user's data back after creating the new user in that method, don't I have a persistent object after creating the new User? But I'm gonna need a 'findUserByUserName' method among many more anyway.

Here is how I create my new user and it's AgencyAndUser data too...

$user = new Users();
        $user->setActive($Active);
        $user->setCreatedAt($CreatedAt);
        $user->setEmailAddress($EmailAddress);
        $user->setEnabledInForms($EnabledInForms);
        $user->setFirstName($FirstName);
        $user->setInitialPassword($InitialPassword);
        $user->setLastName($LastName);
        $user->setModifiedAt($ModifiedAt);      
        $user->setPassword($UserPassword);
        $user->setPhoneNumber($PhoneNumber);
        $user->setTitle($Title);
        $user->setUserName($UserName);
        $user->setUserPasswordActive($UserPasswordActive);  

        $user->save();

        $AgnecyAndUser = new AgenciesAndUsers();
        $AgnecyAndUser->setAgencyId($AgencyId);
        $AgnecyAndUser->setUserLevel($UserLevel);

        // associate the $Users object with the current $AgnecyAndUser
        $AgnecyAndUser->setUsers($user);
        $AgnecyAndUser->save();

Edit: I just realized (after looking at my posted question), that I could probably 'chain' all those 'User' properties into on line instead of repeating the '$User' over and over.

Turned out clean, straight from the Propel Manual...which BTW I have read over many sections too, just having 'startup' issues as a beginner/novice.


回答1:


If you have your relationship set up correctly there should be accessors for the object:

$user->getAgenciesAndUsers();

There shouldnt be a reason to query the AgenciesAndUsers separately. In fact if you use a JOIN in your query you should only need to hit the DB once.

Also if you havent gone to far with this i would rename everything to it is singular ie. AgencyAndUser (or more rightly UserAgency), and User.



来源:https://stackoverflow.com/questions/5446320/how-to-get-related-object-propel-orm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!