[SOLVED] Pass parameter from search form to relational stat query

I have stat rules in model with paramater. I don,t know how to pass parameter from search to my rules in model…

Here my code…

in model




<?php


/**

 * This is the model class for table "inv_stock".

 *

 * The followings are the available columns in table 'inv_stock':

 * @property string $id

 * @property string $stock_id

 * @property string $stock_name

 * @property integer $stock_quantity

 * @property string $supplier_id

 * @property string $company_price

 * @property string $selling_price

 * @property string $category

 * @property string $date

 * @property string $uom

 * @property string $expire_date

 */

class Stock extends CActiveRecord

{

	public $date_first;


	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Stock the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'inv_stock';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('stock_quantity', 'numerical', 'integerOnly'=>true),

			array('stock_id, stock_name, category, uom', 'length', 'max'=>120),

			array('supplier_id', 'length', 'max'=>250),

			array('company_price, selling_price', 'length', 'max'=>10),

			array('expire_date', 'length', 'max'=>65),

			array('date, date_first', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, stock_id, stock_name, stock_quantity, supplier_id, company_price, selling_price, category, date, uom, expire_date, date_first', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'beli_kw' => array(self::STAT,'Purchase','stock_id',

						'select'=>'sum(quantity)',

						'condition'=>'YEAR(date)=YEAR(:tgl) AND MONTH(date)=MONTH(:tgl)',

						'params'=>array(

									':tgl'=>$this->date_first,

									),

						),

			'beli_rp' => array(self::STAT,'Purchase','stock_id',

						'select'=>'sum(total)',

						),

			'jual_kw' => array(self::STAT,'Sales','stock_id',

						'select'=>'sum(quantity)',

						),

			'jual_rp' => array(self::STAT,'Sales','stock_id',

						'select'=>'sum(amount)',

						),			

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'stock_id' => 'Stock',

			'stock_name' => 'Stock Name',

			'stock_quantity' => 'Stock Quantity',

			'supplier_id' => 'Supplier',

			'company_price' => 'Company Price',

			'selling_price' => 'Selling Price',

			'category' => 'Category',

			'date' => 'Date',

			'uom' => 'Kemasan',

			'expire_date' => 'Expire Date',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.

		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id,true);

		$criteria->compare('stock_id',$this->stock_id,true);

		$criteria->compare('stock_name',$this->stock_name,true);

		$criteria->compare('stock_quantity',$this->stock_quantity);

		$criteria->compare('supplier_id',$this->supplier_id,true);

		$criteria->compare('company_price',$this->company_price,true);

		$criteria->compare('selling_price',$this->selling_price,true);

		$criteria->compare('category',$this->category,true);

		$criteria->compare('uom',$this->uom,true);

		$criteria->compare('expire_date',$this->expire_date,true);

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}



in search form




<table>


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

	'action'=>Yii::app()->createUrl($this->route),

	'method'=>'get',

)); ?>




	<td>

		<?php echo "Tahun-Bulan :"; ?>

		<?php 

		echo $this->widget('zii.widgets.jui.CJuiDatePicker', array(

				'name' => 'Stock[date_first]',

				'language' => 'id',

				'value' => $model->date_first,

				// additional javascript options for the date picker plugin

				'options'=>array(

					'showAnim'=>'fold',

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

					'changeMonth' => 'true',

					'changeYear'=>'true',

					'constrainInput' => 'false',

				),

				'htmlOptions'=>array(

					'style'=>'height:20px;width:70px;',

				),

		// DONT FORGET TO ADD TRUE this will create the datepicker return as string

		),true);


		?>		

	</td>


	<td>

		<?php echo CHtml::submitButton('Search'); ?>

	</td>


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


</table><!-- search-form -->



rules beli_kw doesn’t show result… can anybody help me?

STAT relation doesn’t work for searching …

Take a look at this wiki:

http://www.yiiframework.com/wiki/319/searching-and-sorting-by-count-of-related-items-in-cgridview/

The article deals with ‘count’, but I think you can try it with ‘sum’.

The article doesn’t seem quite like i expected…

I just want to find ‘sum’ from other table with condition date from $date_first, date_first is set by search from.

How I can pass value of $date_first to param condition in relation model?


'beli_kw' => array(self::STAT,'Purchase','stock_id',

						'select'=>'sum(quantity)',

						'condition'=>'YEAR(date)=YEAR(:tgl) AND MONTH(date)=MONTH(:tgl)',

						'params'=>array(

									':tgl'=>$this->date_first,

									),

						),

Ah, sorry.

You can’t set a dynamic parameters in your relations() method.

Use a dynamic relational query instead.

http://www.yiiframework.com/doc/guide/1.1/en/database.arr#dynamic-relational-query-options

I mean, set the condition in ‘with()’.

After I read the article, I change the controller


	public function actionAdmin()

	{

		$model=Stock::model()->with(array(

			'beli_kw'=>array(

				'condition'=>'YEAR(date)=YEAR(:tgl) AND MONTH(date)=MONTH(:tgl)',

				'params'=>array(

					':tgl'=>$model->date_first,

				),

			),

		))->findAll();

			

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

			'model'=>$model,

		));

	}	

date is field in Purchase model…

i still get error though, what is it wrong in my code?

Maybe you have to disambiguate the column.

And you have to use 2 params.

We can’t share a place holder for 2 or more parameters even when they hold the same value.




	$model=Stock::model()->with(array(

		'beli_kw'=>array(

			'condition'=>'YEAR(beli_kw.date)=YEAR(:tgl) AND MONTH(beli_kw.date)=MONTH(:tgl2)',

			'params'=>array(

				':tgl'=>$model->date_first,

				':tgl2'=>$model->date_first,

			),

		),

	))->findAll();



I’ve tried your suggestion… It turn out error “Undefined variable: model”…

How to access $date_first from form search?

Ah, sorry again.

I didn’t notice that $model is not yet defined at that moment. :(

Well, return to the beginning and try something like this:




class Stock extends CActiveRecord

{

	public $date_first;


	...

	public function relations()

	{

		return array(

			'beli_kw' => array(self::STAT,'Purchase','stock_id',

						'select'=>'sum(quantity)',

						),

			...

		);

	}

	...


	public function search()

	{

		$criteria=new CDbCriteria;


		$criteria->with('beli_kw' => array(

                        'condition'=>'YEAR(beli_kw.date)=YEAR(:tgl) AND MONTH(beli_kw.date)=MONTH(:tgl2)',

                        'params'=>array(

                                ':tgl'=>$this->date_first,

                                ':tgl2'=>$this->date_first,

                        )

		);


                $criteria->compare('id',$this->id,true);

		...

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}



It solved…

I return from beginning and add this code in my search function


	public function search()

	{

		$criteria=new CDbCriteria;

	

		$criteria->with = array('beli_kw' => array(

				'condition'=>'YEAR(date)=YEAR(:tgl) AND MONTH(date)=MONTH(:tgl2)',

				'params'=>array(

						':tgl'=>$this->date_first,

						':tgl2'=>$this->date_first,

				)

		));

		

		$criteria->compare('id',$this->id,true);

		.....

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



Thanks Softark…