Problem With Relation Dclaration In Model

Hi,

I maked new relation in my model. Now it looks like:

ProductController.php:


public function relations()

	{

		return array(          

                        'category' => array(self::HAS_MANY, 'Category', 'cid', 'alias' => 'c'),

                        'productsCategory' => array(self::HAS_MANY, 'ProductsCategory', 'pid'), 

                        'files' => array(self::HAS_MANY, 'File', 'product_id'),

		);

}

Ofc i have model for my table products_category too:

ProductsCategory.php:


<?php


class ProductsCategory extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @return StaticSite 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 'products_category';

	}


	/**

	 * @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('pid', 'numerical', 'integerOnly'=>true),

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

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

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

			array('pid, cid', '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(

                    'Product' => array(self::HAS_MANY, 'Product', 'id', 'alias' => 't'), 

		);

	}


	/**

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

	 */

	public function attributeLabels()

	{

		return array(

			'pid' => 'Produkt ID',

			'cid' => 'Kategoria ID',

		);

	}


	/**

	 * 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('pid',$this->pid,true);

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

		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}

}


?>

Everything look fine, but when i want to use this relation, for example:


$dataProvider=new CActiveDataProvider('Product', array(

                    'criteria'=>array(

                        'with'=>array('productsCategory'),

                        'order'=>'t.sort_order ASC',

                        'condition'=>'t.id=pc.pid AND archived=0',

                        'together'=>true

                    )

));

i get this error:

Relation "productsCategory" is not defined in active record class "Product".

Whats going on? Why isn’t this relation declared properly? Maybe yii is some kind of ‘caching mode’ and he doesnt reload my files?

Good morning.

I think that instead of ProductController.php should be Product.php (model). You were wrong to copy :lol:

Here:




'condition'=>'t.id=pc.pid AND archived=0',



What is "pc.id"? Should not this be the following?:




'condition'=>'t.id=productsCategory.pid AND t.archived=0',



You haven’t got an alias for productsCategory relation.

Regards.

Yep, i haven’t got an alias but it doesn’t fix my problem. App still doesn’t find this relation. :(

Try this:




$criteria=new CDbCriteria;


//Make a criteria


$dataProvider=new CActiveDataProvider('Product', array($criteria));



The problem could be HAS_MANY relation?

Are you sure that the error is here?:




$dataProvider=new CActiveDataProvider('Product', array(

                    'criteria'=>array(

                        'with'=>array('productsCategory'),

                        'order'=>'t.sort_order ASC',

                        'condition'=>'t.id=pc.pid AND archived=0',

                        'together'=>true

                    )

));