Grid Many many

Hi Yii devs.

This is my db structure:


zaposlenik:


IDzaposlenik 

zaposlenik 


odbitak:


IDodbitak

odbitak


odbitak2zaposlenik:


IDodbitak2zaposlenk

IDzaposlenik

IDodbitak

value

While editing “zaposlenik” I want to add new grid. In controller I’ve:


$submodel=Odbitak2zaposlenik::model()->with('odbitak')->findAll();

And it says:


Fatal error: Call to a member function search() on a non-object in C:\xampp\htdocs\place\protected\views\zaposlenik\dodaj.php on line 168

Thank you.

update

What is on the 168 line in the dodaj.php ?

Did you check the content of $submodel after the findAll() ?

168 line:


	'dataProvider' => $submodel->search(),

This is working:


$submodel=Odbitak2zaposlenik::model()->with('odbitak')->findAll();

		foreach($submodel as $s)

		{

			echo $s['IDodbitak'];

		}

Also, I think i need to change findAll to find and I need to add a condition, because I only want to list "Odbitak" which is referenced to "Zaposlenik".

But I don’t now why currently version doesn’t work.

So the $submodel has values after the findAll() - that is in the controller… but does not have any value in the view…

how are you passing this parameter to the view ?

Yes. This is how my controller looks like:


public function actionUpdate($id)

	{

		// ucitaj podatke

		$model=$this->loadModel("Zaposlenik", $id);  

		$submodel=Odbitak2zaposlenik::model()->with('odbitak')->findAll();

		foreach($submodel as $s)

		{

			echo $s['IDodbitak'];

		}

		

		if(isset($_POST['Zaposlenik']))

		{

			$model->attributes=$_POST['Zaposlenik'];

			if($model->save())

				   $this->redirect(array('data')); 

		}

		

		$this->render('dodaj',array(

			'model'=>$model,

			'submodel'=>$submodel,

		));

	}

Great, now it’s more clear :D

So the $submodel has the values returned from the findAll()… check the docs for it - http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail

findAll() returns an array of active records so you cannot use $submodel->search() as $submodel is not an active record, but an array of active records…