Cakephp 3.0:hasMany 显示键-> 值而不是数组

Cakephp 3.0 : hasMany show key->value instead of array

我正在使用 cakephp 3.0,我有一个 'users' table 和一个 'profiles' table。 users-table 包含最少的信息,用户名,电子邮件,密码。 配置文件表包含详细信息:头像、phone 号码、...

我能够 link 这 2 table 与 hasMany 关联。如果我查看用户的输出,它会将 'profiles' 属性 显示为一个数组,每个配置文件信息都有一个项目:

User (array)
   id    1
   username    admin
   email       admin@test.com
   password    ****
   -> created  (array)
   -> modified (array)
   -> profiles (array)
         -> 0  (array)
              id         1
              user_id    1
              key        avatar
              value      1.jpg
         -> 1  (array)
              id         2
              user_id    1
              key        phone
              value      555-1234

不,还是很难得到阿凡达。我将不得不检查所有配置文件项以查看哪个具有密钥 'avatar'。 我正在寻找的是可以提供这种结构的东西:

User (array)
   id    1
   username    admin
   email       admin@test.com
   password    ****
   -> created  (array)
   -> modified (array)
   -> profiles (array)
         avatar       1.jpg
         phone        555-1234

这会容易得多。现在我可以只到 User->profiles->avatar.

这可能吗?

您可以使用 Hash utility 来操作返回的数组:-

$data['profiles'] = Cake\Utility\Hash::combine(
    $data['profiles'], 
    '{n}.key', 
    '{n}.value'
);