致命错误:在非对象上调用成员函数getDbCriteria()

时间:2014-12-30 11:45:11

标签: php yii

我使用Yii 1.1.15并收到此错误

Fatal error: Call to a member function getDbCriteria() on a non-object

以下代码在我看来

  <?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
                        ));
                   ?>

comment.php中的关系就是这样

public function relations()
{
    return array(
        'user' => array(self::BELONGS_TO, $this->module->userModelClass, 'userId'),
        'posts' => array(self::HAS_MANY, "CommentsPosts", array("commentId" => "id"))           
    );
}

更新 我也尝试了这个,但它没有将限制设置为5

$model = new Comment(); 
$daten=$model::model();
                    $criteria = new CDbCriteria;
                    $criteria->limit=5;

$dataProvider=new CActiveDataProvider($daten, array('criteria'=>$criteria));

当我print_r($daten);我得到了这个

    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] => ) 

我试图动态为返回的结果设置限制,但似乎无法让它发挥作用。关于我做错了什么或错过什么的任何想法?感谢

1 个答案:

答案 0 :(得分:0)

Dataprovider要求第一个参数是类名或模型实例。在您看来,这是findAll()

的结果

with子句移动到第二个参数,类似于:

<?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
));

注意: CActiveDataProvider会自行执行findAll等。它只需要知道需要搜索什么。

这是来自CActiveDataProvider来源的php doc部分:

$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,
     ),
));
相关问题