Using relations with a condition

How do I go about using relations to find a match using criteria? Or am I trying to do this the wrong way?

I have a cashbook model which has a transaction_date and a sales_tax_type,and a salesTaxRate model which has a sales_tax_type, a valid_from_date, a valid_to_date and a tax_rate.

There is only one record that will match the criteria that the sales_tax_type matches and the transaction_date is between the valid_from_date and the valid_to_date.

I can’t find any examples of how to do this search in a relations, and everything I’ve tried hasn’t worked. Can someone give me any guidance?

You mean a condition in a relation?




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(

            'media'=>array(self::MANY_MANY, 'Media',

                'model_has_media(model_id, media_id)',

                'condition'=> 'model = "'.__CLASS__.'" AND metadata = ""',

             ),

            'thumb'=>array(self::HAS_ONE, 'ModelHasMedia', 'model_id',

                'condition'=> 'model = "'.__CLASS__.'" AND metadata = "thumbnail"',

             ),

            'mediaCount'=>array(self::STAT, 'Media',

                'model_has_media(model_id, media_id)',

                'condition'=> 'model = "'.__CLASS__.'" AND metadata = ""',

             ),

            'user' => array(self::BELONGS_TO, 'Users', 'user_id'),

		);

	}



If using a criteria in a dataprovider i think cannot there was a thread about it in the forum… (use search) where i found this :P




<?php


class DataProvider extends CActiveDataProvider {


    public $together = false;


    /**

     * Fetches the data from the persistent data storage.

     * @return array list of data items

     */

    protected function fetchData() {

        $criteria = clone $this->getCriteria();

        if (($pagination = $this->getPagination()) !== false) {

            $pagination->setItemCount($this->getTotalItemCount());

            $pagination->applyLimit($criteria);

        }

        if (($sort = $this->getSort()) !== false)

            $sort->applyOrder($criteria);


        if ($this->together) {

            return CActiveRecord::model($this->modelClass)->with($criteria->with)->together()->findAll($criteria);

        } else {

            return CActiveRecord::model($this->modelClass)->findAll($criteria);

        }

    }


}



use like…




$criteria=new CDbCriteria(array(

                'condition'=>'status = '.Books::STATUS_PUBLISHED,

                'order'=>'t.id DESC',

                'with'=> array('categories',                    

                    'alias' => 'categories',

                ),

            ));


            $criteria->compare('categories.slug', $cat_name);

            foreach($cat_children as $cat){

                $criteria->compare('categories.slug', $cat->name, false, 'OR');

            }            

            

            $dataProvider = new DataProvider('Books', array(

                'criteria'=> $criteria,

                'pagination'=>array(

                    'pageSize'=>self::PAGE_SIZE,

                ),

                'together' => true

            ));