您好我们正在尝试将Cakephp 1.3代码迁移到Cakephp 2.x,因为我们收到了与模型关联相关的警告,我无法解决这个问题。
当我使用旧查询获取具有关联模型(相关Db表)的数据时
$this->loadModel('User');
$this->loadModel('FeaturedMentor');
$featureData = $this->FeaturedMentor->find('all', array('order' => 'FeaturedMentor.id desc', 'limit' => 5));
$this->User->Behaviors->attach('Containable');
foreach ($featureData as $key => $featuredMentor){
$featuredMentorArray[$featuredMentor['FeaturedMentor']['user_id']] = $this->User->find('first',
array(
'contain'=>array(
'UserReference'=>array(
'fields'=>array('zipcode','first_name','last_name','area_of_expertise')
),
'UserImage',
'Testimonial'
),
'conditions'=>array('role_id'=>Configure::read('App.Role.Mentor'),'User.id'=>$featuredMentor['FeaturedMentor']['user_id'],'status'=>Configure::read('App.Status.active'))
)
);
}
比我们收到一些警告
Warning (512): Model "User" is not associated with model "UserReference" [APP/Model/Behavior/ContainableBehavior.php, line 342]
Warning (512): Model "User" is not associated with model "UserImage" [APP/Model/Behavior/ContainableBehavior.php, line 342]
Warning (512): Model "User" is not associated with model "Testimonial" [APP/Model/Behavior/ContainableBehavior.php, line 342]
结果数据的数组如此
Array
(
[965] => Array
(
)
[851] => Array
(
)
[769] => Array
(
)
[734] => Array
(
)
[657] => Array
(
)
)
我们的用户模型类代码:
class User extends AppModel {
var $name = 'User';
var $actsAs = array(
'Multivalidatable'
);
var $hasOne = array(
'UserReference' => array(
'className' => 'UserReference',
'foriegnKey' => 'user_id',
'dependent' => true
)
);
var $hasMany = array(
'UserImage' => array(
'className' => 'UserImage',
'foriegnKey' => 'user_id',
'dependent' => true
),
'Testimonial' => array(
'className' => 'Testimonial',
'foriegnKey' => 'user_id',
'dependent' => true
)
);....
我们的UserReference模型:
class UserReference extends AppModel {
var $name = 'UserReference';
var $actsAs = array('Multivalidatable');
var $belongsTo = array('User');
var $validationSets = array(
'admin' => array(
'first_name' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'First name is required.'
)
),
'last_name' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Last name is required.'
)
),....
我们的推荐模型类:
class Testimonial extends AppModel {
var $name = 'Testimonial';
var $actsAs = array('Multivalidatable');
var $belongsTo = array('User');
}
我的问题可能很愚蠢,但我试图解决它,但没有得到解决方案,所以需要帮助。感谢...
答案 0 :(得分:0)
我修复了我的问题,实际上我的全局变量(配置::读取(' App.Role.Mentor'))在查询中没有得到任何查询没有给出任何结果的值。
...谢谢