doulai8128 2014-12-30 11:45
浏览 202

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

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

  • 写回答

1条回答 默认 最新

  • dsdfd2322 2014-12-30 13:11
    关注

    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,
         ),
    ));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python