Это мое сомнение в cakephp 3.0 с has_many и принадлежит_to: Мне нужно сохранить две модели в одной форме. Это мои модели:О has_many и принадлежит in cakephp 3.0
CiudananosTable.php:
public function initialize(array $config)
{
$this->table('ciudadanos');
$this->displayField('nombrecompleto');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Telefonos', [
'foreignKey' => 'ciudadano_id'
]);
}
и TelefonosTable.php:
public function initialize(array $config)
{
$this->table('telefonos');
$this->displayField('telefono');
$this->primaryKey('id');
$this->belongsTo('Ciudadanos', [
'foreignKey' => 'ciudadano_id'
]);
}
В мой контроллер:
public function add()
{
$ciudadano = $this->Ciudadanos->newEntity($this->request->data, [
'associated' => ['Telefonos']]);
if ($this->request->is('post')) {
$ciudadano = $this->Ciudadanos->patchEntity($ciudadano, $this->request->data, [
'associated' => ['Telefonos']]);
if ($this->Ciudadanos->save($ciudadano)) {
$this->Flash->success('The ciudadano has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The ciudadano could not be saved. Please, try again.');
}
}
$this->set(compact('ciudadano'));
$this->set('_serialize', ['ciudadano']);
}
и это моя форма в шаблонах /Ciudadanos/add.ctp
<?= $this->Form->create($ciudadano); ?>
<fieldset>
<legend><?= __('Add Ciudadano') ?></legend>
<?php
echo $this->Form->input('nombrecompleto');
echo $this->Form->input('Telefono.telefono');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
Проблема в том, что в «input (« Telefono.telefono »); не сохраняйте ничего. Что я делаю неправильно?
Спасибо и извините мой английский :-)
Спасибо ndm, но не работает :-(Я добавляю echo $ this-> Form-> input ('telefonos.0.id'); echo $ this-> Form-> input ('telefonos.0. telefono '); –
@JoseRafaelCamejo Извините, но я не могу помочь вам с «_doesn't work_», так как это не предоставляет никакой информации, которая позволила бы любому устранить ее. – ndm
Можете ли вы рассказать мне, какая информация Нужно? –