开发者

How to get related object Propel ORM

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 'star开发者_如何学Pythontup' issues as a beginner/novice.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜