1
Good evening, I’m having a hard time on Cakephp 3 and the problem is this::
Cannot Insert Row, some of the Primary key values are Missing. Got (, ), expecting (idstorie, users_iduser)
- The system should add a "Storie" to the BD, in which it should automatically generate its ID (Auto_implement) and take the ID of the user who is logged in.
TEMPLATE/STORIES/ADD
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Stories'), ['action' => 'index']) ?></li>
</ul>
</nav>
<div class="stories form large-9 medium-8 columns content">
<?= $this->Form->create($story) ?>
<fieldset>
<legend><?= __('Add Story') ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Form->input('content');
echo $this->Form->input('users_iduser',['value'=>$authUser['iduser'],'type'=>'hidden']); //essa linha pega o usuário da sessão que carrega ao logar.
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
CONTROLLER/STORIESCONTROLLER
public function add()
{
$story = $this->Stories->newEntity();
if ($this->request->is('post')) {
$this->Stories->patchEntity($story, $this->request->data);
if ($this->Stories->save($story)) {
$this->Flash->success(__('The story has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The story could not be saved. Please, try again.'));
}
}
$this->set(compact('story'));
$this->set('_serialize', ['story']);
}
TABLE/STORIESTABLE
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class StoriesTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('stories');
$this->displayField('title');
$this->primaryKey(['idstorie', 'users_iduser']);
$this->addBehavior('Timestamp');
}
public function validationDefault(Validator $validator)
{
$validator
->integer('idstorie')
->allowEmpty('idstorie', 'create');
$validator
->requirePresence('title', 'create')
->notEmpty('title');
$validator
->requirePresence('content', 'create')
->notEmpty('content');
$validator
->integer('users_iduser')
->allowEmpty('users_iduser', 'create');
return $validator;
}
}
ENTITY/STORY
namespace App\Model\Entity;
use Cake\ORM\Entity;
class Story extends Entity
{
public $belongsTo = array('User');
protected $_accessible = [
'*' => true,
'idstorie' => false,
'users_iduser' => false
];
}
Thanks in advance.