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')
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图