1
I am with the error Cannot 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.
Follows code:
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’s missing, because I can’t find examples on the Internet and the one found on the Cakephp website doesn’t help me.