i have got 2 models: rooms , roomattributes. there many-many relation between them:
$this->hasmanytomany( "id", "roomattributes", "roomid", "attributesid", "roomattributesrelation", "id", array('alias' => 'attributes') ); now i'm creating form add new room , want have list of attributes checkboxes. best way , how should save room model after?
maybe this:
use phalcon\forms\element\select; class roomform extends \phalcon\forms\form { $attr_arr = ['attr1_id' => 'attr1_name', 'n_id' => 'n_name']; // or $attr_arr= array_column(roomattributes::find()->toarray(),'id','name') $attributes = new select( 'attributes[]', $attr_arr , ['multiple' => 'multiple' ]); $this->add($attributes); } in controller
**** if($new_room->save()){ $attributes = $_post['attributes']; foreach ($attributes $id){ $new_attribute = new roomattributes(); $new_attribute->roomid = $new_room->id; $new_attribute->attributesid = $id; $new_attribute->save(); } }
Comments
Post a Comment