致命错误:在非对象上调用成员函数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

dongtaijue1578
dongtaijue1578 为了我的脚本调用getDbCriteria成员函数,我不必这样做吗?$criteria=newCDbCriteria;
5 年多之前 回复
doushen9863
doushen9863 添加了print_r($daten)的结果;质疑
5 年多之前 回复
dpzzkfb1244
dpzzkfb1244 这有点奇怪:$daten=$model::model();,尝试$daten=Comment::model();.检查$daten是否是CActiveRecord的实例。
5 年多之前 回复
doucha4054
doucha4054 然后,您需要在该脚本中调试如何初始化对象以及如何调用该类的getDbCriteria成员函数。
5 年多之前 回复
duanrebo3559
duanrebo3559 我没有调用getDbCriteria的代码。通过我的项目搜索时发现的唯一getDbCriteria是在dev/common/lib/yii/framework/db/ar/CActiveFinder.php
5 年多之前 回复
dongyata3336
dongyata3336 页面刚刚死掉,它显示了这个致命错误:在/Applications/XAMPP/xamppfiles/htdocs/dev/common/lib/yii/framework/web/CActiveDataProvider.php上的非对象上调用成员函数getDbCriteria()第225行
5 年多之前 回复
douzhenao6515
douzhenao6515 任何堆栈跟踪?
5 年多之前 回复
dongreng9864
dongreng9864 来自哪个getDbCriteria函数被调用?
5 年多之前 回复
dsjmrpym220113739
dsjmrpym220113739 试了一下。它没有设置限制,但这次没有错误。更新的问题
5 年多之前 回复
douzong3599
douzong3599 这可能对您有所帮助stackoverflow.com/questions/23239758/...
5 年多之前 回复

1个回答



Dataprovider要求第一个参数是类名或模型实例。 在您的视图中,它是 findAll()</ code>的结果。</ p>

将带有</ code>子句的移动到第二个参数,类似于:</ p>

 &lt;?php 
$ dataProvider = new CActiveDataProvider(Comment :: model(),array(
'criteria'=&gt; array(
'with'=&gt; array) ('posts')
)));

$ this-&gt; widget('zii.widgets.CListView',array(
'dataProvider'=&gt; $ dataProvider,
'itestView'=&gt; ;'_ view_latest_comment',//查看文件位置
));
</ code> </ pre>

注意:</ strong> CActiveDataProvider </ code>将 单独执行 findAll </ code>等。 它只需要知道需要搜索什么。</ p>

这是来自 CActiveDataProvider </ code>源码的php doc部分:</ p>

   $ dataProvider = new CActiveDataProvider('post',array(
'criteria'=&gt; array(
'condition'=&gt;'status = 1',
'order'=&gt;'create_time DESC ',
'与'=&gt;数组('作者'),
),
'countCriteria'=&gt;数组(
'条件'=&gt;'状态= 1',
//' order'和'with'子句对计数查询没有意义
),
'pagination'=&gt; array(
'pageSize'=&gt; 20,
),
));
< / code> </ pre>
</ div>

展开原文

原文

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

duanfuchi7236
duanfuchi7236 这是完全不同的故事,如果你仍然有限制问题,我建议提出新问题。 也许你必须将限制放入条件数组? 通常,dataprovider会自行设置限制/偏移量,因此dataprovider可能会设置为ovveriden。
5 年多之前 回复
dongtiao0657
dongtiao0657 它仍然不会限制输出。 是因为self :: HAS_MANY关系? $ dataProvider = new CActiveDataProvider(Comment :: model(),array('criteria'=> array('with'=> array('posts'=> array('limit'=> 2)))));
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问