evoself 2012-09-07 17:29:44 4829次浏览 5条回复 0 0 0
public function getPost()
{           
            $criteria= new CDbCriteria;
            $criteria->select='name,create_time';
            $criteria->join='posts';
            $criteria->limit=30;
            $criteria->order='create_time DESC';
            $criteria->condition='user_id=:user_id';
            $criteria->params=array(':user_id'=>  Yii::app()->user->id);
            $post=  Exercise::model()->findAll($criteria);
            return $post;
}

为什么运行了就报错呢,语法有问题吗 另外$criteria->condition='user_id=:user_id';可以改成$criteria->condition=array('user_id'=>Yii::app()->user->id); 这样可以么

  • 回复于 2012-09-07 17:50 举报

    为什么不用compare?
    $criteria->compare('user_id', Yii::app()->user->id);

  • 回复于 2012-09-07 18:18 举报

    什么错误呢?发出来看看
    改成这样试试:$criteria->condition=array("user_id=".Yii::app()->user->id);

  • 回复于 2012-09-07 19:40 举报
    $criteria= new CDbCriteria;
    $criteria->select='name,create_time';
    $criteria->join='posts';
    $criteria->limit=30;
    $criteria->order='create_time DESC';
    $criteria->compare('user_id',  Yii::app()->user->id);
    $post= Posts::model()->findAll($criteria);
    return $post;
    

    CDbCommand
    CDbCommand 无法执行 SQL 语句:

    SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'posts WHERE user_id='1' ORDER BY create_time DESC LIMIT 30' at line 1. The SQL statement executed was: SELECT name,create_time FROM `posts` `t` posts WHERE user_id=:ycp0 ORDER BY create_time DESC LIMIT 30
    
    controller
    $post= Posts::model()->getNewRecord();      
    $this->render('index',array(
        'post'=>$post,
    )
    
    views
    
    <?php echo $post; ?>
    
  • 回复于 2012-09-07 19:53 举报

    Array to string conversion

  • 回复于 2012-09-07 21:56 举报

    join错误

您需要登录后才可以回复。登录 | 立即注册