How To Use Non-Model Field Value

I believe this to be a relatively simple issue but I have been searching for hours on the forum and have not found

what I am looking for.

I have a form (CGridView) where I use the advanced search to filter the rows returned (AllSites). I then want to allow the

user to click on a button (or a link) and create a copy of the list with their customer id in another table (MySites)

It works fine but my requirements were changed by the user. Now they want to name the list when they create it and want to be able to manage multiple lists. So I added a table "MyList" to the schema.

I have typical CRUD forms for "AllSites", "MyList" and for "MySites"

Table 1: AllSites

(id, name, url)

Table 2: MyList

(id, my_id)

Table 3: MySites

(id, name, url, my_id, list_id)

Form reviewSites.php contains link to controller actionCreateList($myid)

Works great but I don’t know the best place to ask the user for the “listname”.

I could do it in the reviewSites view they are on or navigate to a new form (create a list) then call the function to populate the list.

reviewSites.php





        echo CHtml::TextField("Campaign", 'Enter Campaign Name');   //, 'campaign'=>$_POST["Campaign"]

 	echo CHtml::link('Add Site to List',array('createReviewlist', 'id'=>$id ));


         $this->widget('zii.widgets.grid.CGridView', array(

	'id' => 'reviewsite-grid',

	'dataProvider' => $model->searchSites($criteria),

	'filter' => $model, ....




If it is not part of the model and is just a field on the reviewSites view above the CGridView, how do I pass the value to the controller?

In the controller I do the following:

Read the Parameters for the search

Get the data

Create the list header (default the name currently)

Create the list detail

Controller:




public function actionCreateReviewlist($id) {


	$campaign = "Test" ;           // $_POST['reviewSites']['Campaign'];


	$model = new Website('searchSites');

 	$model->unsetAttributes();

	$params = Yii::app()->user->getState('SiteSearchParams');

	if ( isset($params) ) {

		$model->attributes = $params;

	}

	$criteria = new CDbCriteria;

	// get the websites that are not in table for that partner

	$criteria-> condition = (" t.id NOT IN (select website_id from partner_site where partner_id = ".$id.")");

	$dataProvider = $model->searchSites($criteria);

	$sites = $dataProvider->getData();


	// create campaign

		$c = new Campaign;

		$c->title = $campaign;

		$c->partner_id=$id;

		$c->save();




		foreach ($sites as $sid) {

		// for each site insert a new record.

		$ps = new PartnerSite;

		$ps->partner_id = $id;

		$ps->website_id = $sid->id;

		$ps->campaign = $c->id;

		$ps->save();

		}


	 $this->redirect(array('reviewlist', 'id'=>$id));


    }




Any suggestions? I have been playing with this and can’t seem to make it work.

How do I reference the field value the user enters ?

Could I also use Modal Windows to create a series of steps?

Thanks alot for any ideas or suggestions!

Hello, what I perceived from your question is that how to send the data , which is not related to the model, to the controller, am I correct ? If so, then I guess there are 2 methods :

  1. Stay using the link but add the param on clicked

You could add jquery like this :




$('#id-of-the-link').bind('click', function(e) {

// get name from input

var campaignVar = $('#campaign').val();

//redirect user

e.preventDefault();

window.location.href = encodeURI($(this).attr("href") + '&campaign=' + campaignVar );

});



But first of all you should set the id’s of the elements

  1. Use form instead of link



<?php

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

		'id'=>'form-id',

		'action'=>... // to the controller action

        )

	)); 

echo CHtml::textField("Campaign", 'Enter Campaign Name'); 

echo CHtml::submitButton("Send");

$this->endWidget();

?>



And in your controller :




public function actionCreateReviewlist($id) {

        

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

        $campaign = $_POST['Campaign'];

        ...

}



Thank you C.S. Putera !

You are correct, I was looking for the best way to send the data from a field that was not part of the model.

I believe the problem I was encountering was the fact that I did not have a form referenced.

I will play with this concept, thank you again!

You are welcome :)