CakePHP3 HABTM error inserting data into table

1

I have the error Can not insert row in "addresses_companies" table, it has no primary key in my application, after entering a company and filling in the address data and selecting a company appears the error quoted above.

Follow the code below:

AddressesController.php

public function add()
{
    $address = $this->Addresses->newEntity();

    if ($this->request->is('post')) {
        $address = $this->Addresses->patchEntity($address, $this->request->data, ['associated' => ['Companies']]);
        if ($this->Addresses->save($address)) {
            $this->Flash->success(__('The address has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The address could not be saved. Please, try again.'));
        }
    }
    $companies = $this->Addresses->Companies->find('list', ['limit' => 200]);
    $this->set(compact('address', 'companies'));
    $this->set('_serialize', ['address']);
}

Address     

use Cake\ORM\Entity;

/**
 * Address Entity.
 */
class Address extends Entity
{

/**
 * Fields that can be mass assigned using newEntity() or patchEntity().
 *
 * @var array
 */
   protected $_accessible = [
    'CEP' => true,
    'ENDERECO' => true,
    'BAIRRO' => true,
    'CIDADE' => true,
    'ESTADO' => true,
    'UF' => true,
    'companies' => true,
    'addresses_companies' => true,
   ];
}

AddressesTable     

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

/**
* Addresses Model
*
* @property \Cake\ORM\Association\BelongsToMany $Companies
*/
class AddressesTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    $this->table('addresses');
    $this->displayField('ID');
    $this->primaryKey('ID');
    $this->belongsToMany('Companies', [
        'foreignKey' => 'address_id',
        'targetForeignKey' => 'company_id',
        'joinTable' => 'addresses_companies',
        'dependent' => true
    ]);
}

/**
 * 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
        ->add('CEP', 'valid', ['rule' => 'numeric'])
        ->requirePresence('CEP', 'create')
        ->notEmpty('CEP');

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

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

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

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

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

    return $validator;
}
}

add.ctp

<div class="addresses form large-10 medium-9 columns">
<?= $this->Form->create($address) ?>
<fieldset>
    <legend><?= __('Add Address') ?></legend>
    <?php
        echo $this->Form->input('CEP');
        echo $this->Form->input('ENDERECO');
        echo $this->Form->input('BAIRRO');
        echo $this->Form->input('CIDADE');
        echo $this->Form->input('ESTADO');
        echo $this->Form->input('UF');
        echo $this->Form->input('companies._ids', ['options' => $companies]);
    ?>
    </fieldset>
       <?= $this->Form->button(__('Submit')) ?>
       <?= $this->Form->end() ?>
</div>

I want to understand what is missing, as I do not find examples on the internet and the one found on the CakePHP website does not help me.

    
asked by anonymous 24.08.2015 / 04:29

1 answer

0

I solved the problem by adding them as primary key, but now the following sentence appeared to me.

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

Can anyone help me solve this problem?

Thankful

    
24.08.2015 / 17:00