Hello!
I am trying to save an associated model (defined in Table) through the patchEntity method.
Table Channel
PK - ChannelId
Name ..
...
...
Table ChannelContact
PK/FK - ChannelId
Street ....
Place ....
ChannelTable.php
$this->hasOne('ChannelContact', [
'foreignKey' => 'ChannelId',
'propertyName' => 'Contact',
'dependent' => true
]);
ChannelContactTable.php
$this->belongsTo('Channel', [
'foreignKey' => 'ChannelId',
'propertyName' => 'Channel'
]);
ChannelController.php add()
public function add()
{
$users = $this->User->find('list')->select(['UserId', 'Username'])->toArray();
$countries = TableRegistry::get('Country')->find('list')->select(['CountryId', 'Name'])->toArray();
$channel = $this->Channel->newEntity();
$channel->PasswordDisplay = $this->UserUtil->generatePassword(24);
if ($this->request->is('post')) {
$channel = $this->Channel->patchEntity($channel, $this->request->data, ['associated' => ['ChannelContact']]);
$channel->Password = $channel->PasswordDisplay;
$timestamp = Time::now();
$channel->DateCreate = $channel->DateChange = $timestamp;
$channel->Deleted = 0;
if ($this->Channel->save($channel, ['associated' => ['ChannelContact']])) {
$this->Flash->success(__('Der Channel wurde gespeichert!'));
debug($channel);
//return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Der Channel konnte nicht gespeichert werden. Versuchen Sie es später erneut!'));
}
$this->set('users', $users);
$this->set('countries', $countries);
$this->set(compact(['channel', 'channelContact']));
$this->set('_serialize', ['channel']);
}
Output of add()-debug:
object(App\Model\Entity\Channel) {
'PasswordDisplay' => 'twuGX5eClL0gWjPJ3RAi4g0y',
'UserId' => (int) 1,
'Description' => 'asdas',
'Mode' => 'stream',
'FSK' => true,
'Active' => true,
'ChannelContact' => [
'Street' => 'abc',
'ZipCode' => '1234',
'Place' => 'Ort',
'CountryId' => '1',
'ContactPersonOne' => '',
'ContactPersonTwo' => '',
'ContactPersonPositionOne' => '',
'ContactPersonPositionTwo' => '',
'PhoneOne' => '',
'PhoneTwo' => '',
'EmailOne' => '',
'EmailTwo' => ''
],
'Password' => '$2y$10$eviwYiUUTc4Dc4niO4jI3uaij0Y5LRCbB.pLCCd/GtK0kmf3y1AmC',
'DateChange' => object(Cake\I18n\Time) {
'time' => '2017-02-20T12:33:35+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'DateCreate' => object(Cake\I18n\Time) {
'time' => '2017-02-20T12:33:35+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'Deleted' => (int) 0,
'ChannelId' => (int) 18,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Channel'
}
Every post on “add” results in an new row in channel table, but not one in child table “channel_contact”.
UPDATE:
while waiting for response on this topic i tried to implement an “hasMany” - “1 to n” association -> wasted (also not working for me?)
1 channel has n channel_opening_hour
Table channel_opening_hour
ChannelOpeningHourId (PK)
ChannelId (FK)
Day
...
ChannelOpeningHourTable.php
$this->belongsTo('Channel', [
'foreignKey' => 'ChannelId',
'propertyName' => 'Channel'
]);
ChannelTable.php
$this->hasMany('ChannelOpeningHour', [
'foreignKey' => 'ChannelId',
'propertyName' => 'OpeningHour',
'dependent' => true
]);