Fatal error: Call member function getDbCriteria () on non-object
I am using Yii 1.1.15 and am getting this error
Fatal error: Call to a member function getDbCriteria() on a non-object
below code
<?php
$model = new Comment(); //name of my model Project refers to Mysql innoDB table tblproject.
$daten=$model::model();
$dataProvider=new CActiveDataProvider($daten->with(array('posts' => array('limit'=>6)))->findAll());
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view_latest_comment', //view file location
));
?>
attitude in my comment.php
as such
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, $this->module->userModelClass, 'userId'),
'posts' => array(self::HAS_MANY, "CommentsPosts", array("commentId" => "id"))
);
}
UPDATE: I've also tried this, but it doesn't set the limit to 5
$model = new Comment();
$daten=$model::model();
$criteria = new CDbCriteria;
$criteria->limit=5;
$dataProvider=new CActiveDataProvider($daten, array('criteria'=>$criteria));
when print_r($daten);
i get this
Comment Object (
[_type:Comment:private] =>
[_key:Comment:private] =>
[_make:Comment:private] =>
[_model:Comment:private] =>
[_year:Comment:private] =>
[_new:Comment:private] =>
[_attributes:CActiveRecord:private] => Array ( )
[_related:CActiveRecord:private] => Array ( )
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] =>
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array ( )
[_validators:CModel:private] =>
[_scenario:CModel:private] =>
[_e:CComponent:private] => Array (
[onbeforesave] => CList Object (
[_d:CList:private] => Array (
[0] => Array (
[0] => CTimestampBehavior Object (
[createAttribute] => createDate
[updateAttribute] =>
[setUpdateOnCreate] =>
[timestampExpression] =>
[_enabled:CBehavior:private] => 1
[_owner:CBehavior:private] => Comment Object *RECURSION*
[_e:CComponent:private] =>
[_m:CComponent:private] => )
[1] => beforeSave ) )
[_c:CList:private] => 1
[_r:CList:private] =>
[_e:CComponent:private] =>
[_m:CComponent:private] => ) )
[_m:CComponent:private] => Array (
[commentable] => CommentableBehavior Object (
[mapTable] =>
[mapCommentColumn] => commentId
[mapRelatedColumn] =>
[mapMakeColumn] => make_code
[mapModelColumn] => model_code
[mapYearColumn] => year_made
[mapVariantColumn] => variant
[_enabled:CBehavior:private] => 1
[_owner:CBehavior:private] => Comment Object *RECURSION*
[_e:CComponent:private] =>
[_m:CComponent:private] => )
[CTimestampBehavior] => CTimestampBehavior Object (
[createAttribute] => createDate
[updateAttribute] =>
[setUpdateOnCreate] =>
[timestampExpression] =>
[_enabled:CBehavior:private] => 1
[_owner:CBehavior:private] => Comment Object *RECURSION*
[_e:CComponent:private] =>
[_m:CComponent:private] => ) )
[_new:CActiveRecord:private] => )
I'm trying to dynamically set a limit on the returned results, but can't get it to work. any idea on what i am doing wrong or missing? Thanks to
source to share
Dataprovider requires the first parameter to be a class name or model instance. In your opinion, the result findAll()
.
Move the sentence with
to the second parameter, something like this:
<?php
$dataProvider = new CActiveDataProvider(Comment::model(), array(
'criteria' => array(
'with' => array('posts')
)));
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view_latest_comment', //view file location
));
NOTE. CActiveDataProvider
will do it himself findAll
, etc. You just need to know what to look for.
Here is the php doc part from CActiveDataProvider
source:
$dataProvider=new CActiveDataProvider('Post', array(
'criteria'=>array(
'condition'=>'status=1',
'order'=>'create_time DESC',
'with'=>array('author'),
),
'countCriteria'=>array(
'condition'=>'status=1',
// 'order' and 'with' clauses have no meaning for the count query
),
'pagination'=>array(
'pageSize'=>20,
),
));