I have two models Property and Amenity the relationship between them is many to many. the reference class is PropertyAmenity when i want to insert single record into one model it is easy for me.
to insert into Property
$property = new Property();
$property->serial = 'SER09088';
$property->title = 'Some title';
$property->save();
and to insert record into Amenity it is
$amenity = new Amenity();
$amenity->name = 'Swimming Pool';
$amenity->save();
what i want to do is add existing amenities to Property the record of which holds in reference class PropertyAmenity for example something like.
$property = new Property();
$property->serial = 'SER09088';
$property->title = 'Some title';
$property->PropertyAmenity->amenity_id[] = 1;
$property->save();
when i try the above code it gives me following exception
Add is not supported for PropertyAmenity
the situation is i gather collective information from a form, from a user. for example a user inputs all the value related to Property and select existing Multiple Amenities from the form and submit. i need to save Property in property table and the selected amenities in property_amenity table which consist of two columns property_id and amenity_id, how do insert this value in doctrine?
After digging around a little bit, i found the solution myself and as expected, it is so easy to implement. the only change i had to do was.
From this
To
and it works, hope it helps someone 🙂