[英]CakePHP retrieve data, associate models, containable behavior
I can't seem to figure out how the containable behavior works, or how to retrieve the data from (and based) on associated models. 我似乎无法弄清楚可容纳行为是如何工作的,或者如何从(和基于)关联模型中检索数据。
I need to put the following MySQL query in the cakephp find('all') function, I know i can use the query builder for the sql syntax but I want to use find function and want to figure out how it works. 我需要将以下MySQL查询放入cakephp find('all')函数中,我知道我可以使用查询生成器来获取sql语法,但是我想使用find函数并想弄清楚它是如何工作的。
SELECT events.id, events.name FROM events, user_roles
WHERE user_roles.event_id = events.id
AND user_roles.user_id = 1
The idea is simple, in the user_roles the user_id, role_id and event_id are stored. 这个想法很简单,在user_roles中存储了user_id,role_id和event_id。 Now I want to retrieve all events for a certain user (here user_id = 1).
现在,我想检索某个用户的所有事件(此处为user_id = 1)。 I tried some variations on the following:
我在以下方面尝试了一些变体:
$data = $this->Event->find('all',array(
'contain' => array(
'UserRole' => array(
'fields' => array('id'),
'conditions' => array(
'UserRole.user_id' => $this->Auth->user('id')
)
)
)
));
But this keeps returning all events. 但这一直返回所有事件。 What I want is an array like (or any other result with the same associated data):
我想要的是一个数组(或具有相同关联数据的任何其他结果):
array(
[0] => array(
[Event] => array(
'id' => 1,
'name' => 'Event Name'
),
[UserRole] => array(
'role_id' => 2,
'event_id'=> 1
)
[1] => array(
.... etc ...
)
The following models and associations are defined 定义了以下模型和关联
class Event extends AppModel {
public $hasMany = array(
'UserRole' => array(
'className' => 'UserRole',
'foreignKey' => 'event_id',
'dependent' => false,
)
)
)
class UserRole extends AppModel {
public $belongsTo = array(
'Event' => array(
'className' => 'Event',
'foreignKey' => 'event_id'
),
)
)
So how does it work ;). 那么它是怎样工作的 ;)。
Edit: 编辑:
I forgot to mention that I've set in AppModel.php: 我忘了提到我在AppModel.php中设置的内容:
public $recursive = -1;
Containable finds the associated records for the original query. Containable查找原始查询的关联记录。 What you are doing is a
find('all')
on your Event
model with absolutely no conditions, so it will return all Events. 你在做什么是
find('all')
在你的Event
模型,绝对没有条件,所以它会返回所有活动。
Then Containable will retrieve all the UserRole
records that are for the given user_id
and are also associated to the Event
by the event_id
然后,Containable将检索给定
user_id
所有UserRole
记录,并通过event_id
将其与Event
相关联
If you want to only retrieve events for the given user, you could do it one of two ways 如果您只想检索给定用户的事件,则可以采用以下两种方法之一
Via the Event->UserRole association and chained models: 通过Event-> UserRole关联和链接的模型:
$data = $this->Event->UserRole->find('first', array(
'conditions' => array('UserRole.user_id' => $this->Auth->user('id'),
'contain' => array('Event')
));
$events = $data['Event'];
or 要么
Via your current code and then using the Hash::extract
method to get the Events that match. 通过当前代码,然后使用
Hash::extract
方法获取匹配的事件。 ( Hash
is a CakePHP core class) (
Hash
是一个CakePHP核心类)
$events = Hash::extract('/UserRole[user_id='.$this->Auth->user('id').']/..', $data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.