cakephp - 一个find()查询来统治它们

时间:2012-07-06 05:29:59

标签: php cakephp

我定义了以下模型关系:

class Publication extends AppModel {

    var $name = 'Publication';

    var $hasAndBelongsToMany = array(
        'Author'=>array(
            'className'=>'Author'
        )
    );     
}

class Author extends AppModel {

    var $name = 'Author';

    var $hasAndBelongsToMany = array(
        'Publication'=>array(
            'className'=>'Publication'
        )
    );

    var $belongsTo = array(
        'College' => array (
            'className' => 'College'
        )
    );      
}

class College extends AppModel {

    var $name = 'College';

var $hasMany = array(
        'Department'=>array(
            'className'=>'Department'
        )
    );      
}

class Department extends AppModel {

    var $name = 'Department';

var $belongsTo = array(
        'College'=>array(
            'className'=>'College'
        )
    );      
}

正确设置数据库表(HABTM的连接表等)。我试图找到一个数据库查询来统治它们。我想创建一个查询,找到所有与相关作者,学院,部门等相关的出版物。从表单中获取数据后,我试图运行这样的查询:

 $conditions = array(
            "Author.id" => $this->data['authors'],
            "Publication.year" => $this->data['year']                                
            );
$publications = $this->Publication->find('all', array('conditions' => $conditions));

这会抛出SQL错误,说Author.id不是有效字段。现在,这是因为在搜索Author.id时尚未完成与'authors'db表的连接。但是,如果我这样做:

$pubs = $this->Publication->find('all', array('conditions' => $conditions));

然后我得到一个包含所有相关作者的所有出版物的数组(但由于某种原因,不是相关的学院)。

我的问题是:在搜索Author.id之前,我需要做什么才能使表加入?我试图使用bindModel,包含子查询,但由于某种原因(可能是ID10T错误)无法使用它们。

感谢您的任何建议!

编辑: 以下调用的结果:

$this->Publication->recursive = 2;
$pubs = $this->Publication->find('all');

如下:

Array ( 

[0] =>阵列(     [出版物] =>数组([id] => 1 [标题] => TestArticle [年] => 2011 [type_id] => 3)     [类型] =>数组([id] => 3 [type_name] =>期刊文章)     [作者] =>阵列(         [0] =>数组([id] => 2 [firstname] => Jeremy [lastname] => Gustine [中] => A [faculty] => 0 [college_id] => 4 [AuthorsPublication] =>数组([id] => 3 [author_id] => 2 [publication_id] => 1)             [学院] =>数组([id] => 4 [college_name] =>信件,艺术和科学))         [1] =>数组([id] => 3 [名字] =>乔治[姓氏] =>奥巴马[中] => A [faculty] => 0 [college_id] => 6 [AuthorsPublication] =>数组([id] => 2 [author_id] => 3 [publication_id] => 1)             [学院] =>数组([id] => 6 [college_name] =>公共事务学院))         [2] =>数组([id] => 2 [firstname] => Jeremy [lastname] => Gustine [中] => A [faculty] => 0 [college_id] => 4 [AuthorsPublication] =>数组([id] => 1 [author_id] => 2 [publication_id] => 1)             [学院] =>数组([id] => 4 [college_name] =>信件,艺术和科学)))) [1] =>阵列(     [出版物] =>数组([id] => 2 [title] => TestBook [年] => 2010 [type_id] => 1)     [类型] =>数组([id] => 1 [type_name] =>图书)     [作者] =>阵列(         [0] =>数组([id] => 7 [firstname] =>索尼[lastname] =>东西[中] => L [faculty] => 0 [college_id] => 5 [AuthorsPublication] =>数组([id] => 4 [author_id] => 7 [publication_id] => 2)             [学院] =>数组([id] => 5 [学院名称] =>护理与健康科学)))))

希望这有点可读......

1 个答案:

答案 0 :(得分:1)

你的第二个发现是正确的

即。 $pubs = $this->Publication->find('all', array('conditions' => $conditions));

就在上面尝试使用递归2(代码下面)来获得相关的College

$this->Publication->recursive = 2;