I am not able to save associated data in table…
My Controller
public function add() {
$venue = $this->Venues->newEntity();
if ($this->request->is('post')) {
// $associated = ['associated' => ['VenuesTypes']];
$venue = $this->Venues->patchEntity($venue, $this->request->data);
$venue->user_id = $this->Auth->user('id');
pr($venue);
exit();
if ($result = $this->Venues->save($venue)) {
$this->copyVenueImages($result->id);
$this->Flash->success(__('The venue has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The venue could not be saved. Please, try again.'));
}
// $category = $this->Venues->EventCatagories->find('list');
$types = $this->Venues->Types->find('list');
// $amenities = $this->Venues->Amenities->find('list');
$countries = $this->Venues->Countries->find('list');
$this->set(compact('venue', 'category', 'countries', 'types'));
$this->set('imgId', uniqid());
}
Venue Model
// Venues Types
$this->belongsToMany('VenuesTypes', [
'foreignKey' => 'venues_id',
'targetForeignKey' => 'types_id',
'joinTable' => 'venues_types'
]);
$this->hasMany('Types', [
'joinTable' => 'venues_types',
]);
//Types Model
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('types');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('VenuesTypes', [
'foreignKey' => 'types_id'
]);
}
// VenuesTypes Model
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('venues_types');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Venues', [
'foreignKey' => 'venues_id'
]);
$this->belongsTo('Types', [
'foreignKey' => 'types_id'
]);
}
Result data :-
App\Model\Entity\Venue Object
(
[title] => Costumes
[contact_person] =>
[email] =>
[phone_no] =>
[mobile_no] =>
[address] =>
[city] =>
[state] =>
[zipcode] =>
[latitude] =>
[longitude] =>
[venues_types] => Array
(
[_ids] => Array
(
[0] => 2
[1] => 5
[2] => 10
[3] => 13
)
)
[setting] =>
[capacity] =>
[website] =>
[user_id] => 1
[[new]] => 1
[[accessible]] => Array
(
[title] => 1
[slug] => 1
[user_id] => 1
[contact_person] => 1
[email] => 1
[website] => 1
[address] => 1
[country_id] => 1
[state] => 1
[city] => 1
[zipcode] => 1
[latitude] => 1
[longitude] => 1
[phone_no] => 1
[mobile_no] => 1
[image] => 1
[created] => 1
[modified] => 1
[country] => 1
[capacity] => 1
[setting] => 1
[venue_categories] => 1
[venues_types] => 1
[venues_amenities] => 1
[venue_enqueries] => 1
)
[[dirty]] => Array
(
[title] => 1
[contact_person] => 1
[email] => 1
[phone_no] => 1
[mobile_no] => 1
[address] => 1
[city] => 1
[state] => 1
[zipcode] => 1
[latitude] => 1
[longitude] => 1
[venues_types] => 1
[setting] => 1
[capacity] => 1
[website] => 1
[user_id] => 1
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[invalid]] => Array
(
)
[[repository]] => Venues
)