dstudio
(Nedim T)
February 6, 2012, 9:35pm
1
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.
mdomba
(Maurizio Domba Cerin)
February 7, 2012, 9:16am
3
What is on the 168 line in the dodaj.php ?
Did you check the content of $submodel after the findAll() ?
dstudio
(Nedim T)
February 7, 2012, 10:33am
4
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.
mdomba
(Maurizio Domba Cerin)
February 7, 2012, 10:39am
5
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 ?
dstudio
(Nedim T)
February 7, 2012, 11:18am
6
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,
));
}
mdomba
(Maurizio Domba Cerin)
February 7, 2012, 11:24am
7
Great, now it’s more clear
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…