Inserting Form Fields with Relations

First off I’m pretty new to the Yii Framework so I’m still getting a firm grasp on how all this works and using Models/ Controllers properly. Any help that can be provided for me would be very much appreciated.

What I am trying to accomplish is to create a form that has information to populate a campaign in addition to have a checkbox list of all the sites in the Sites table to be selected to be tied to that campaign. As it is now, if you want to run a campaign you have to also select what site that the campaign will be available on. I have setup three different tables to accomplish this. These listings are directly from the model with the title the Model name. I’ve also included the relations that are setup with each model.

Campaign:

    ad_campaignID,


advertiserID,


advertiser.advertisersCompany,


ad_campaignName,


ad_campaignDescription,


ad_campaignCreationDate,


ad_campaignStartDate,


ad_campaignEndDate,


ad_campaignImpressionsActual,


ad_campaignImpressionsTarget,


ad_campaignClicksActual,


ad_campaignClicksTarget,


ad_campaignURL,


ad_campaignArchive

public function relations()

	{

		return array(

			'advertiser' => array(self::BELONGS_TO, 'Advertisers', 'advertiserID'),

			'campaignGroupAds' => array(self::HAS_MANY, 'CampaignGroupAds', 'ad_campaignID'),

			'campaignSites' => array(self::HAS_MANY, 'CampaignSites', 'ad_campaignID'),

		);

	}

CampaignSites:

    ad_campaign_sitesID,


ad_campaignID,


sitesID,


ad_campaign_sitesCreationDate

public function relations()

	{

		return array(

			'sites' => array(self::BELONGS_TO, 'Sites', 'sitesID'),

			'adCampaign' => array(self::BELONGS_TO, 'Campaign', 'ad_campaignID'),

		);

	}

Sites:

    sitesID,


affiliatesID,


sitesName

public function relations()

	{

		return array(

			'adCampaignSites' => array(self::HAS_MANY, 'AdCampaignSites', 'sitesID'),

			'adCampaignStats' => array(self::HAS_MANY, 'AdCampaignStats', 'sitesID'),

		);

	}

I have already created a form that setups all the campaign information, I need to know add the site information. I would like to do a checkbox list so that It will display the sitesName with the sitesID as the value. In addition, it needs to add all those to the CampaignSites table so that can be accessed by other logic.

Below is the code for my form.


<div class="form">


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

	'id'=>'campaign-form',

	'enableAjaxValidation'=>false,

)); ?>


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


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


	<div class="row">

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

		<?php echo $form->dropDownList($model,'advertiserID', $data = CHtml::listData(Advertisers::model()->findAll(), 'advertisersID', 'advertisersCompany')); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($model,'ad_campaignName',array('size'=>60,'maxlength'=>255)); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textArea($model,'ad_campaignDescription',array('rows'=>6, 'cols'=>50)); ?>

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

	</div>


	<div class="row">

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

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

			    'name'=>'Campaign[ad_campaignStartDate]',

			    // additional javascript options for the date picker plugin

			    'options'=>array(

			        'showAnim'=>'fold',

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

			    ),

				'value'=>$model->ad_campaignStartDate,

			)); ?>

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

	</div>


	<div class="row">

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

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

			    'name'=>'Campaign[ad_campaignEndDate]',

			    // additional javascript options for the date picker plugin

			    'options'=>array(

			        'showAnim'=>'fold',

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

			    ),

				'value'=>$model->ad_campaignEndDate,

			)); ?>

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

	</div>


	<div class="row">

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

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

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

	</div>


	<div class="row">

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

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

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

	</div>


	<div class="row">

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

		<?php echo $form->textArea($model,'ad_campaignURL',array('rows'=>6, 'cols'=>50)); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->checkBox($model,'ad_campaignArchive'); ?>

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

	</div>


	<div class="row buttons">

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

	</div>


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


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

I would like the checkbox at the end of the form so that it’s just one more piece of this form. I can’t figure out how to properly do this within the form taking advantage of the model relations. I have read through the Yii Documentation but either I don’t understand what this is doing or I can’t figure out how to apply it in my circumstance.

Any help that can be given would greatly help me. I have a couple different kinds of relation forms like this I need to build but just need some help getting one figured out so I can take that theory and run. I hope I haven’t oversimplified this as I know what it needs to look like in my head but I just don’t know the right way to do this in Yii. I really want to take advantage of these relations instead of building my own loops to do this but if that is right way to do it, I will.

To: el chief . A lot of peaple are using yii and this is the best php fw ever. A lot of questions was sold in forums, sometimes it’s not possible to answer all of them. El chief, you can find an answer and post it here.