【问题标题】:Cakephp 2 retrieve data where condition depends on child tableCakephp 2 检索条件取决于子表的数据
【发布时间】:2023-04-11 08:41:02
【问题描述】:

我有一个奇怪的行为,我的 cakephp 2 无法理解。 在我的模型“研究”中,我有很多关系:

class Study extends AppModel {

public $hasOne = array(
    'SubjectFilter' => array(
        'className' => 'Subject_filter',
        'dependent' => true
    )

    );


public $hasMany = array(

    'ExecutedStudyTable' => array(
            'className' => 'ExecutedStudyTable',
            'foreignKey' =>'study_id',
            'dependent' => true,
    ),   

);

ExecutedtStudyTable 模型如下所示:

class ExecutedStudyTable extends AppModel {

    public $belongsTo= array(
    'Study' => array(
        'className' => 'Study',
        'foreignKey' => 'study_id'
    ),
    'User' => array(
        'className' => 'User',
        'foreignKey' => false,
        'conditions' => array('ExecutedStudyTable.user_id = User.id')
    )

);

} 检索数据时,一切看起来都很好,直到我尝试这样做:

        $studies = $this -> Study -> find('all',array(
 'conditions' => array(
            'Study.user_id' => $cId,
            'OR'=>array(
                array('Study.state'=>'active'),
                array('Study.state'=>'rehearsal')
            ),                
            'SubjectFilter.studyCode'=>null,
            'ExecutedStudyTable.user_id'=>$user
 )
    ));

我收到此错误:Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ExecutedStudyTable.user_id' in 'where clause'

Cakephp 像这样构建这个查询:

 SELECT `Study`.`id`, `Study`.`user_id`, `Study`.`created`, `Study`.`modified`, `Study`.`started`, `Study`.`completed`, `Study`.`title`, `Study`.`description`, `Study`.`numberParticipants`, `Study`.`state`, `User`.`id`, `User`.`username`, `User`.`password`, `User`.`role`, `User`.`firstName`, `User`.`familyName`, `User`.`email`, `User`.`addressStreet`, `User`.`addressCity`, `User`.`addressZip`, `User`.`phone1`, `User`.`phone2`, `User`.`created`, `User`.`modified`, `User`.`passwordResetCode`, `User`.`passwordResetDate`, `User`.`sendUsernameDate`, `SubjectFilter`.`id`, `SubjectFilter`.`study_id`, `SubjectFilter`.`m`, `SubjectFilter`.`f`, `SubjectFilter`.`age18_24`, `SubjectFilter`.`age25_34`, `SubjectFilter`.`age35_44`, `SubjectFilter`.`age45_54`, `SubjectFilter`.`age55plus`, `SubjectFilter`.`studyCode` FROM `eyevido`.`studies` AS `Study` LEFT JOIN `eyevido`.`users` AS `User` ON (`Study`.`user_id` = `User`.`id`) LEFT JOIN `eyevido`.`subject_filters` AS `SubjectFilter` ON (`SubjectFilter`.`study_id` = `Study`.`id`) WHERE `Study`.`user_id` = 1402402538 AND ((`Study`.`state` = 'active') OR (`Study`.`state` = 'rehearsal')) AND `SubjectFilter`.`studyCode` IS NULL AND `ExecutedStudyTable`.`user_id` = 1130451831

ExecutedStudyTable 不像 SubjectFilter 那样连接,也没有为表定义别名。为什么这在 hasOne 关系上正常工作,而不是在 hasMany 上?我在哪里犯错了?

感谢您的回复

【问题讨论】:

    标签: cakephp cakephp-2.0 cakephp-model


    【解决方案1】:

    如果您想通过关联模型的字段过滤模型,一种方法是通过Joining tables

    注意:为模型 ExecutedStudyTable 提及正确的表名。还要提到两个表之间的连接条件。

    $studies = $this->Study->find('all',array(
     'fields' => array('Study.*'),
     'joins' => array(
          array('table' => 'executed_study_table', // Table name
            'alias' => 'ExecutedStudyTable',
            'type' => 'INNER',
            'conditions' => array(
                'ExecutedStudyTable.<field name> = Study.<field>', // Mention join condition here
            )
        )
     ),
     'conditions' => array(
            'Study.user_id' => $cId,
            'OR'=>array(
                array('Study.state'=>'active'),
                array('Study.state'=>'rehearsal')
            ),                
            'SubjectFilter.studyCode'    => null,
            'ExecutedStudyTable.user_id' => $user
      ),
      'recursive' => -1
    ));
    

    【讨论】:

    • 好的,我可以做到这一点,就像你说的那样,但是为什么连接会自动为我的模型 SubjectFilter 的 hasOne 关系工作?我没有收到错误消息,并且正如您在 SQL 查询中看到的那样,连接是由 cake 完成的
    • 其实和hasOne关系没有关系。加入独立工作。这就是为什么我们添加了recursive = -1
    • 在旧版本的 CakePHP 1 和 2 中,很难根据关联模型过滤数据。我们可以使用 Containable 行为,但我更喜欢 JOIN。可包含行为对于 CakePHP 3.x 更适合和灵活
    • 但是当 ExecutedStudyTable 中没有任何要加入的记录时,这将失败。有没有办法说 'ExecutedStudyTable.user_id'=>$user 或 ExecutedStudyTable is Empty?
    • 它在执行左连接而不是内连接时会起作用:'joins' => array( array('table' => 'executed_study_table', // 表名 'alias' => ' ExecutedStudyTable', 'type' => 'LEFT', 'conditions' => array( 'ExecutedStudyTable. = Study.', // 在此提及连接条件 ) ) ),
    猜你喜欢
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    相关资源
    最近更新 更多