【问题标题】:Doctrine : do not load related records原则:不加载相关记录
【发布时间】:2010-12-09 09:44:58
【问题描述】:

为了提高应用程序的性能,我想分离查询而不是使用 leftJoins。然后我必须创建自己的相关 Doctrine_Collection :

$user->Friends->add($current_friend);

但是当我尝试访问相关的(未加载的)集合时,我不希望教义进行查询。

我怎么能做到这一点。 提前致谢。

【问题讨论】:

    标签: php orm doctrine


    【解决方案1】:

    我认为答案在this § about relation handling。建立新的友谊关系并将其保存,而不是向用户对象添加朋友。

    【讨论】:

    • 我明白你的意思,但我不想保存新朋友(它已经存在于数据库中),我想用多个查询自己重新创建用户和朋友的对象图只有一个。
    【解决方案2】:

    然后我找到了这种方式(我应该优化它):

    $my_relation_collFriend = FriendTable::getInstance()->findByIdUser($user->id_user);
    
    foreach($my_relation_collFriend as $friend)
    {
       $collFriend = $user->get('Friends', false); //get the related collection without db query
       if(!$collFriend ) //unfornatly, It can be null 
       {
         $collFriend = new Doctrine_Collection::create('friend'); //create the collection
         $user->set('Friends', $collFriend, false); // define the related collection without db query
       }
       $collFriend->add($friend); //add the record to related collection
     }
    

    通过这个例子,我知道这是没用的,但是有很多连接和数据,它变得很有必要

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多