cakephp - Working with hasMany and Multi-select -
scenario
- i have
provider
,package
. - a provider can have number of featured packages.
- so need habtm between
provider
,package
- i want save providers featured packages in 1 shot using
provider::edit()
method
setup
have 3 models.
provider
featuredpackage
package
models
these setup using 'habtm through', should not confused habtm. relationships follows.
provider
hasmany featuredpackage
featuredpackage
belongsto provider
package
hasmany featuredpackage
featuredpackage
belongsto package
controller
public function admin_edit($id) { if ($this->request->is('post') || $this->request->is('put')) { if ($this->provider->saveall($this->request->data)) { // snip
view
echo $this->form->input('featuredpackages', array('type' => 'select', 'multiple' => true, 'options' => $packages));
the issue
i'm not sure how save multiple variable numbers of hasmany
records 1 side of relationship. know reading book expected data array should numerically indexed beneath model.
however i'm not sure name field data formatted correctly.
i unable bake view seems ignore hasmany
, not add field it.
field names i've tried
featuredpackage
featuredpackage.featuredpackage
featuredpackage.package_id
featuredpackage..package_id
featuredpackage.[].package_id
featuredpackage.package_id][
i've solved using data massage method in model, called controller.
this goes through , updates array submitted form match expected save.
i blogged solution, http://jedistirfry.co.uk/blog/2013-08/how-to-create-hasmanythrough-multi-selects/
Comments
Post a Comment