【问题标题】:Cakephp 3.7 - Entity accessible property element changedCakephp 3.7 - 实体可访问的属性元素已更改
【发布时间】:2020-01-13 20:37:45
【问题描述】:

user_id 键和值的 db 列的 $_accessible 属性正在从 'user_id' => true 更改为 (int)0 => false,我不知道如何或为什么。因此,表单数据中的user_id 将被忽略,并且不是修补实体的一部分。

规格:Windows Server 2012 上的 CakePHP 3.7 和 MySQL 8。

Warehouses 实体最初是通过烘焙创建的,没有关联的user_id,但有关联的company_id。当company_id 被填充时,我能够保存/编辑记录。然后我手动将 user_id 字段添加到 DB 和所有相关的 Warehouses & Users 文件中。

我的验证逻辑可以正常工作以确定是否填充了company_iduser_id。但由于 $_accessible 属性上的 user_id 字段正在更改,因此当我尝试创建此类记录时,我的记录创建时没有 user_id 值。

我的应用程序中的其他表具有类似的设置,运行良好(table.id、table.jobs_id、table.onsite_id),其中将填充 job_id 或填充 onsite_is。但永远不要同时为空或都已填充。

数据库表:

CREATE TABLE `warehouses` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `company_id` int(11) NOT NULL DEFAULT '0',
  `user_id` int(11) NOT NULL DEFAULT '0',
  `created` datetime NOT NULL,
  `create_user` int(10) unsigned NOT NULL DEFAULT '0',
  `modified` datetime NOT NULL,
  `modify_user` int(10) unsigned NOT NULL DEFAULT '0',
  `costed` tinyint(1) NOT NULL DEFAULT '0',
  `deleted` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `id` (`id`,`name`,`deleted`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

WarehousesTable.php

public function initialize(array $config)
  {
      parent::initialize($config);
      $this->setTable('warehouses');
      $this->setDisplayField('name');
      $this->setPrimaryKey('id');
      $this->addBehavior('Timestamp');
      $this->hasMany('Inventory', ['foreignKey' => 'warehouse_id']);
      $this->belongsTo('Companies', ['foreignKey' => 'company_id']);
      $this->belongsTo('Users', ['foreignKey' => 'user_id']);
  }

仓库.php

protected $_accessible = [
      'name' => true,
      'company_id' => true,
      'user_id' > true,
      'created' => true,
      'create_user' => true,
      'modified' => true,
      'modify_user' => true,
      'costed' => true,
      'deleted' => true,
      'inventory' => true 
  ];

UsersTable.php

public function initialize(array $config)
  {
    parent::initialize($config);
    $this->setTable('users');
    $this->setPrimaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsTo('Companies');
    $this->setDisplayField('full_name');
    $this->hasMany('Uploads', ['foreignKey' => 'user_id']);
   $this->hasMany('Warehouses', ['foreignKey' => 'user_id']);
  }

添加.ctp

<div class="warehouses form">
<?php echo $this->Form->create($warehouse);?>
    <fieldset>
        <legend>Add Warehouse</legend>
    <?php
        echo $this->Form->control('name');
        echo $this->Form->control('what_needed', array('type' => 'radio', 'options' => ['0' => 'Internal', 'C' => 'Customer', 'T' => 'Field Tech'], 'label' => 'Warehouse for internal, Customer or Field Tech?'));
        echo $this->Form->control('company_id', array('empty' => 'Select a company', 'options' => $companies, 'label' => 'Company'));
        echo $this->Form->control('user_id', array('empty' => 'Select a user', 'options' => $users, 'label' => 'User'));
        echo $this->Form->control('costed', array('label'=>'Is Warehouse Costed?'));
        echo "</fieldset>";
    ?>
    </fieldset>
<?php 
echo $this->Form->button(__('Submit'));
echo $this->Form->end();
?>
</div>

在 Warehouses 控制器的 add 方法中,我调试了 ->newEntity()、->request->getData() 和 ->patchEntity()。该信息如下。

提前感谢您的帮助。非常感谢。

debug($warehouse = $this->Warehouses->newEntity());
object(App\Model\Entity\Warehouse) {

    '[new]' => true,
    '[accessible]' => [
        'name' => true,
        'company_id' => true,
        (int) 0 => false, //*why is this false? what happened to user_id*
        'created' => true,
        'create_user' => true,
        'modified' => true,
        'modify_user' => true,
        'costed' => true,
        'deleted' => true,
        'inventory' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[hasErrors]' => false,
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Warehouses'

}

debug($this->request->getData());
[
    'name' => 'Blah Blah Trunk',
    'what_needed' => 'T',
    'company_id' => '',
    'user_id' => '5',
    'costed' => '0'
]

debug($this->Warehouses->patchEntity($warehouse, $this->request->getData()));
object(App\Model\Entity\Warehouse) {

    'name' => 'Blah Blah Trunk',
    'costed' => false,
    'create_user' => (int) 2,
    'modify_user' => (int) 2,
    '[new]' => true,
    '[accessible]' => [
        'name' => true,
        'company_id' => true,
        (int) 0 => false, //*why is this false? what happened to user_id*
        'created' => true,
        'create_user' => true,
        'modified' => true,
        'modify_user' => true,
        'costed' => true,
        'deleted' => true,
        'inventory' => true
    ],
    '[dirty]' => [
        'name' => true,
        'costed' => true,
        'create_user' => true,
        'modify_user' => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[hasErrors]' => false,
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Warehouses'

}

【问题讨论】:

    标签: cakephp cakephp-3.x cakephp-3.7


    【解决方案1】:

    检查您的错字:'user_id' > true

    protected $_accessible = [
          'name' => true,
          'company_id' => true,
          'user_id' > true, // <---------- you have typo here, must be 'user_id' => true
          'created' => true,
          'create_user' => true,
          'modified' => true,
          'modify_user' => true,
          'costed' => true,
          'deleted' => true,
          'inventory' => true 
      ];
    

    您可以使用一些脚本轻松检测代码中的错误,例如 cakephp/cakephp-codesniffer、phpstan、..

    composer require --dev cakephp/cakephp-codesniffer
    composer require --dev phpstan/phpstan
    composer require --dev phpstan/phpstan-deprecation-rules
    

    添加您的作曲家脚本,例如:

    "scripts": {
        "cs-check": "phpcs --colors -p --ignore=*.js --standard=vendor/cakephp/cakephp-codesniffer/CakePHP config/ src/ plugins/ tests/",
        "cs-fix": "phpcbf --colors --ignore=*.js --standard=vendor/cakephp/cakephp-codesniffer/CakePHP config/ src/ plugins/ tests/",
        "phpstan": "./vendor/bin/phpstan analyse src plugins --level=0",
        "test": "phpunit --colors=always"
    },
    

    从终端/控制台运行:

    • composer cs-check 检查代码样式错误
    • composer cs-fix 快速修复一些代码样式错误
    • composer phpstan 用于分析代码,从 --level 0 开始

    【讨论】:

    • 叹息。我觉得自己好傻。最蹩脚和最容易纠正的。也感谢您的建议。我一定会调查的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多