2

有 User 和 UserProfile 模型。用户模型与 UserProfile 具有 hasOne 关联。UserProfile mysql 表为空。当我执行 $this->User->find('all', array('contain' => array('UserProfile'))) 而不是空的 UserProfile 数组时,如您所料,我得到一个填充了空的数组与架构对应的字段:

大批
(
    [0] => 数组
        (
            [用户] => 数组
                (
                    [id] => 1
                    [名字] => 乔
                    [姓氏] => 博客
                    [电子邮件] => katie.barnes@scotlandsdna.com
                    [密码] => $2a$10$re4r7AXWQcXgkKcabUqmtO6j.7p2bA1t2SLeG93eVsiDPBgGaeXRS
                    [启用] => 1
                    [user_group_id] => 1
                    [创建] => 2014-06-26 15:01:38
                    [修改] => 2014-06-26 15:01:38
                )

            [用户配置文件] => 数组
                (
                    [身份证] =>
                    [职位名称] =>
                    [user_id] =>
                    [启用] =>
                    [创建] =>
                    [修改] =>
                )

        )

任何人都看过这个并且知道如何解决它?!

4

1 回答 1

4

这是正确的行为,因为hasOne关联不是可选的。如果您想将其设为可选,请将其更改为 ahasMany并且只创建一条记录。

Cake 正在执行LEFT JOINfrom UsertoUserProfile并且查询结果NULL为丢失的记录生成了值。如果您在 Cake 之外的编辑器中执行 SQL,这与您将得到的结果相同。

于 2014-06-27T13:26:57.537 回答