related models and findall or find

i have a table in database known as modules

it’s struscture is

id(primary key)

display_name(module name)

parent_id(the parent of the module where the parent id 0 shows that the module is the parent module) and the level is upto 3 that is

I have the

      Parent module


      child mochdule


      subchild module

every child module belongs to a parent and subchile belongs to a child module

now i am using

    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(


				'groupmodule'=>array(self::HAS_ONE, 'UserGroupsModulesNew', 'parent_id'),


				'childmodule'=>array(self::BELONGS_TO, 'UserGroupsModulesNew', 'parent_id'),





	);


}

when i run the query it returns the correct result and number of rows but when i retreive the result it only shows one result or row …I am using this in case of update view where the id condition gets concatinated in the where clause

     please help me out 

2)also when i use findall

    public function actionUpdate($id)


{


	


	$model=$this->loadModel($id);


		


	$allmodules=$this->getAllModules();


	echo '<pre>';


	print_r($allmodules);


	$mainmodules=CHtml::listData($allmodules,'id','display_name');


	$this->render('update',array('model'=>$model,'mainmodules'=>$mainmodules));


}

public function loadModel($id, $scenario = false)

{


	$model=array();


	$model=UserGroupsModulesNew::model()->with('groupmodule')->with('childmodule')->findAll('t.id=:id',array(':id'=>$id));


	


	if($model===null)


		throw new CHttpException(404,Yii::t('userGroupsModule.general','The requested page does not exist.'));


	if ($scenario)


		$model->setScenario($scenario);


	return $model;


}

I get errors like get_class() expects parameter 1 to be object, array given

Please help me out i have spent around 3 days …the forum has got slow i don’t know why but last 7 to 8 posts are unanswered no one bothers…i hope i’ll get a reply

Hi!

You use findAll() method - it returns an array of objects, not object.

Then, you try this:

, so you try to call method of array.

Correct your code as follows:


$model=UserGroupsModulesNew::model()->with('groupmodule')->with('childmodule')->findByPk($id);

findByPk() returns a single model, and findAll() returns list of models, even if there is one model.

my question is something diff i want that the findall returns multiple rows …i know that findall returns all the rows but i am using relation just because of that there are two rows containig the primar key in this case id…so what yii does is that due to minimize or remove the redundancy it picks up only one record if there are two records related to the primary key but i need that both rowsssssssssssssss…not one this was my first question and the second is that if i’ll get two rows how would i use

$model=$this->loadmodel($id);

just because $this->loadmodel($id) return two rows how would i use $model in an view(update)…

so these are my questionsssssssssss so please help me out asap

For first question: you use relation HAS_ONE:

but you need relation HAS_MANY:


'groupmodule'=>array(self::HAS_MANY, 'UserGroupsModulesNew', 'parent_id')

Then, if you want to find record by primary key, use findByPk, because your code whatever will return ONE record, with his related records. It will not be list of records, it will be one object, in which you can do $model->groupmodule or $model->childmodule to get related records. The only difference in using method with() is the eager loading instead of lazy loading.

can you please explain why would we use has_many relationship.I shall be really thankful to you

Hi,

try to read this and you probably will understand which relation type when to use.