【发布时间】:2012-09-01 18:47:08
【问题描述】:
我正在使用 Yii 框架,在构建朋友/连接风格系统时遇到了问题。我有一个标准的用户表并存储朋友我有一个具有以下布局的朋友表(已设置适当的 FK 等):
id | userId | friendId | and some other fields...
由于“朋友”是相互的 - 因此,如果用户 A 是用户 B 的朋友,则用户 B 是用户 A 的朋友 - 因此每个“朋友”只有一个条目。
我想要做的是使用 Yii 的关系查询最终返回属于朋友的项目。目前我在用户模型中有以下关系...
'friends' => array(self::HAS_MANY, 'UserFriend', 'userId'),
但是,如果用户 ID 存在于 userId 字段中,则仅返回“朋友”。如果用户 ID 在friendId 字段中,则无法识别“友谊”。
有没有办法使用“OR”进行查询 - 例如:如果用户 ID == userId OR 用户 ID ==friendId 则返回该条目?
另外,有没有办法让 Yii 返回另一个 ID,如果用户 ID == userId 则返回friendId,否则如果用户 ID == friendId 则返回 userId?
我最终会尝试做类似的事情:
$userModel = User::model()->findByPk(Yii::app()->user->id); // user's model
$userFriends = $userModel->with(items)->friends; // return friends with their items
希望我没有混淆太多!抱歉解释不好
谢谢:)
【问题讨论】:
标签: php activerecord yii relational-database