Merge more Model form in 1 form + repeatable input

This is my database schema:



CREATE TABLE `Dettaglio` (


  `id` int(11) NOT NULL AUTO_INCREMENT,


  `lastminuteId` int(11) NOT NULL,


  PRIMARY KEY (`id`),


  KEY `lastminuteId` (`lastminuteId`)


) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;





CREATE TABLE `DettaglioTesto` (


  `id` int(11) NOT NULL AUTO_INCREMENT,


  `dettaglioId` int(11) NOT NULL,


  `codiceLingua` varchar(2) NOT NULL,


  `titolo` varchar(128) NOT NULL,


  `descrizione` text NOT NULL,


  PRIMARY KEY (`id`,`dettaglioId`,`codiceLingua`),


  KEY `dettaglioId` (`dettaglioId`)


) ENGINE=InnoDB DEFAULT CHARSET=utf8;





CREATE TABLE `Lastminute` (


  `id` int(11) NOT NULL AUTO_INCREMENT,


  `prezzo` double(6,2) NOT NULL,


  `dataScadenza` date NOT NULL,


  `immagine` text NOT NULL,


  PRIMARY KEY (`id`)


) ENGINE=InnoDB  DEFAULT CHARSET=utf8;





CREATE TABLE `Periodo` (


  `id` int(11) NOT NULL AUTO_INCREMENT,


  `dataInizio` date NOT NULL,


  `dataFine` date NOT NULL,


  `lastminuteId` int(11) NOT NULL,


  PRIMARY KEY (`id`),


  KEY `lastminuteId` (`lastminuteId`)


) ENGINE=InnoDB  DEFAULT CHARSET=utf8;





ALTER TABLE `Dettaglio`


  ADD CONSTRAINT `Dettaglio_ibfk_1` FOREIGN KEY (`lastminuteId`) REFERENCES `Lastminute` (`id`) ON DELETE CASCADE;





ALTER TABLE `DettaglioTesto`


  ADD CONSTRAINT `DettaglioTesto_ibfk_1` FOREIGN KEY (`dettaglioId`) REFERENCES `Dettaglio` (`id`) ON DELETE CASCADE;





ALTER TABLE `Periodo`


  ADD CONSTRAINT `Periodo_ibfk_1` FOREIGN KEY (`lastminuteId`) REFERENCES `Lastminute` (`id`) ON DELETE CASCADE;


I've created a model and a crud for all table. First of all i create a Lastminute, than, when i'm showing a Lastminute istance I want to allow the user to add more than a Periodo ( Period ) and more than a Descrizione ( Description ).

How can i do that?

Posted Image

Sorry for the crappy image but I’m not a designer :P

I need to know how to merge more Model inputs fields in one form, how to can make them repeatable, and how can I handle the save and store back-end.

Thank you very much :)

I know that maybe I’ve explained a bit bad the scenario but I’m not so good in english :)

Please check the following:

http://www.yiiframew…oc/cookbook/19/

http://www.yiiframew…uide/form.table

Ok this morning I will try to do it :)

I will give some feedback :)

This is my scenario. I don’t know how many Periods the user will create in the form ( couse he/she can add dinamically ) so how can i retrive the items from the form? ( I’m not a Yii master at the moment :( ).

I see that in the cookbook the user use this function: $items=$this->getItemsToUpdate();

I need something like this ( getItemsToUpdate() is a standard function? ) to get all Period items from the form and create the respective object.

I'm in the same boat as you. I have one questions too:

  1. Will yii pass the lastminute id to the periods and description or i need to post lastminute, get the id and pass to the periods and description by hand ?

I've done like this Khaoz:

LastminuteController:



public function actionCustomize()


	{


		


		$lastminute = $this->loadLastminute();


		


		


		/* 


		 * This is the main problem, I want to allow the user to add dinamically in client-side way 


		 * with javascript  more Periods and than ( in server-side ) collet that Periods, validate and save them.


		 */


		


		$periods = array (


			new Periodo,


			new Periodo,


			


		);


				


		if ( isset( $_POST['submitDatas'] ) && isset( $_POST['Periodo'] )  ) {


			


			$valid = true;


			


			foreach ( $periods as $i => $period ) {


				if ( isset( $_POST['Periodo'][$i] ) ) {


					$period->attributes = $_POST['Periodo'][$i];


				}				


				$valid = $valid && $period->validate();


					


			}


			if ( $valid )


				exit(valid);


		}	    


	    


	    $this->render('customize', array(


			'lastminute' => $lastminute,


			'periods' => $periods,


	    	'update' => false,


		));


	    


	}


customize:

<h2>View Lastminute <?php echo $lastminute->id; ?></h2>





<div class="actionBar">


[<?php echo CHtml::link('Lastminute List',array('list')); ?>]


[<?php echo CHtml::link('New Lastminute',array('create')); ?>]


[<?php echo CHtml::link('Update Lastminute',array('update','id'=>$lastminute->id)); ?>]


[<?php echo CHtml::linkButton('Delete Lastminute',array('submit'=>array('delete','id'=>$lastminute->id),'confirm'=>'Are you sure?')); ?>


]


[<?php echo CHtml::link('Manage Lastminute',array('admin')); ?>]


</div>





<table class="dataGrid">


<tr>


	<th class="label"><?php echo CHtml::encode($lastminute->getAttributeLabel('prezzo')); ?>


</th>


    <td><?php echo CHtml::encode($lastminute->prezzo); ?>


</td>


</tr>


<tr>


	<th class="label"><?php echo CHtml::encode($lastminute->getAttributeLabel('dataScadenza')); ?>


</th>


    <td><?php echo CHtml::encode($lastminute->dataScadenza); ?>


</td>


</tr>


<tr>


	<th class="label"><?php echo CHtml::encode($lastminute->getAttributeLabel('immagine')); ?>


</th>


    <td><?php echo CHtml::image('img/'.$lastminute->immagine); ?>


</td>


</tr>


</table>





<div class="yiiForm">





<p>


Fields with <span class="required">*</span> are required.


</p>





<?php echo CHtml::form(); ?>





<?php 


/*


 * This is test and temp solution. I want to allow the user to add more Periods using javascript.


 * Is there some way to do it with Yii? Like saying that this model input is repeatable?


 * 


 */


?>


<?php foreach($periods as $i => $period): ?>


<div class="periodo">


	


	<?php echo CHtml::errorSummary($period); ?>


	<div class="simple">


	<?php echo CHtml::activeLabelEx($period,'dataInizio'); ?>


	<?php echo CHtml::activeTextField($period,"dataInizio[$i]"); ?>


	</div>


	<div class="simple">


	<?php echo CHtml::activeLabelEx($period,'dataFine'); ?>


	<?php echo CHtml::activeTextField($period,"dataFine[$i]"); ?>


	</div>


	<br />


</div>


<?php endforeach; ?>





<div class="row action">


<?php echo CHtml::submitButton($update ? 'Save' : 'Submit', array('name'=>'submitDatas')); ?>





</div>





</div><!-- yiiForm -->

The problem is this: Does Yii offer some standart tool to reapeat inputs ( client side and not server side ) like I explained and than collect that input ( server side ) to validate and save? Is there some way to do it with Yii? Like saying that this model input is repeatable in that form?

up

I was thinking about your problem. What about to create a function to be called by an ajax request which will expand your array of periods and add a new line to your tabular form.

Hey Stermi, take a look here:

http://www.yiiframew…40.html#msg5240

My question still remains:

Based on the link, if my relation is a Client HAS_MANY Address, and use a tabular form to collect those addresses, i need to do a Client->save() and a foreach to fill all Addresses with the client id ?