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