【问题标题】:CakePHP hasOne association returns array of empty fields for non-existant recordCakePHP hasOne 关联返回不存在记录的空字段数组
【发布时间】:2014-06-27 13:23:46
【问题描述】:

拥有 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] => [启用] => [创建] => [修改] => ) )

有人看到这个并且知道如何解决它吗?!

【问题讨论】:

    标签: mysql cakephp model-associations cakephp-2.5


    【解决方案1】:

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

    Cake 正在执行从UserUserProfileLEFT JOIN,并且查询结果为缺少的记录生成了NULL 值。如果您在 Cake 之外的编辑器中执行 SQL,这与您得到的结果相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多