bibig
(Liushuxun)
August 23, 2009, 3:06am
1
想知道用AR的relation功能如何描述像twitter那样的i follow 和 following me的表自关联关系
有一张UserFriend表(userId,friendId)来记录关联数据
我在User model的relations里是这样写的:
‘iFollow’=>array(self::MANY_MANY,‘User’,‘UserFriend(UserId,FriendId)’),
‘followMe’=>array(self::MANY_MANY,‘User’,‘UserFriend(FriendId,UserId)’),
在controller里面测试了一下,代码如下:
$iFollow=User::model()->with(‘iFollow’)->findAll();
var_dump($iFollow);
提示了Fatal错误:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 77 bytes) in /var/www/yii-1.0.8.r1317/framework/db/CDbCommand.php on line 308
不知道问题出在哪里,能否请qiang哥指点一下,非常感谢
bibig
(Liushuxun)
August 23, 2009, 1:40pm
3
好友关系,就是user与user自身的many_many关联
不知道哪里写错了
bibig
(Liushuxun)
August 24, 2009, 7:09am
4
问题已解决,打扰各位了。
错误是因为测试输出的时候,没有设定好查询条件,改成下面的即可
$criteria=new CDbCriteria;
$criteria->condition=‘User.id=’.Yii::app()->user->id;
$iFollow=User::model()->with(‘iFollow’)->find($criteria);
$followMe=User::model()->with(‘followMe’)->find($criteria);
echo(‘i follow:<br />’);
foreach($iFollow->iFollow as $one)
{
echo($one->username.'<br />');
}
echo(‘follow me:<br />’);
foreach($followMe->followMe as $one)
{
echo($one->username.'<br />');
}