I'm using Yii 1.1.15 and am getting this error
Fatal error: Call to a member function getDbCriteria() on a non-object
the code below is in my view
<?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
));
?>
the relation in my comment.php
is 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 also tried this, but it does not 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 i 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 to the results returned, but can't seem to get it to work. any idea on what i'm doing wrong or missing? Thanks
Dataprovider require first parameter to be class name or model instance. In your view it's result of findAll()
.
Move with
clause to second parameter, something like that:
<?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 findAll
etc. by itself. It just need to know what need to be searched for.
Here is 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,
),
));