using 2 conditions in CActiveDataProvider

I am using 2 [b]CActiveDataProvider conditions

[/b]





	

public function actionawait()

	{

				

		

		

		

		$id=Yii::app()->user->id;

		$dataProvider=new CActiveDataProvider('a' ,array('criteria'=>array(

        'condition'=>'status_id=1','condition'=>'person_id='.$id,)));

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

			'dataProvider'=>$dataProvider,

		

		

		));

	}

	

	




Is this the right way to do it

Will both the conditions run

The second condition will overwrite the first one in your code - two array elements can’t have the same index.

Try this:




'condition' => 'status_id = 1 AND person_id = ' . $id,



what is the ‘a’ in $dataProvider=new CActiveDataProvider(‘a’ ,array(‘criteria’=>array(

    'condition'=>'status_id=1','condition'=>'person_id='.$id,)));

is that the model name???

Hi,

a is the model class name :)

thanks… yeah i know, a newbie question… :) i still can’t get this to work… maybe i’m going about it the wrong way…

what i am trying to do is create a screen like the GII generated admin screen for a model called ‘promos’… i get the screen to show fine but i just want to fine tune it a bit to only show SOME of the records on the promos model/table - not all of them… so i created a new dataProvider in my controller that looks like this:

public function actionPromosList() {

    // renders the view file 'protected/views/site/index.php'


    // using the default layout 'protected/views/layouts/main.php'


    $model = new Promos();

// $model->unsetAttributes(); // clear any default values

    if (isset($_GET['Promos'])) {


        $model->attributes = $_GET['Promos'];


    }


    $this->user = Users::model()->find('businessID');


    $dataProvider = new CActiveDataProvider('Promos', array('criteria' => array(


            'condition' => Promos::model()->businessID == $this->user->businessID)));





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


        'model' => $model,


        'business' => $this->user->businessID,


        'dataProvider' => $dataProvider


    ));


}

what i’m trying to do here is select records from the promos table that match the businessID of the person who is logged in (from the Users model/table)… ignore the other parameters that i am passing to this screen, like the model and business - i’m using them for displays there. in the promoslist program the coding looks like this:

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


    'id' => 'promos-grid',


    'dataProvider' => $dataProvider, 

// ‘filter’ => $model,

    'columns' => array(


        'id',


        'offerCode',


        'value',


        'details',


        'startDate',


            


        'endDate',


        'maxOffers',


        


       


        array(


            'class' => 'CButtonColumn',


        ),


    ),


));

but i am still getting ALL of the records from the Promos model/table to show on this new screen - it is not showing just the promos for the logged in user.

screen can be seen at http://demo.engajdloyalty.com/index.php/promos/promoslist - it may be up or down as i’m testing all sorts of ideas…

Hi,

what is this line means

‘condition’ => Promos::model()->businessID == $this->user->businessID

it should be like this

‘condition’ => 'business_id = ’ . $this->user->businessID

you have to mention column name in condition for filter…

I hope this will work…

WOW - that worked… i read the documentation and NOWHERE is that stated that i need to use the column name like that with single quote (’) marks around it and the equal sign… I NEVER would have figured that out from the documentation… thank you very much… what i need is a mentor like you… do you do mentoring or consultuing?

also is there a way to sum one of the columns? i need to sum the ‘value’ field at the end of every page and don’t know where or how to sum it…

Hi,

First of all, congratz for your success… I am glad to help you…

This is the way you can get sum(column name)




public function getTotals($ids)

        {

                $ids = implode(",",$ids);

                

                $connection=Yii::app()->db;

                $command=$connection->createCommand("SELECT SUM(columnname)

                                                                                        FROM `tablename` where id in ($ids)");

                return "Total Rs: ".$amount = $command->queryScalar();

        }



how do it then access that ‘getTotals’ into the cGridView in my View at the bottom underneath the column (promo_amt)

i get the following errors when i try this in my ‘Promos’ model:

PHP warning

implode(): Invalid arguments passed

/var/www/demo.domainxxx.com/html/protected/models/Promos.php(123)

111 $criteria->compare(‘businessID’,$this->businessID);

112 $criteria->compare(‘signupOffer’,$this->signupOffer);

113 $criteria->compare(‘broadcastTime’,$this->broadcastTime);

114

115

116 return new CActiveDataProvider($this, array(

117 ‘criteria’=>$criteria,

118 ));

119 }

120

121 public function getTotals($ids)

122 {

123 $ids = implode(",",$ids);

124

125 $connection=Yii::app()->db;

126 $command=$connection->createCommand("SELECT SUM(value)FROM Promos where id in ($ids)");

127 return "Total Rs: ".$amount = $command->queryScalar();

128 }