Question

I have Post model. When I was using AR like this:

Controller:

public function view($id)
{
   $post = Post::model->find("id=$id");
   $this->render('view', array('post'=>$post))
}

View:

echo $this->title

But when i started using DAO to improve performance of application, I got error:

Trying to get property of non-object

The problem is I am using '->' instead of '[]'. I want change active records to DAO, so I would have change many views.

What is the way to get element using '->' without much work?

Was it helpful?

Solution

Untested, but I think:

$cmd = Yii::app()->db->createCommand($q);
$cmd->setFetchMode(PDO::FETCH_CLASS, 'Page');
$model = $cmd->queryRow();
// $model->title 

Also

what are you using now as your code though? Also how you use find is not ideal. should be

find('id=:id', array(':id'=>$id));

OTHER TIPS

Please show your code for DOA. However, DAO commands return arrays. They do not return objects. So when you do

echo $this->title

You are trying to get the title from something that is not an object (but probably an array; or undefined)...

The definitive guide shows examples of how to use DOA

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top