1

赞!我的模型没有被配置为可包含的。以下查询不显示正确的结果(我想检索所有类别中两个日期之间的所有可用汽车)

$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' => ''
    )
);

}

4

1 回答 1

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)是一个蛋糕惯例。在定义关联时,您实质上是将模型链接在一起而不是控制器,因此需要单数大写的类名。

于 2012-05-03T13:58:59.087 回答