cakePHP 2.0关联模型未检索数据

时间:2013-02-11 15:06:18

标签: cakephp model

我有2张桌子,民兵和禁令。民兵可以有许多禁令,禁令只属于一个民兵。我在模型中设置了这些,但是当我为民兵打电话时,禁令不会被撤销。

民兵模型

class Militia extends AppModel {
    public $belongsTo = array(
        'User' => array(
            'className'    => 'User',
            'foreignKey'   => 'user_id'
        )
    );

    public $hasMany = array(
        'Injunction' => array(
            'className'     => 'Injunction',
            'foreignKey'    => 'militia_id'
            //'conditions'    => array("not" => array('Injunction.removed' => null))
        )
    );
}

禁令模式

class Injunction extends AppModel {
    public $belongsTo = array(
        'Militia' => array(
            'className'    => 'Militia',
            'foreignKey'   => 'militia_id'
        )
    );
}

以及获取民兵成员的查询

$user = $this->Session->read("User");

        $militias = $this->Militia->find('all',
            array(
                'conditions'=>array(
                    "Militia.user_id"   => $user['User']['id'],
                    "Militia.deleted"   => 0
                )
            )
        );

输出

    /app/Controller/UsersController.php (line 41)
array(
    (int) 0 => array(
        'Militia' => array(
            'id' => '26',
            'first_name' => 'Chris',
            'last_name' => 'Morris',
            'created' => '2013-02-11 13:45:24',
            'user_id' => '2',
            'status' => '1',
            'deleted' => '0'
        )
    ),
    (int) 1 => array(
        'Militia' => array(
            'id' => '31',
            'first_name' => 'John',
            'last_name' => 'Smith',
            'created' => '2013-02-11 14:03:50',
            'user_id' => '2',
            'status' => '0',
            'deleted' => '0'
        )
    ),
    (int) 2 => array(
        'Militia' => array(
            'id' => '32',
            'first_name' => 'test',
            'last_name' => 'user',
            'created' => '2013-02-11 14:21:38',
            'user_id' => '2',
            'status' => '0',
            'deleted' => '0'
        )
    ),
    (int) 3 => array(
        'Militia' => array(
            'id' => '33',
            'first_name' => 'test',
            'last_name' => 'user',
            'created' => '2013-02-11 14:24:02',
            'user_id' => '2',
            'status' => '1',
            'deleted' => '0'
        )
    )
)

我之前在其他项目上做过同样的事情,但由于某种原因,这一次没有取出相关数据。它可能像拼写错误一样愚蠢,但我一直在寻找和测试,并且找不到任何错误。

1 个答案:

答案 0 :(得分:0)

  1. 检查ORM(hasOne,hasMany,belongsTo,hasAndBelongsToMany)定义,
  2. 检查外键ID是否设置正确
  3. 检查表格及其相关表格中是否有数据。
  4. 检查无条件查找结果
  5. 检查递归级别是否大于0
  6. 要排除旧的缓存查询问题,请将调试级别设置为0
  7. 查看sql dump的底部部分,了解正在触发的查询,如果感觉不错,请尝试使用SQL远离Cake / Php的相同查询。