dongwei2882 2015-04-12 03:23
浏览 32
已采纳

无法使用cakephp中的containsable从2个表中获取数据

I cant get data from 2 tables with conditions on each. There is no example in the docs. I just need rows from students table where a field is flagged as inactive and corresponding rows from Guardian table the email field is not null. Guardian has many Students. I get results but I get null values for Guardian email. I have tried many combinations with ID, model name etc but I am just not getting it.

$guardianFound = $this->Student->find('all', array(
    'contain' => array( 'Guardian', array(
        'conditions' => array('guardian_email !=' => null),
        'fields' => array('id,guardian_email','guardian_first_name,guardian_last_name')
    )),
    'conditions' => array('student_inactive' => 1),
    'fields'=> array('student_inactive' ),
    'recursive'=> -1
)); 

Result:

(int) 0 => array(
    'Student' => array(
        'student_inactive' => true
    ),
    'Guardian' => array(
        'guardian_email' => '',
        'guardian_first_name' => 'Tulay',
        'guardian_last_name' => 'Karadavut',
        'id' => '100'
    )
)

http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

  • 写回答

1条回答 默认 最新

  • duanpao6163 2015-04-12 07:27
    关注

    It looks like your query is working as expected, except that you probably want to exclude where guardian_email is null OR empty.

    To specify multiple values for a field in CakePHP, you can usually wrap it in another array, but for your case, since you're negating NULL and empty, it's a little complicated because of how CakePHP handles db field types. See this question for more info: Cake PHP complex find 'OR' is not working properly with null and empty string. So to simplify it you can set your Guardian conditions like so:

    'conditions' => array(
        'guardian_email IS NOT NULL',
        'guardian_email != ""'
    )
    

    You don't need the recursive key here since you're specifying what to contain using containable. So the final find call would be:

    $guardianFound = $this->Student->find('all', array(
        'contain' => array(
            'Guardian' => array(
                'conditions' => array(
                    'guardian_email IS NOT NULL',
                    'guardian_email != ""'
                ),
                'fields' => array(
                    'id', 'guardian_email', 'guardian_first_name', 'guardian_last_name'
                )
            )
        ),
        'conditions' => array('student_inactive' => 1),
        'fields' => array('student_inactive')
    ));
    

    Note that you're finding all inactive students, then only performing the guardian conditions on the guardians associated with that student. If no Guardian record matches the conditions the field would still be returned but with all the values as NULL.

    I'm assuming here that because Student belongsTo Guardian, and there would only be one Guardian per Student, you actually want to only return student records where the guardian email is provided. If that's the case, you just need to move the guardian conditions into the main conditions.

    $guardianFound = $this->Student->find('all', array(
        'contain' => array(
            'Guardian' => array(
                'fields' => array(
                    'id', 'guardian_email', 'guardian_first_name', 'guardian_last_name'
                )
            )
        ),
        'conditions' => array(
            'student_inactive' => 1,
            'Guardian.guardian_email IS NOT NULL',
            'Guardian.guardian_email != ""'
        ),
        'fields' => array('student_inactive')
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?