【问题标题】:hasOne does not deliver users and profileshasOne 不提供用户和个人资料
【发布时间】:2018-08-12 08:53:42
【问题描述】:

当我想检索我的用户时,我没有个人资料。 我按照约定设置了我的数据库。

我的模型(UserTable)中有以下代码:

<?php
// src/Model/Table/UsersTable.php
namespace App\Model\Table;

use Cake\ORM\Table;

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');
        $this->hasOne('Profiles')
          ->setDependent(true);
    }
}

我的控制器是这样的:

<?php
// src/Controller/UsersController.php

namespace App\Controller;

class UsersController extends AppController
{
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Paginator');
        $this->loadComponent('Flash'); // Include the FlashComponent
    }

    public function index()
    {
        $query = $this->Users->find();
        $results = $query->all();
        $this->set(compact('results'));
    }
}

调用索引后,我得到了所有用户,这是一件好事。但是没有个人资料。 我想,我在某处遗漏了一行代码。任何帮助表示赞赏。

【问题讨论】:

    标签: cakephp-3.x has-one


    【解决方案1】:

    我错过了contain 的事情。在 cakephp 2 中,不需要它。

    控制器代码现在是这样的(它正在工作):

    $query = $this->Users->find();
    $query->contain(['Profiles']);
    $results = $query->all();
    

    还是谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 2016-08-31
      • 1970-01-01
      • 2010-12-01
      • 2013-05-06
      相关资源
      最近更新 更多