0

I'm using CDbCriteria to filter a CListView (Products page)

    $criteria = new CDbCriteria();
    $criteria->join ='LEFT JOIN collections ON collections.id = t.collection_id';

    (isset($_GET['name'])) ? $name = $_GET['name'] : $name = "";
    $criteria->addCondition('t.name LIKE :name');
    $criteria->params = array(':name' => "%$name%");
    $criteria->order = 't.name asc';

    //get count
    $count = Products::model()->count($criteria);

But the problem: In the products model I have a virtual attribute named ProductFinalPrice what calculates the final price based on user role & product category and user specific product price.

How can I sort by this virtual attribute?

Szántó Zoltán
  • 981
  • 1
  • 12
  • 26

1 Answers1

1

If you have a simple virtual attribute which can be calculated by the database you need to define the attribute with a SELECT [...] AS virtual attribute_name. Here, here or here is a topic on that.

If you have a more complicated calculation which is a method in your model, you cannot use CActiveDataprovider. Best would be to use CArrayDataprovider which can use virtual attributes. I'm sorry, I cannot find a good example for that at the moment.

Be aware that CArrayDataprovider is somewhat unperformant, so you are best to avoid it whenever you can and do your best to put the calculation of that virtual attribute in the sql of CDbCriteria.

Community
  • 1
  • 1
chris---
  • 1,536
  • 1
  • 13
  • 14