Your suggestion works fine for actionCreate. But for actionUpdate MultiModelForm code would require an update…
Since my tbl_order_products primary key is a composite key (order_id, product_id) MultiModelForm code fails when extracting the primary key from this table:
PHP Error
Illegal offset type
C:\...\extensions\multimodelform\MultiModelForm.php(523)
$pkValue = $model->primaryKey;
if (!empty($pkValue))
{
$pkName = $model->primaryKey();
if (empty($pkName))
$pkName = $model->tableSchema->primaryKey;
-> $result = array($pkName => $pkValue);
}
Is this a bug or your extension is not supposed to work with composite keys?
I know that the multimodelform (at the moment) only supports a single primary key.
This is no bug.
I have implemented this, because Yii’s CRUD doesn’t support a composite PK out of the box (loadByPK).
But you are right, maybe a code modification in the method getPrimaryKey() is enough to add the support for a composit PK because CActiveRecord supports this.
Can be something like this - but not tested.
public function getPrimaryKey($model)
{
$result = array();
if ($model instanceof CActiveRecord)
{
$pkName = $model->primaryKey();
if (!empty($pkName))
{
if (is_array($pkName))
{
foreach ($pkName as $pk)
if (isset($model->$pk))
$result[] = array($pk => $model->$pk);
}
else
{
$pkValue = $model->primaryKey;
$pkName = $model->tableSchema->primaryKey;
if (isset($pkValue))
$result = array($pkName => $pkValue);
}
}
}
else // when working with EMongoDocument
if (method_exists($model, 'primaryKey'))
{
$pkName = $model->primaryKey();
$pkValue = $model->$pkName;
if (empty($pkValue))
$result = array($pkName => $pkValue);
}
return $result;
}
But you have to test:
line 744: renderHiddenPk - will work for composit PK
line 286: $item->save() - should work too with composit PK
line 291: needs testing
if (!empty($deleteItems))
foreach($deleteItems as $pk)
Sorry, I have no time in the moment to build a test enviroment for this, but your code seems to be one
So, if you do the modification(s) and send me your working/tested new code of the multimodelform, I will add this to the next release.
Thank you for plugin! Wish you having time and strength to continue supporting this plugin!
Also I have one question: is it possible to clone several model input fields, but not all of them?
For example, if I have 10 input fields, and 7 of them are "static" (text will be entered only once) and another 3 will be copied by pressing "add item".
One question, how I can do to change the form view related record, I have many fields and I want them for example in groups of three or more in vertical?, something like what is shown in the picture. is it possible?
thanks for the help and sorry for my google-English
[b] 'tableView' => true,////just add this[/b]
'id' => 'id_member', //the unique widget id
'formConfig' => $memberFormConfig, //the form configuration array
'model' => $cbcparam, //instance of the form model
//if submitted not empty from the controller,
//the form will be rendered with validation errors
'validatedItems' => $validatedMembers,
//array of member instances loaded from db
'data' => $cbcparam->findAll('id_cbc=:id_cbc', array(':id_cbc'=>$model->id)),
));
Yii::import('ext.multimodelform.MultiModelForm');
$model=$this->loadModel($id); //the Group model
$cbcparam = new CbcParam;
$validatedMembers = array(); //ensure an empty array
if(isset($_POST['Cbc']))
{
$model->attributes=$_POST['Cbc'];
//the value for the foreign key 'groupid'
$masterValues = array ('id_cbc'=>$model->id);
if( //Save the master model after saving valid members
MultiModelForm::save($cbcparam,$validatedMembers,$deleteMembers,$masterValues) &&
$model->save()
)//what the contents of $deleteMembers
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
//submit the member and validatedItems to the widget in the edit form
'cbcparam'=>$cbcparam,
'validatedMembers' => $validatedMembers,
));
_form
$memberFormConfig = array(
'elements'=>array(
'param_type'=>array(
'type'=>'dropdownlist',
'items'=>array(''=>'-Pleace Choose One--',1=>'IP Address',2=>'Channel'),
),
'param_value'=>array(
'type'=>'text',
'maxlength'=>40,
),
));
$this->widget('ext.multimodelform.MultiModelForm',array(
'id' => 'id_member', //the unique widget id
'formConfig' => $memberFormConfig, //the form configuration array
'model' => $cbcparam, //instance of the form model
//if submitted not empty from the controller,
//the form will be rendered with validation errors
'validatedItems' => $validatedMembers,
//array of member instances loaded from db
'data' => $cbcparam->findAll('id_cbc=:id_cbc', array(':id_cbc'=>$model->id)),
));
when create success, but when the update and I did not add any data and I click save, the data is stored twice with the same …
but when i test with a sample that is in extension, run smoothly
There can’t be a correct update without a primary key, because multimodelform uses the standard CActiveRecord.save, means CActiveRecort.update.
But you can define a PK in your model by adding a method ‘primaryKey’. Yii will call this method to get the PK if there is no pk-definition in the table schema.
public function primaryKey()
{
return 'myPKFieldname';
}