如何获取相关对象 Propel ORM

发布于 2024-10-27 14:57:32 字数 2134 浏览 4 评论 0原文

在使用 Zend Framework 进行项目时,我将 Propel ORM 与许多相关的数据库对象一起使用。我对两者都是新手,但我已经创建并运行了对象模型。我现在只是想了解对象访问。

我有一种创建新用户的方法,它运行良好并更新相关行(不是所有相关的,但它是一个开始)。现在我想向系统用户(用户创建者)呈现“新”用户数据。我取回了用户名,现在我想获取“UserLevel”以及其他一些属性。

创建用户后,我调用 findUserByUserName 方法,传递新用户的用户名,我取回 Users 表中的所有内容,但我还需要“AgenciesAndUsers”表中与该新用户相关的属性。

这就是我正在尝试的,但它似乎不起作用,而且无论如何它似乎都不正确......我应该能够通过“用户->”进入 AgenciesAndUsers 以获取该用户的相关属性:

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

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

         $data = // merge the two objects

         return $data;

这是我停下来的地方,因为我意识到如果这有效,我必须将两个对象传递回我的控制器(或任何称为我的方法)。有吗,等等...我知道有...那么“我如何”访问相关对象的属性?我知道这是使用 ORM 系统的一半......或者也许是全部......原因:)

我试图扭转我在创建用户时所做的事情,但我似乎无法做到这一点。另外,在该方法中创建新用户后将新用户的数据传回可能会更好,创建新用户后我不是有一个持久对象吗?但无论如何,我需要一个“findUserByUserName”方法。

这是我创建新用户的方式,它也是 AgencyAndUser 数据...

$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();

编辑:我刚刚意识到(在查看我发布的问题之后),我可能可以将所有这些“用户”属性“链接”到线上,而不是重复一遍又一遍地“$User”。

结果很干净,直接来自 Propel 手册...顺便说一句,我也阅读了很多部分,只是作为初学者/新手遇到了“启动”问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没企图 2024-11-03 14:57:32

如果您正确设置了关系,则应该有该对象的访问器:

$user->getAgenciesAndUsers();

不应该有理由单独查询 AgenciesAndUsers。事实上,如果您在查询中使用 JOIN,您应该只需要访问数据库一次。

另外,如果你还没有对此走得太远,我会将所有内容重命名为它是单数,即。 AgencyAndUser(或更准确地说是UserAgency)和User

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文