[EXTENSION] multimodelform/jqrelcopy

Hi Joblo,

Thanks for your valuable inputs.

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?

Can you please provide me a workaround for this?

Thanks for your support,

A.Miguel

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 :wink:

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.

Hello Joblo!

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".

Thanks in advance

I didn’t test it, but should be possible.

Take a look at the documentation of the integrated jqrelcopy jQuery plugin

There exists the property ‘excludeSelector’ and you can set the jqrelcopy-properties through the ‘options’ property of the multimodelform widget.




 $this->widget('ext.multimodelform.MultiModelForm',array(

       ...

        'options'=>array('excludeSelector'=>'YourExcludeSelector'),

       ...

    ));




But maybe this will have sideeffects on building the items-arrays on validate/save… and needs more workaround.

Please correct me if I’m doing something wrong.

Here is my code:




$techFormConfig = array(

      'elements'=>array(

        'tech_channel_id'=>array(

            'type'=>'text',

            'maxlength'=>50,

            'rel'=>'tech_channel_id',

        ),

   ....

   ....

);


$this->widget('ext.multimodelform.MultiModelForm',array(

        'id' => 'id_tech', //the unique widget id

        'formConfig' => $techFormConfig, //the form configuration array

        'model' => $tech, //instance of the form model

        'options' => array('excludeSelector' => 'tech_channel_id'),

    ...

    ...

    ));



And these lines are not working…

When i click “add item” the whole form is added, ignoring the ‘excludeSelector’

The excludeSelector is a jQuery selector, that means:

  • use ‘#tech_channel_id’ if its a element with id=“tech_channel_id”

  • use ‘.tech_channel_id’ if its a element with class=“tech_channel_id”

your code from above will exclude all tags: <tech_channel_id>…</tech_channel_id>

Hi Joblo, excellent work, congratulations!

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 :rolleyes:

hi joblo…

I use your extension multimodelform

and success …

I want to change the appearance of:

firstName: inputField

lasName: inputField

FirstName: inputField

LastName: inputField

Remove

be like this

FirstName-------LastName-------action

inputField--------InputField--------

inputField--------InputField--------Remove

where do I need to change … I’ve read MultiModelForm.php but still do not understand … :huh:

thanks in advance for the extension… :)

At IRCSASW:

Because you have to use the Yii formbuilder you have to work with the features of the CForm and the CFormInputElement.

I didn’t test this, but you should be able to specify the layout property and add additional tags.

Maybe you can wrap elements into a div tag with style: float left.





$formConfig = array(

      'elements'=>array(

        

        <div style="float:left">, 


          'elem1'=>array(

            'type'=>'text',

            'maxlength'=>2,

            'layout'=>'<div ...> ...{label} {input}...</div>',

          ),

        .... more elements


        '</div>',

);




So you have to play around with additional div tags and css.

At Mr D:

You can use the ‘tableView’ feature:




$this->widget('ext.multimodelform.MultiModelForm',array(

       ...

        'tableView' => true,

     //'tableFootCells' => array('footerCol1','footerCol2'...), //optional table footer

       ...

    ));



If this is not enough you have to play around with the features of CForm - see my answer to IRCSASW above.

that’s what I want … thank you very much :D :D

how to rename the header name??

2262

untitled.PNG

I hope autocomplete can be used immediately :P

You can set the property ‘label’ of the elements in the formConfig to change the header.

autocomplete - sorry :frowning:

See my note at the extensions page:

Note: I couldn’t get Autocomplete working. Maybe one of you has an idea (see afterNewIdAutoComplete in MultiModelForm.php).

Can you show me how you did so that the fields shown in this way?, as the image samples.

Tks.

$this->widget(‘ext.multimodelform.MultiModelForm’,array(

  [b]  'tableView' =&gt; true,////just add this[/b]


	


	'id' =&gt; 'id_member', //the unique widget id


	'formConfig' =&gt; &#036;memberFormConfig, //the form configuration array


	'model' =&gt; &#036;cbcparam, //instance of the form model





	//if submitted not empty from the controller,


	//the form will be rendered with validation errors


	'validatedItems' =&gt; &#036;validatedMembers,





	//array of member instances loaded from db


	'data' =&gt; &#036;cbcparam-&gt;findAll('id_cbc=:id_cbc', array(':id_cbc'=&gt;&#036;model-&gt;id)),


));

?>

Ok, thank you, that I have it, I confused what I want is that the child form is seen in blocks, each item.

keep trying with the proposal of joblo that it does not work. >:(

Thanks Jobo,

…ignores everything that I put as text in the array of elements:




$rfqitemFormConfig = array(

  'elements'=>array(

  '<div class="width-100 fltlft"><div class="width-50 fltlft">',

  

  'estado'=>array(

    'type'=>'dropdownlist',

    'items'=>Rfqitems::model()->getEstadosPartida(),

    'layout'=>'<div class="width-50 fltlft">{label}{input}{hint}{error}</div>',

  ...	        	),



result:




...

<div class="row">

  <label for="RfqItems_estado">Estado</label>

  <div class="width-50 fltlft">

    <select id="RfqItems_estado" name="RfqItems[estado][]">

  </div>

</div>

...



and what I put like ‘layout’ of each element that has no effect, automatically puts a:




<div class="row"> 

  ... 

</div>



addition to the label element is not within the format of the ‘layout’

any idea?

hi … i tried to make the table

tbl_cbc

-id

-name

tbl_cbc_param

-id_cbc

-time

actionUpdate





	  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

What did I miss?

My problem is that my table has no pk

and how to handle it other than adding a PK at my table??

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';

	}




how to change the position of "Add Item" to be next to "Remove" in the last row

like this pic

2271

ask.JPG

I tried to read the code from the MultiMoldeForm.php, but because my knowledge is still lacking so I do not understand at all :(