1

我的查找查询是这样的;

//here $u_id is id of the users table retrieved using session variable

$this->User->Request->find("all",array("conditions" =>array("request.status" => "friends" , 
"OR" => array("request.friend_id" => "$u_id","request.user_id" => "$u_id"))));

等效的 SQL 查询是:-

SELECT `Request`.`id`, `Request`.`user_id`, `Request`.`friend_id`, `Request`.`status`, `User`.`id`, `User`.`name`, `User`.`email`, `User`.`password`, 
FROM 
`myblog`.`requests` AS `Request` JOIN `myblog`.`users` AS `User` 
ON 
(`Request`.`friend_id` = `User`.`id`) 
WHERE  
((`request`.`friend_id` = 3) OR (`request`.`user_id` = 3)) AND `request`.`status` = 'friends'

而我希望在上述 SQL 查询中的“ON”之后使用以下行来获得所需的结果:

`Request`.`user_id` = `User`.`id` OR `Request`.`friend_id` = `User`.`id`

我应该对方法进行哪些更改find(),或者我应该更改我的模型?

我的桌子是:

users(id, name, password, email)
requests(id, user_id(users表的id),
friend_id(users表的id), status)

4

1 回答 1

0

您可以在模型中指定条件。

public $belongsTo = array('User' => array('className' => 'Request',
                                          'foreignKey' => 'user_id',
                                          'conditions' => array('OR' => array('Request.user_id' => 'User.id', 'Request.friend_id' => 'User.id')
                                         ))
                         );
于 2012-07-06T06:54:33.183 回答