Project.php型号文件应如下
<?php
App::uses('AppModel','Model');
/*
*
* Project Model
*
*/
Class Project extends AppModel{
/*
*
* actsAs Behaviour
*
*/
public $actsAs = array('Containable');
/*
*
* belongsTO association
*
*/
public $belongsTo = array(
'Item' => array(
'className' => 'Item',
'foreignKey' => 'project_id',
)
);
}
?>
Item.php型号文件应如下
<?php
App::uses('AppModel','Model');
/*
*
* Item Model
*
*/
Class Item extends AppModel{
/*
*
* actsAs Behaviour
*
*/
public $actsAs = array('Containable');
/*
*
* belongsTO association
*
*/
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'item_id',
)
);
/*
*
* hasMany association
*
*/
public $hasMany = array(
'Project' => array(
'className' => 'Project',
'foreignKey' => 'project_id',
)
);
}
?>
User.php型号文件应该如下
<?php
App::uses('AppModel','Model');
/*
*
* User Model
*
*/
Class User extends AppModel{
/*
*
* actsAs Behaviour
*
*/
public $actsAs = array('Containable');
/*
*
* hasMany association
*
*/
public $hasMany = array(
'Item' => array(
'className' => 'Item',
'foreignKey' => 'item_id',
)
);
}
?>
您可以尝试在控制器中使用下面的代码。
<?php
$contain = array('Item' => array('User'));
$this->set('projects', $this->Project->find('all', array('contain' => $contain)));
?>
除非你在这里有一个拼写错误,你似乎没有'items'表中的'user_id'字段。 – Mike 2010-05-28 13:47:46
您可以发布您的可容纳数组(代码)还是您已应用于模型字段的可容纳提取中的条件? – 2010-05-28 16:12:50