property

Controller:


$ultimaEncomenda=null;

             

                $ultimaEncomenda=Encomenda::model()->findAllBySql(

                           "select * from encomenda where idencomenda=(select max(idencomenda) from encomenda where customer=:customer)",

                           array(":customer"=>Yii::app()->user->id));

$this->render('index',array('encomenda'=>$ultimaEncomenda));



view:


<?php  echo ($encomenda!=null)?$encomenda->quantity:0 ?>

why i get Trying to get property of non-object ?

Check the documentation for findAllBySql() and see what it returns if no records is found…

Hint: it’s not null ;)

ok,it’s an array.Im new to yii.How to check if the array is empty from the view?

Now i did


<?php  echo (!empty($encomenda))?$encomenda->quantity:0 ?>

but i get the same error

As you already figured out, $encomenda holds an array. It doesn’t have properties.

Maybe


<?php  echo count($encomenda) ?>

?

well in that case i dont want findBySql method.

How can i run the query "select * from encomenda where idencomenda=(select max(idencomenda) from encomenda where customer=:customer)"

and send to the view the quality attribute value?

Ok, I got it now. You need a finder method that returns a model instance. CActiveRecord::findBySql() does exactly that. The cause of the problem is the findAllBySql() method used in your code. It returns an array of objects.

Exactly, my mistake was to confuse the two methods.By the way and what if the sql returns more than two records.The active record get the first one?