CakePHP查找方法HABTM,无法正常工作

时间:2014-02-12 08:21:23

标签: php cakephp

只是一个我无法找到答案的快速问题:

CakePHP中的find方法不起作用,这是正常的吗?:

$this->Video->Client->find('all', array(
    'fields' => array('Video.video_id'),
    'conditions' => array('Client.client_id' => 1)
))

在这样的数据库结构中: enter image description here

视频模型:

class Video extends AppModel {
  public $name = 'Video';
  public $primaryKey = 'video_id';

    public $hasAndBelongsToMany = array(
    'Client' => array(
      'className' => 'Client',
      'joinTable' => 'clients_videos',
      'foreignKey' => 'video_id',
      'associationForeignKey' => 'client_id',
      'unique' => 'keepExisting'
    )
  );
}

客户端型号:

class Client extends AppModel {
  public $name = 'Client';
  public $primaryKey = 'client_id';

    public $hasAndBelongsToMany = array(
    'Video' => array(
      'className' => 'Video',
      'joinTable' => 'clients_videos',
      'foreignKey' => 'client_id',
      'associationForeignKey' => 'video_id',
      'unique' => 'keepExisting'
    )
  );
  public $belongsTo = array(
    'User'
  );
}

编辑1

client_id改为id,就像CakePHP的约定一样。

现在我在$this->Video->find('all')上返回的内容:

    array(
        (int) 0 => array(
            'Video' => array(
                'id' => '5',
                'video' => '/files/cz/Chrysanthemum.jpg'
            ),
            'Client' => array(
                (int) 0 => array(
                    'id' => '5',
                    'name' => 'CZ',
                    'image' => null,
                    'vector_image' => null,
                    'slug' => 'cz',
                    'user_id' => '1'
                )
            )
        ),
   )

0 个答案:

没有答案