Has_One/has_Many Problem

Hi,

I try to understand why relation table return an array instead of object.

My model looks like:




        public function eventCriteria() {

                $criteria=new CDbCriteria;

		$criteria->addCondition('t.nazwa IS NOT NULL');

                $criteria->addCondition('t.usuniety = false');

		$criteria->compare('t.idUzytkownik',$this->idUzytkownik);

                $criteria->order =('event.czasLokalny desc');

                $criteria->with = array('event');

                $criteria->together = true;

		return $criteria;

	}

        public function eventSearch() {

		return new CActiveDataProvider($this, array(

			'criteria'=>$this->eventCriteria(),

                        'sort'=>array(

                            'defaultOrder'=>'t.idUzytkownik',

                        ),

			'pagination'=>array(

                         'pageSize'=>Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),

                        ),

		));

	}



relation:


'event' => array(self::HAS_ONE, 'Event', 'idUzytkownik')

And if I want to display event in cgridview i can do it like $data->event["czasLokalny"] but dont know why when I try $data->event->czasLokalny, PHP notice says "Trying to get property of non-object". And when i change relation to HAS_MANY i cant display anything(or dont know how).

So any explanation of this problem will be very helpful, probably i miss something important here.

Thanks in advance

Dear Friend

1.When using CActiveDataProvider , It is not advisable to use CDbCriteria::order priorhand.It will affect the pagination in the CActiveDataProvider.

2.HAS_ONE relation gives a single object.If the relation is not met and related object is null it throws error.

3.HAS_MANY return array of objects.To access the values you have to use foreach loop.Otherwise it will throw error.

Here is a must read article which will address some inconveniences in defining relations.

Drills : Search by a HAS_MANY relation

Regards

Thanks, this link helped me a lot. But now i have another question. If i have relation HAS_MANY in example from my first post, i can get




$data->event[0]->czasLokalny



in the view, but even if there are more events i cant show any more records like for example




$data->event[1]->czasLokalny



count($data->event) always shows 0 or 1.

Any idea why ?