The $_accessible
property for the db column of user_id
key & value are being changed to (int)0 => false
from 'user_id' => true
and I don't know how or why. Therefore, the user_id
in the form data is being ignored and is not part of the patched entity.
Specs: CakePHP 3.7 on Windows Server 2012 with MySQL 8.
The Warehouses entity was initially created via bake without the associated user_id
, but with the associated company_id
. I am & was able to save/edit records when company_id
is populated. I then manually added the user_id field to DB and all related Warehouses & Users files.
I've got validation logic that works appropriately to determine if company_id
or user_id
is populated. But since the user_id
field is being changed on the $_accessible property, my record is created without the user_id value when I am attempting to create such a record.
I have other tables in my app with similar setups working perfectly well (table.id, table.jobs_id, table.onsite_id) where job_id would be populated or onsite_is would be populated. But never both empty or both populated.
DB Table:
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']);
}
Warehouse.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']);
}
add.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>
In the Warehouses controller add method, I've debugged the ->newEntity(), the ->request->getData() and the ->patchEntity(). That information is below.
Thanks in advance for any help. It is greatly appreciated.
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'
}
Check your typo: '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
];
You can easy detect errors in your code using some scripts, like cakephp/cakephp-codesniffer, phpstan,..
composer require --dev cakephp/cakephp-codesniffer
composer require --dev phpstan/phpstan
composer require --dev phpstan/phpstan-deprecation-rules
Add in your composer scripts like:
"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"
},
Run from terminal / console:
composer cs-check
for check code style errorscomposer cs-fix
for quick fix some code style errorscomposer phpstan
for analysing code, start with --level 0