1

偉い!私のモデルは包含可能として構成されていません。この次のクエリは正しい結果を表示しません (すべてのカテゴリの 2 つの日付の間に利用可能なすべての車を取得したい)

$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

通常、モデル名は単数形にする必要があるため、「cars」ではなく「Car」を使用します。

$car=$this->Genre->find('all', array(
                            'contain' => array(
                                'Car'=>array(
                                    'conditions'=>array(
                                        'Car.startdate <=' => $qdu ,
                                        'Car.enddate >=' => $qau
                                    )
                                )
                            )
                        )
                    );

また、ジャンル モデルが 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 の規則です。アソシエーションを定義するときは、本質的にモデルをリンクしていて、コントローラーをリンクしていないため、単一の大文字のクラス名が必要です。

于 2012-05-03T13:58:59.087 に答える