complicated form

Hello,

I have to create a form that integrates 4 tables: Stocks, Inventorio and Inventario_Stocks.

1931

mante_sql.png

The fourth table is Ubicaciones, which is formatted as a tree, and from Stocks are pointing to one of these Ubicaciones.

The idea is that you select the Ubicaciones information is displayed in a tree or via dropdown. Whether to create a new entry (the Ubicacion must be previously created) and to modify it.

My View _form4.php


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'inventario-form2',

	'enableAjaxValidation'=>true,

)); ?>

<?php Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');?>

	<p class="note">Los campos con <span class="required">*</span> son obligatorios.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Crear' : 'Save'); ?>

	</div>


	<div id="mtreeview" style="width: 250px;border: 1px solid GRAY;float:right">

		<?php echo $form->labelEx($stock, 'Ubicacion'); ?>

		<?php

		$this->widget('application.extensions.MTreeView.MTreeView',array(

			'collapsed'=>true,

			'animated'=>'fast',

			//---MTreeView options from here

			'table'=>'Ubicaciones',//what table the menu would come from

			'hierModel'=>'adjacency',//hierarchy model of the table

			//'conditions'=>array('idUbicaciones=:actual',array(':actual'=>$ubi->idUbicaciones)),//other conditions if any                                    

			'fields'=>array(//declaration of fields

				'id'=>'idUbicaciones',

				'text'=>'ubicacion',//no `text` column, use `title` instead

				'alt'=>'ubicacion',//skip using `alt` column

				'id_parent'=>'padre',//no `id_parent` column,use `parent_id` instead

				'position'=>'padre',

				'task'=>false,

				'icon'=>false,

				'tooltip'=>false,

				'url'=>false,

				//'options'=>'options',

				//'url'=>array('/ubicaciones/view',array('id'=>'idUbicaciones'))

			),

			//'template'=>'{icon}&nbsp;{text}',

			'ajaxOptions'=>array('update'=>'#Ubicaciones_idUbicaciones')

		));

		?>

	</div>	


	<div class="row">

		<?php echo $form->labelEx($model,'pieza'); ?>

		<?php echo $form->textField($model,'pieza',array('size'=>45,'maxlength'=>45)); ?>

		<?php echo $form->error($model,'pieza'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'Tipo_idTipoPieza'); ?>

		<?php echo $form->dropDownList($model,'Tipo_idTipoPieza',CHtml::listdata(TipoPieza::model()->findAll(),'idTipoPieza','tipopieza')); ?>

		<?php echo $form->error($model,'Tipo_idTipoPieza'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock, 'Ubicaciones_idUbicaciones'); ?>

		<?php echo CHtml::encode($stock->Ubicaciones_idUbicaciones); ?>

		<?php echo $form->error($stock, 'Ubicaciones_idUbicaciones'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Cantidad'); ?>

		<?php echo $form->textField($stock, 'cantidad', array('size'=>45, 'maxlength'=>45)); ?>

		<?php echo $form->error($stock, 'cantidad'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Creado por'); ?>

		<?php echo $form->dropDownList($stock,'tecnico_crear',CHtml::listdata(Tecnicos::model()->findAll(),'idTecnicos','nombre')); ?>

		<?php echo $form->error($stock, 'tecnico_crear'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Creado el'); ?>

		<?php $form->widget('CJuiDateTimePicker',array('model'=>$stock,'attribute'=>'dia_creacion','mode'=>'date','options'=>array()));?>

		<?php echo $form->error($stock, 'dia_creacion'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Modificado por'); ?>

		<?php echo $form->dropDownList($stock,'tecnico_modif',CHtml::listdata(Tecnicos::model()->findAll(),'idTecnicos','nombre')); ?>

		<?php echo $form->error($stock, 'tecnico_modif'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Modificado el'); ?>

		<?php $form->widget('CJuiDateTimePicker',array('model'=>$stock,'attribute'=>'dia_modif','mode'=>'date','options'=>array()));?>

		<?php echo $form->error($stock, 'dia_modif'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Uso'); ?>

		<?php echo $form->dropDownList($stock,'Usos_idUsos',CHtml::listdata(Usos::model()->findAll(),'idUsos','uso')); ?>

		<?php echo $form->error($stock, 'Usos_idUsos'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Vendedor'); ?>

		<?php echo $form->dropDownList($stock,'Vendedores_idVendedor',CHtml::listdata(Vendedores::model()->findAll(),'idVendedor','Proveedores_idProveedores')); ?>

		<?php echo $form->error($stock, 'Vendedores_idVendedor'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->

InventarioController




	public function actionActualizaPieza($id)

	{

		$model=$this->loadModel($id);

		$I_S=InventarioStocks::model()->findByAttributes(array('Inventario_idInventario'=>$id));

		$stock=Stocks::model()->findByPK($I_S->Stocks_idStocks);


		// Uncomment the following line if AJAX validation is needed

		$this->performAjaxValidation2($model,$I_S, $stock);


		if(isset($_POST['Inventario'], $_POST['InventarioStocks'], $_POST['Stocks']))

		{

			$model->attributes=$_POST['Inventario'];

			$I_S->attributes=$_POST['InventarioStocks'];

			$stock->attributes=$_POST['Stocks'];

			if($model->save() && $I_S->save() && $stock->save())

				$this->redirect(array('verpieza','id'=>$model->idInventario));

		}


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

			'model'=>$model,

			'I_S'=>$I_S,

			'stock'=>$stock,

		));

	}



Right now the problem is that it can be selected in the tree, at least MTreeView.

If I do it with linked DropDownList:

_form2.php




<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'inventario-form2',

	'enableAjaxValidation'=>true,

)); ?>

<?php Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');?>

	<p class="note">Los campos con <span class="required">*</span> son obligatorios.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'pieza'); ?>

		<?php echo $form->textField($model,'pieza',array('size'=>45,'maxlength'=>45)); ?>

		<?php echo $form->error($model,'pieza'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'Tipo_idTipoPieza'); ?>

		<?php echo $form->dropDownList($model,'Tipo_idTipoPieza',CHtml::listdata(TipoPieza::model()->findAll(),'idTipoPieza','tipopieza')); ?>

		<?php echo $form->error($model,'Tipo_idTipoPieza'); ?>

	</div>


	<div class="row">

		<?php echo CHtml::activeLabelEx($ret,'Ubicacion0'); ?>

		<?php

			$modelo=Ubicaciones::model()->findAllByAttributes(array('padre'=>0));

			$listado= CHtml::listData($modelo, 'idUbicaciones','ubicacion');

			echo CHtml::dropDownList('retata', 'idUbicaciones',$listado, array(

				'prompt' => 'Selecciona...',

				'ajax' => array(

					'type'=>'POST',

					'url'=>CController::createUrl('inventario/encontrarubicacion'),

					'update'=> '#tatara',

					'data'=>'js:jQuery(this).serialize()'

				)

			));

			echo '<br />';

			echo CHtml::dropDownList('tatara', 'idUbicaciones',array($tat->ubicacion), array(

				'id'=>'tatara',

				'ajax' => array(

					'type'=>'POST',

					'url'=>CController::createUrl('inventario/encontrarubicacion'),

					'update'=> '#bisabuelo',

					'data'=>'js:jQuery(this).serialize()'

				)

			));

			echo CHtml::dropDownList('bisabuelo', 'idUbicaciones',array($bis->ubicacion), array(

				'id'=>'bisabuelo',

				'ajax' => array(

					'type'=>'POST',

					'url'=>CController::createUrl('inventario/encontrarubicacion'),

					'update'=> '#abuelo',

					'data'=>'js:jQuery(this).serialize()'

				)

			));


			echo CHtml::dropDownList('abuelo', 'idUbicaciones',array($abu->ubicacion), array(

				'id'=>'abuelo',

				'value'=>$abu->ubicacion,

				'ajax' => array(

					'type'=>'POST',

					'url'=>CController::createUrl('inventario/encontrarubicacion'),

					'update'=> '#padre',

					'data'=>'js:jQuery(this).serialize()'

				)

			));


			echo CHtml::dropDownList('padre', 'idUbicaciones',array($pad->ubicacion), array(

				'id'=>'padre',

				'ajax' => array(

					'type'=>'POST',

					'url'=>CController::createUrl('inventario/encontrarubicacion'),

					'update'=> '#hijo',

					'data'=>'js:jQuery(this).serialize()'

				)

			));


			echo $form->DropDownList($stock, 'Ubicaciones_idUbicaciones', array($ubi->ubicacion), array(

				'id'=>'hijo',

			));

		?>


	<div class="row">

		<?php echo $form->labelEx($stock,'Cantidad'); ?>

		<?php echo $form->textField($stock, 'cantidad', array('size'=>45, 'maxlength'=>45)); ?>

		<?php echo $form->error($stock, 'cantidad'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Creado por'); ?>

		<?php echo $form->dropDownList($stock,'tecnico_crear',CHtml::listdata(Tecnicos::model()->findAll(),'idTecnicos','nombre')); ?>

		<?php echo $form->error($stock, 'tecnico_crear'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Creado el'); ?>

		<?php $form->widget('CJuiDateTimePicker',array('model'=>$stock,'attribute'=>'dia_creacion','mode'=>'date','options'=>array()));?>

		<?php echo $form->error($stock, 'dia_creacion'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Modificado por'); ?>

		<?php echo $form->dropDownList($stock,'tecnico_modif',CHtml::listdata(Tecnicos::model()->findAll(),'idTecnicos','nombre')); ?>

		<?php echo $form->error($stock, 'tecnico_modif'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Modificado el'); ?>

		<?php $form->widget('CJuiDateTimePicker',array('model'=>$stock,'attribute'=>'dia_modif','mode'=>'date','options'=>array()));?>

		<?php echo $form->error($stock, 'dia_modif'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Uso'); ?>

		<?php echo $form->dropDownList($stock,'Usos_idUsos',CHtml::listdata(Usos::model()->findAll(),'idUsos','uso')); ?>

		<?php echo $form->error($stock, 'Usos_idUsos'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($stock,'Vendedor'); ?>

		<?php echo $form->dropDownList($stock,'Vendedores_idVendedor',CHtml::listdata(Vendedores::model()->findAll(),'idVendedor','Proveedores_idProveedores')); ?>

		<?php echo $form->error($stock, 'Vendedores_idVendedor'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Crear' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->



InventarioController.php




	public function actionActualizaPieza($id)

	{

		$model=$this->loadModel($id);

		$I_S=InventarioStocks::model()->findByAttributes(array('Inventario_idInventario'=>$id));

		$stock=Stocks::model()->findByPK($I_S->Stocks_idStocks);

		$ubi=Ubicaciones::model()->findByPK($stock->Ubicaciones_idUbicaciones);

		$pad=Ubicaciones::model()->findByPK($ubi->padre);

		$abu=Ubicaciones::model()->findByPK($pad->padre);

		$bis=Ubicaciones::model()->findByPK($abu->padre);

		$tat=Ubicaciones::model()->findByPK($bis->padre);

		$ret=Ubicaciones::model()->findByPK($tat->padre);


		// Uncomment the following line if AJAX validation is needed

		$this->performAjaxValidation2($model,$I_S, $stock);


		if(isset($_POST['Inventario'], $_POST['InventarioStocks'], $_POST['Stocks']))

		{

			$model->attributes=$_POST['Inventario'];

			$I_S->attributes=$_POST['InventarioStocks'];

			$stock->attributes=$_POST['Stocks'];

			if($model->save() && $I_S->save() && $stock->save())

				$this->redirect(array('verpieza','id'=>$model->idInventario));

		}


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

			'model'=>$model,

			'I_S'=>$I_S,

			'stock'=>$stock,

			'ubi'=>$ubi,

			'pad'=>$pad,

			'abu'=>$abu,

			'bis'=>$bis,

			'tat'=>$tat,

			'ret'=>$ret,

		));

	}



This way the DropDownList work, but it depends on what level you have, because if the tree levels are lower does not work, would also have to update the information downstream, I don’t know how to do.

Sorry to post so long and my poor English

nobody knows how to use MTrewView in a form? or other TreeView?