YII help

Hi guys

I am experiencing an error when I attempt to view some data in the my view template.

In my controller I have $productsList=products::model()->with('categories')->findAll($criteria);

In my view I have this :

<td><?php echo CHtml::encode($category->name); ?></td>

But I get an error : Trying to get property of non-object.

I know I am missing something. I basically want to show the category name for a given product.

Do you pass the needed argument to the view?


This way you could then traverse using:

Hope this helps

categories is the related object of your productList.

so maybe you should use the follow code to get the category name

This does not make sense. Why would I need to use a loop to grab this information?

What I need is the product category. A sample output in the view would be :

Product ID Product Name Category Name

1              ProductA        CategoryB

2              ProductB        CategoryB

Previously, in traditional SQL I would have select productid,productname,categoryname from products p inner join categories c on c.id = p.categoryID.

Now, at the view level, which should already contain the category name for the products since I use the with('categories') clause in the model , I thought I would only need to grab the name of the category. Not loop records.

Also, I implemented the code below and got : Invalid argument supplied for foreach().

Can you please clarify this?

Well you might want to do something like this:

in the Product model:


public function relations()


		return array(




in the ProductController:




and in the view:


foreach($productList as $product) {

     echo $product->id;

     echo $product->name;

     echo $product->Category->name;



Maybe some studies at relational AR in the guide will clarify things:


Kartom, thanks for the reply.

I have done something exactly the same.

I added the relationship in the model as well as add the with category to the controller and then i use the same piece of code in the view.

I have read that the AR docs and attempted to follow this to no avail.

I am not sure about why this does not work.

Also, on a side note, it coming from frameworks which centralise the database tables relationships in the form of a centralise XML file, does YII have anything similar or is it all based in the model object's relations()?

Thanks guys.

I solved the problem by using the following :

In my model : $productsList=products::model()->findAll($criteria);

In my view :

<?php foreach($productsList as $n=>$model):

$category = products::model()-&gt;findbyPk($model-&gt;categoryID); ?&gt; 

  <tr class="<?php echo $n%2?'even':'odd';?>">

    <td><?php echo CHtml::link($model->id,array('show','id'=>$model->id)); ?></td>

    <td><?php echo CHtml::encode($model->name); ?></td>

    <td><?php echo CHtml::encode($model->unit); ?></td>

    <td><?php echo CHtml::encode($category->name); ?></td>


      <?php echo CHtml::link('Update',array('update','id'=>$model->id)); ?>

      <?php echo CHtml::linkButton('Delete',array(



        'confirm'=>"Are you sure to delete #{$model->id}?")); ?>



<?php endforeach; ?>

Now, I know this is not the right way but it was the only way it worked.

I found that when I was previously using the with('category')->findall() function and in my view using : $model->category->name - i was getting errors. I noted that $model->category was an array. If I dumped the $model->category-name object (using var_dump()) the category data that corresponded to each product was not up to date as in the database.

I have thought that maybe it is caching something ? Or maybe the lazy loading has something to do with my error ? As I am new to this framework, I am eager to understand as much as I can about it.

Any feedback is so welcome at the moment regarding this issue.


Just to correct the previous pasted code:

$category = products::model()->findbyPk($model->categoryID);

Should be

$category = categories::model()->findbyPk($model->categoryID);