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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题