无法得到cakephp 3.0协会的工作

时间:2015-07-01 12:02:39

标签: cakephp

我一直在使用cakephp 2.x一段时间了,现在在cakephp 3.0上开始了一个新项目。 但我无法让该关联适用于我的用户/角色表。 我浏览了cakephp文档,并在谷歌搜索了很多答案,但没有。

任何帮助?

MySQL'用户'表:

id        int(11)        unsigned    NOT NULL    AUTO_INCREMENT    PRIMARY KEY
username  varchar(32)                NOT NULL
email     varchar(64)                NOT NULL
password  varchar(255)               NOT NULL
role_id   int(11)        unsigned    NOT NULL
active    tinyint(1)     unsigned    NOT NULL
created   datetime                   NULL
modified  datatime                   NULL

MySQL'角色'表:

id        int(11)        unsigned    NOT NULL    AUTO_INCREMENT     PRIMARY KEY
name      varchar(64)                NOT NULL
level     int(11)                    0
created   datetime                   NULL
modified  datetime                   NULL

/src/Model/Entitiy/Role.php

<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Role extends Entity
{
     protected $_accessible = [
          'name' => true,
          'level' => true,
     ];
}

/src/Model/Entity/User.php

<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;
user Cake\Auth\DefaultPasswordHasher;

class Userextends Entity
{
     protected $_accessible = [
          'username' => true,
          'email' => true,
          'password' => true,
          'role_id' => true,
     ];

     protected function _setPassword($value)
     {
          return (new DefaultPasswordHasher)->hash($value);
     }
}

的src /型号/表/ UsersTable.php

<?php
namespace App\Model\Table;

use App\Model\Entity\User;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        $this->table('users');
        $this->displayField('username');
        $this->primaryKey('id');

        $this->belongsTo('Roles');
    }

    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create');

        $validator
            ->requirePresence('username', 'create')
            ->notEmpty('username');

        $validator
            ->add('email', 'valid', ['rule' => 'email'])
            ->requirePresence('email', 'create')
            ->notEmpty('email');

        $validator
            ->requirePresence('password', 'create')
            ->notEmpty('password');

        return $validator;
    }

    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->isUnique(['username']));
        $rules->add($rules->isUnique(['email']));
        return $rules;
    }
}

的src /型号/表/ RolesTable.php

<?php
namespace App\Model\Table;

use App\Model\Entity\Role;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class RolesTable extends Table
{
    public function initialize(array $config)
    {
        $this->table('roles');
        $this->displayField('name');
        $this->primaryKey('id');
        $this->hasMany('Users');
    }

    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create');

        $validator
            ->requirePresence('name', 'create')
            ->notEmpty('name');

        $validator
            ->add('level', 'valid', ['rule' => 'numeric'])
            ->requirePresence('level', 'create')
            ->notEmpty('level');

    return $validator;
    }

    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->isUnique(['name']));
        $rules->add($rules->isUnique(['level']));
        return $rules;
    }
}

我做了一个快速检查(我知道,不是将echo或print_r放在控制器中的最佳做法,但这只是一个检查)来打印第一个用户。

SRC /控制器/ UsersController.php

<?php

namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;

class UsersController extends AppController
{

    public function index()
    {
        // In a controller or table method.
        echo '<pre>';
        $query = $this->Users->findById(1);
        foreach ($query as $user) 
        {
             print_r($user->toArray());
        }
        echo '</pre>';
    }

    ...
}

但是当我打开'用户'页面时,它只是显示用户类的信息,带有'role_id'。但不是角色细节。

Array
(
    [id] => 1
    [username] => Test
    [email] => test@test.com
    [password] => ***
    [role_id] => 1
    [active] => 1
    [created] => Cake\I18n\Time Object
        (
            [date] => 2015-06-29 12:43:39.000000
            [timezone_type] => 3
            [timezone] => UTC
        )

    [modified] => Cake\I18n\Time Object
        (
            [date] => 2015-06-29 12:43:39.000000
            [timezone_type] => 3
        [timezone] => UTC
        )
)

1 个答案:

答案 0 :(得分:1)

我认为问题是3.0 cakePHP不会自动执行急切加载...尝试更改您的查找以使用新的第二个参数并将其指定为:

$this->get(1, ['contain' => ['Roles']]);

或使用

$this->find('all')
     ->where('User.id' => 1)
     ->contain('Roles');