greatings! 我的模型没有配置为可包含。以下查询不显示正确的结果(我想检索所有类别中两个日期之间的所有可用汽车)
$car=$this->Genre->find('all', array(
'contain' => array(
'cars'=>array(
'conditions'=>array(
'cars.startdate <=' => $qdu ,
'cars.enddate >=' => $qau
)
)
)
)
);
这是我的类型模型:
class Genre extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array(
'houses' => array(
'className' => 'houses',
'foreignKey' => 'houses_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'cars' => array(
'className' => 'cars',
'foreignKey' => 'genres_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
这是车型:
class Car extends AppModel {
public $belongsTo = array(
'Genres' => array(
'className' => 'Genres',
'foreignKey' => 'genres_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'ways' => array(
'className' => 'ways',
'foreignKey' => 'cars_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
答案 0 :(得分:0)
模型名称通常应该是单数,所以“Car”而不是“cars”。
$car=$this->Genre->find('all', array(
'contain' => array(
'Car'=>array(
'conditions'=>array(
'Car.startdate <=' => $qdu ,
'Car.enddate >=' => $qau
)
)
)
)
);
另外,请确保Genre模型加载Containable行为。
class Genre extends AppModel {
$actsAs = array('Containable');
}
关联还应使用单数,大写的模型名称:
class Genre extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array(
'House' => array(
'className' => 'House',
'foreignKey' => 'houses_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'Car' => array(
'className' => 'Car',
'foreignKey' => 'genres_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
拥有多元化控制器(CarsController,GenresController)和奇异模型(Car,Genre)是一种Cake约定。在定义关联时,你实际上是将模型连接在一起而不是控制器,因此需要单一的大写类名。