【发布时间】:2010-12-09 09:44:58
【问题描述】:
为了提高应用程序的性能,我想分离查询而不是使用 leftJoins。然后我必须创建自己的相关 Doctrine_Collection :
$user->Friends->add($current_friend);
但是当我尝试访问相关的(未加载的)集合时,我不希望教义进行查询。
我怎么能做到这一点。 提前致谢。
【问题讨论】:
为了提高应用程序的性能,我想分离查询而不是使用 leftJoins。然后我必须创建自己的相关 Doctrine_Collection :
$user->Friends->add($current_friend);
但是当我尝试访问相关的(未加载的)集合时,我不希望教义进行查询。
我怎么能做到这一点。 提前致谢。
【问题讨论】:
我认为答案在this § about relation handling。建立新的友谊关系并将其保存,而不是向用户对象添加朋友。
【讨论】:
然后我找到了这种方式(我应该优化它):
$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
}
通过这个例子,我知道这是没用的,但是有很多连接和数据,它变得很有必要
【讨论】: