CakePHP - Relation 1 ~ n

1

Good evening, I'm having difficulty in CakePHP 3 and the problem is this:

  

Can not insert row, some of the primary key values are missing. Got (,), expecting (idstorie, users_iduser)

  • The system should add a "Storie" to the DB, in that it should automatically generate its ID (Auto_Implement) and get the user id that 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.

    
asked by anonymous 30.06.2016 / 04:11

2 answers

1

[RESOLVED] - Refined the entire database from scratch.

  

The error was : Can not insert row, some of the primary key values are missing. Got (,), expecting (idstorie, users_iduser)

The problem was that I generated the database with the MySQL Workbench based on my project, and the settings were conflicting with the BAKE CakePHP).

  • Tip for anyone starting with CAKE:
  

Do everything at hand, do not rely on tools, so you know exactly where the error is, and you will not have the same problem as me. I lost almost 2 days with this error.

    
30.06.2016 / 14:35
0

If someone encounters this type of error, in a ManyToMany relationship, make explicit what foreign keys in the Connection Table in Table Classes. For example (in a ManyToMany relation between the User and Alert Tables (without using cake's plural convention)):

class AlertaTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsToMany('Usuario', [
                                         'joinTable' => 'alerta_usuario', 
                                         'foreignKey' => 'alerta_id'
                                        ]
                            );
    }
}

And in the UserTable class:

class UsuarioTable extends Table
    {
        public function initialize(array $config)
        {
            $this->belongsToMany('Alerta', [
                                        'joinTable' => 'alerta_usuario', '
                                        foreignKey' => 'usuario_id'
                                        ]
                            );
        }
    }

This should solve the problem. Remembering that it is not necessary to create the Table Class for the binding table, Cake solves this for you.

    
07.01.2019 / 14:25