declaring new Model('search')

Hi,

the Yii crud generator creates:

  1. This line in the adminAction() Controller method:

$model=new FlkActivities(‘search’);

  1. The method search() inside the FlkActivities Model.

It seems that the method is called automatically, but I am a little bit confused by the syntax new FlkActivities(‘search’) and by the fact that it seems to be called ‘anyway’.

In fact I tried to call new FlkActivities() with no parameter and new FlkActivities(‘mymethod’) but it seems that always the ‘search’ method is called.

Can anyone help me understanding how does it work?

I want to add an ‘order by’ condition to the model used in the controller adminAction() method and the only way I managed to do it is, inside the FlkActivities Model

  1. Created a scope:

public function scopes()

    {

        return array(

            'ordByNewer'=>array(

                'order'=>'pdate DESC',

            ),

        );

    }

  1. Modified the return command inside the search() function from this:

return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

to this




return new CActiveDataProvider($this->ordByNewer(), array(

			'criteria'=>$criteria,

		));



It works, but I would have liked to keep the ordByNewer() scope call outside the search() function. Is there any way to do this?

Thank you,

Andrea

ok, this is my firt reply…

so forgive me if I’m wrong, I’m also quite newby.

I think that the search() funcion is called inside Admin view file:




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

	'id' => 'division-grid',

	'dataProvider' => $model->search(),

	'filter' => $model,

	'columns' => array(

		'id',

		array(

				'name'=>'fk_grado_id',

				'value'=>'GxHtml::valueEx($data->fkGrado)',

				'filter'=>GxHtml::listDataEx(Grado::model()->findAllAttributes(null, true)),

				),

		'descripcion',

		'turno',

		'orden',

		array(

			'class' => 'CButtonColumn',

		),

	),

)); ?>

I think that what you need is to alter $criteria order attribute.

inside search function.

$criteria->order

$criteria->order = ‘pdate DESC’

Best Regards

The reason why it works is because the function ‘search’ returns a dataProvider object.

So you can use either a dataProvider directly, or create a function which returns one. :)

Thank you very much guys :rolleyes: