Data saving in MM table

This is my my form code




<div class="form">


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

	'id'=>'materiel-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Les champs avec <span class="required">*</span> réquis.</p>


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

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

    

 	<div class="row">

		

        <?php $model2->lformation_id=@$_GET['lformation_id']; ?>

        <?php echo $form->textField($model2,'lformation_id'); ?>

	 	<?php echo $form->error($model2,'lformation_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx(Materielformation::Model(),'nom_materiel'); ?>

		<?php echo $form->DropDownList($model1,'nom_materiel',CHtml::listData(Materiel::model()->findAll(), 'id', 'nom_materiel'),array('empty'=>'choisir dans la liste')); ?>

		<?php echo $form->error($model1,'nom_materiel'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model2,'Quantit&eacute;'); ?>

		<?php echo $form->textField($model2,'quantite',array('size'=>5,'maxlength'=>10)); ?>

		<?php echo $form->error($model2,'quantite'); ?>

	</div>

    

    	


<?php echo CHtml::submitButton('Ajouter materiel',array('name'=>'addMateriels')); ?>


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


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




When I clic on Submit button nothing is saved in my materielformation table .

My addmateriels cod is:




public function addMateriels($id) 


	{

	  $materiel=new Materielformation;

	  $materiel->materiel_id=$this->id;

	  $materiel->lformation_id=$id;

	  $materiel->quantite=$this->quantite;

	  

	  return $materiel->save();

	}



How to make this function work?

$materiel->save() will make the validations… and if any validation fails it will not save the data…

try first with $model->save(false)… this way you will skip validations… if data gets saved… than you need to find why and where validation fails…

I have $model1 and $model2

In your addmateriels code you posted above… you are working only with one model!

Ok

so I changed my function this ways but no saving




	public function addMateriels($id) 


	{

	  $materiel=new Materielformation;

	  $materiel->materiel_id=$this->id;

	  $materiel->lformation_id=$id;

	  $materiel->quantite=$this->quantite;

	  

	  return $materiel->save(false);

	}




Do you get any error… have you checked if database NOT NULL fields gets a value… for example if $this->id, $id and $this->quantite has a value…

Hello!

I’ve tryed to do it another way.

My materielformation table is:




materielformation (lformation_id,materiel_id,quantite)



in the materielfrmation Cotroller I have




public function actionCreate2()

	{

		$model=new Materielformation;


		


		if(isset($_POST['Materielformation']))

		{

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

			

			if($model->save())

				$this->redirect(array('lformation/view','id'=>$model->lformation_id));

		}


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

			'model'=>$model,

		));

	}



A my form code is:




<div class="form">


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

	'id'=>'materiel-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Les champs avec <span class="required">*</span> réquis.</p>


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

    

 	<div class="row">

		

        <?php $model->lformation_id=@$_GET['lformation_id']; ?>

        <?php echo $form->textField($model,'lformation_id'); ?>

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

	</div>


	<div class="row">

		<?php echo $form->labelEx(Materielformation::Model(),'materiel_id'); ?>

		<?php echo $form->DropDownList($model,'materiel_id',CHtml::listData(Materiel::model()->findAll(), 'id', 'nom_materiel'),array('empty'=>'choisir dans la liste')); ?>

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

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'Quantit&eacute;'); ?>

		<?php echo $form->textField($model,'quantite',array('size'=>5,'maxlength'=>10)); ?>

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

	</div>

    

    	




<div class="row buttons">

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

</div>

	




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


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



And it’s working now…

thx!