Hi,
I don’t know the correct english terms for this, so it’s hard to find the answer yet. I think it’s just a basic PHP Object thing, but it could also be something of Yii. So I want to give it a try.
I always thought that this two things are the same:
// 1
$test = new Product;
$test->category($id)->findAll();
// 2
Product::model()->category($id)->findAll();
Am I correct this is creating an instance of an class?
But this is not the same. I realize this, because I got an MySQL error. So I found that the thing that cause this, was that I call a scope ‘newest’ in a line above for a $dataProvider, like this:
$dataProvider=new CActiveDataProvider(Product::model()->newest(),array());
// This one gives me a MySQL error, because it includes the Order
// part (ORDER BY id DESC) of the newest() scope of the line above too so the error
// is: Column 'id' in order clause is ambiguous.
print_r(Product::model()->category($id)->findAll());
// But this one works, not including the newest() scope.
$test = new Product;
print_r($test->category($id)->findAll());
The newest scope in my Product model:
public function newest()
{
$this->getDbCriteria()->mergeWith(array(
'order' => 'id DESC',
'offset'=>0,
));
return $this;
}
Could someone explain this to me, or give me a hint what keywords I should look for? I guess it’s a very simple thing, but I just don’t get it .
update:
Yes I understand that I can fix it with:
Product::model()->[b]resetScope/b->category($id)->findAll();
or in the newest() scope (add t. ):
public function newest()
{
$this->getDbCriteria()->mergeWith(array(
'order' => 't.id DESC',
'offset'=>0,
));
return $this;
}
But what I don’t understand is why the DbCriteria are still in the ‘memory’ when creating a new object. Or am I not creating a new object?