php - 我怎样才能在 cakephp 中正确关联这些表?

标签 php mysql cakephp crud cakephp-bake

我正在尝试使用 cakephp3 创建一组 CRUD。我的数据库模型如下所示:

My ER model

我使用蛋糕的身份验证教程来创建用户表及其类,它工作正常。但我想使用一组更复杂的角色,所以我创建了这些其他表。创建数据库模型后,我烘焙了相应的类,进行了一些调整,让系统和角色 CRUD 正常工作。现在我想集成 roles_users 表,可能在用户的 CRUD 中。

我想看看 cake's bake 在自己编写这个关系之前会怎么做,但我无法打开/rolesUsers。当我调用该 URL 时,我收到以下错误消息:

Cannot match provided foreignKey for "Roles", got "(role_id)" but expected foreign key for "(id, system_id)" RuntimeException

我认为这是因为 system_id 是角色表中的 PK,并且不存在于 roles_users 中(我将展示烘焙模型,这个 PK 将出现在角色类中)。 有没有不在 roles_users 中添加 system_id 的简单方法使其工作? IMO 添加这个额外的字段不会是一个大问题,但我想知道我是否做错了什么,一些错误的设计决策。

我的 src/Model/Table/RolesUsersTable.php:

<?php
namespace App\Model\Table;

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

/**
 * RolesUsers Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Users
 * @property \Cake\ORM\Association\BelongsTo $Roles
 */
class RolesUsersTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('roles_users');
        $this->displayField('user_id');
        $this->primaryKey(['user_id', 'role_id']);

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Roles', [
            'foreignKey' => 'role_id',
            'joinType' => 'INNER'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('valido_ate', 'valid', ['rule' => 'date'])
            ->requirePresence('valido_ate', 'create')
            ->notEmpty('valido_ate');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['user_id'], 'Users'));
        $rules->add($rules->existsIn(['role_id'], 'Roles'));
        return $rules;
    }
}

我的 src/Model/Table/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;

/**
 * Roles Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Systems
 */
class RolesTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('roles');
        $this->displayField('name');
        $this->primaryKey(['id', 'system_id']);

        $this->belongsTo('Systems', [
            'foreignKey' => 'system_id',
            'joinType' => 'INNER'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create');

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

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

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['system_id'], 'Systems'));
        return $rules;
    }
}

我的 src/Model/Table/UsersTable:

<?php 
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
class UsersTable extends Table{
    public function validationDefault(Validator $validator){
        return $validator
            ->notEmpty('username', 'O campo nome de usuário é obrigatório')
            ->notEmpty('password', 'O campo senha é obrigatório')
            ->notEmpty('role', 'O campo perfil é obrigatório')
            ->add('role', 'inList', [
                'rule' => ['inList', ['admin', 'author']],
                'message' => 'Escolha um perfil válido'
                ]
            );

    }
}
?>

最佳答案

由用户 jose_zap 在#cakephp @freenode 中回答:

在 RolesUsersTable.php 的初始化函数中,我向两个 $this->belongsTo 调用添加了一个参数,包括“bindingKey”和值“id”。所以这个旧代码:

$this->belongsTo('Users', [
        'foreignKey' => 'user_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Roles', [
        'foreignKey' => 'role_id',
        'joinType' => 'INNER'
    ]);

变成了这个:

$this->belongsTo('Users', [
        'foreignKey' => 'user_id',
        'bindingKey' => 'id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Roles', [
        'foreignKey' => 'role_id',
        'bindingKey' => 'id',
        'joinType' => 'INNER'
    ]);

关于php - 我怎样才能在 cakephp 中正确关联这些表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34422355/

相关文章:

mysql - 有没有办法检测MySQL是否在Google云SQL中运行?

php - Rest api cakephp 分页

php - 从 Carbon 类对象获取元素

mysql - Rake 似乎在劫持 MySQL

在 PDF 中选中 PHP mPDF 复选框

Mysql如何从一个表中选择列值不是X和Y的所有记录

jquery - 使用 jQuery 操作 HTML 表格

php - 将 jQuery Mobile 与 Facebook Like 和 Twitter Tweet 集成

php - 用正则表达式匹配逗号后跟换行符

php - 为什么这个正则表达式有 3 个匹配项,而不是 5 个?