Cakephp多级协会无法正常工作

时间:2014-03-28 07:31:42

标签: mysql cakephp associations

我尝试了什么:

我的第一个模特

<?php
App::uses('AppModel', 'Model');

class Appregister extends AppModel {

    public $name        = 'Appregister';
    public $useTable    = 'app_register';


    public $primaryKey  = 'id';

    var $hasOne = array(        
        'Pollusers' => array(
            'className' => 'Pollusers',
            'foreignKey' => 'AppRegisterId',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

第二

<?php
App::uses('AppModel', 'Model');

class Pollusers extends AppModel {

    public $name        = 'Pollusers';
    public $useTable    = 'poll_users';


    public $primaryKey  = 'id';
    var $hasMany = array(
        'Pollresult' => array(
            'className' => 'Pollresult',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
}

第三

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

App::uses('AppModel', 'Model');

/**
 * CakePHP Pollresult
 * @author Kalpesh
 */
class Pollresult extends AppModel {
    public $name        = 'Pollresult';
    public $useTable    = 'poll_result';


    public $primaryKey  = 'id';


}

现在我已经开始了 的 $this->Appregister->recursive = 2;

它仍然给我一个错误:

 Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Pollresult.poll_id' in 'where clause'

SQL Query: SELECT `Appregister`.`device_token`, `Appregister`.`type` FROM
`reviewpr_aljassem`.`app_register` AS `Appregister` 
LEFT JOIN `reviewpr_aljassem`.`poll_users` AS `Pollusers` 
       ON (`Pollusers`.`AppRegisterId` = `Appregister`.`id`) 
WHERE 1=1 And 
     `Pollresult`.`poll_id` = 11 And 
     `Pollresult`.`answer_id` = "1" AND 
      `Appregister`.`type` != 3 

这里有什么不对,为什么它不加入Pollresult查询?

更新

这里 的 Appregister ----hasone---> Pollusers ----hasmany---> Pollresult

所以我检查并了解了cakephp如何处理多种关系:

[4] => Array
                (
                    [query] => SELECT `Appregister`.`id`, `Appregister`.`app_id`, `Appregister`.`vendor_id`, `Appregister`.`device_token`, `Appregister`.`type`, `Appregister`.`created_date`, `Appregister`.`modified_date`, `Pollusers`.`id`, `Pollusers`.`AppRegisterId`, `Pollusers`.`age`, `Pollusers`.`gender`, `Pollusers`.`area_id`, `Pollusers`.`governor_ate`, `Pollusers`.`email`, `Pollusers`.`name`, `Pollusers`.`name_en`, `Pollusers`.`date` FROM `reviewpr_aljassem`.`app_register` AS `Appregister` LEFT JOIN `reviewpr_aljassem`.`poll_users` AS `Pollusers` ON (`Pollusers`.`AppRegisterId` = `Appregister`.`id`)  WHERE 1 = 1
                    [params] => Array
                        (
                        )

                    [affected] => 96
                    [numRows] => 96
                    [took] => 2
                )

            [5] => Array
                (
                    [query] => SELECT `Pollresult`.`id`, `Pollresult`.`poll_id`, `Pollresult`.`user_id`, `Pollresult`.`answer_id`, `Pollresult`.`date` FROM `reviewpr_aljassem`.`poll_result` AS `Pollresult`   WHERE `Pollresult`.`user_id` = (1)
                    [params] => Array
                        (
                        )

                    [affected] => 1
                    [numRows] => 1
                    [took] => 0
                )

            [6] => Array
                (
                    [query] => SELECT `Pollresult`.`id`, `Pollresult`.`poll_id`, `Pollresult`.`user_id`, `Pollresult`.`answer_id`, `Pollresult`.`date` FROM `reviewpr_aljassem`.`poll_result` AS `Pollresult`   WHERE `Pollresult`.`user_id` = (10)
                    [params] => Array
                        (
                        )

                    [affected] => 1
                    [numRows] => 1
                    [took] => 0
                )

在单独的查询中获取pollresult数据,现在如何在同一查询中加入它?

0 个答案:

没有答案