Manage Related Models

Hi all.

I am a beginner with Yii Framework. I want to create a view for a related data model in my project.

I need to create a user, then add him to a group, on another page. But, as group is related to contact, I am getting some exceptions.

My contact create form is the following :




<div class="form">




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

	'id' => 'contact-form',

	'enableAjaxValidation' => false,

));

?>


	<p class="note">

		<?php echo Yii::t('app', 'Fields with'); ?> <span class="required">*</span> <?php echo Yii::t('app', 'are required'); ?>.

	</p>


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


		<div class="row">

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

		<?php echo $form->textField($model, 'nom_contact', array('maxlength' => 30)); ?>

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

		</div><!-- row -->

		<div class="row">

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

		<?php echo $form->textField($model, 'prenom_contact', array('maxlength' => 30)); ?>

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

		</div><!-- row -->

		<div class="row">

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

		<?php echo $form->textField($model, 'sexe', array('maxlength' => 1)); ?>

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

		</div><!-- row -->

		<div class="row">

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

		<?php $form->widget('zii.widgets.jui.CJuiDatePicker', array(

			'model' => $model,

			'attribute' => 'date_naissance',

			'value' => $model->date_naissance,

			'options' => array(

				'showButtonPanel' => true,

				'changeYear' => true,

				'dateFormat' => 'yy-mm-dd',

				),

			));

; ?>

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

		</div><!-- row -->

		<div class="row">

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

		<?php echo $form->textField($model, 'mail', array('maxlength' => 50)); ?>

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

		</div><!-- row -->

		<div class="row">

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

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

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

		</div><!-- row -->

		<div class="row">

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

		<?php echo $form->textField($model, 'nationalite', array('maxlength' => 50)); ?>

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

		</div><!-- row -->

		<div class="row">

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

		<?php echo $form->textField($model, 'ville', array('maxlength' => 50)); ?>

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

		</div><!-- row -->

		<div class="row">

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

		<?php echo $form->textField($model, 'pays', array('maxlength' => 50)); ?>

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

		</div><!-- row -->

		<div class="row">

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

		<?php echo $form->textField($model, 'societe', array('maxlength' => 50)); ?>

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

		</div><!-- row -->

		<div class="row">

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

		<?php echo $form->textField($model, 'details', array('maxlength' => 50)); ?>

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

		</div><!-- row -->

<?php

echo GxHtml::submitButton(Yii::t('app', 'Save'));

$this->endWidget();

?>

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

</code]

Then I create the form where to chose the group to which the contact belongs :

[code]

<?php


/* 

 * To change this license header, choose License Headers in Project Properties.

 template file, cTools | Templates

 * and open the template in the editor.

 */


?>

<div class="form">

    <?php 

        $form = $this->beginWidget('GxActiveForm', array(

            'id' => 'groupeAdd-form',

            'enableAjaxValidation' => true,

            ));

    ?>

    

    <p class="note">

		<?php /* echo Yii::t('app', 'Fields with'); ?> <span class="required">*</span> <?php echo Yii::t('app', 'are required'); */?>

    </p>

    

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

    

    <div class="row">

        <?php echo $form->textField($model, 'id_groupe', array('hidden' => true, 'value' => $_GET['id'])); ?>

    </div>

        

    <div class="row">   

        <label><?php 

                echo "Sélectionner les contacts à ajouter"; 

                 //echo GxHtml::encode($model->getRelationLabel('id_contact')); 

                 ?>

        </label>

        <?php echo $form->checkBoxList($model, 'id_contact', GxHtml::encodeEx(GxHtml::listDataEx(Contact::model()->findAllAttributes(null, true)), true, true)); ?>

    </div>

    <?php echo GxHtml::submitButton(Yii::t('app', 'Ajouter')); ?>

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

</div>



But, unfortunately, when sending the form, I get :




Please fix the following input errors:

Id Contact must be a number.



Can anybody help ?

Am I wrong or are you expecting Id Contact to be a single number but allows user to select more than one checkbox? It looks like you are getting array of choices.

In fact, I want to save many rows in the table named


Etre_membre

with


(id_groupe, id_contact)

as primary key. For the same


id_groupe

, I need many rows.

Can anybody suggest me a way to store such data ?

So there is one contact that can belong to many groups, right?

If so, in your controller, after saving contact in DB, you send the contact ID to the next form. When reciving data from the second form you can make the loop like that:


foreach ($groups as $group) {

    $em = new Etre_membre;

    $em->id_groupe = $group;

    $em->id_contact = $contact_id;

    $em->save();

}

Of course you need to set up everything properly.

Thanks, it’s what I need. Thanks for your help.

Hello sk001,

Give +1 to Bizley as your accepted answer.