I am using a slave db, and when i switch the db connection, this is what i get:
what does this mean?
CException
Property "CActiveDataProvider.select" is not defined.
public function actionIndex() {
$criteria = new CDbCriteria();
$criteria->condition = 'user_id = ' . Yii::app()->user->id;
VillageSlaveM::$server_id = Yii::app()->session['user_active_world'];
$dataProvider = new CActiveDataProvider('VillageSlaveM', $criteria);
$this->render('index', array(
'dataProvider' => $dataProvider,
));
}
Instead of line:
$dataProvider = new CActiveDataProvider('VillageSlaveM', $criteria);
try
$dataProvider = new CActiveDataProvider('VillageSlaveM', array('criteria'=>$criteria));
I'm not sure, but documentation describes CActiveDataProvider's construct.
Related
Relations of review below:
return array(
'product' => array(self::BELONGS_TO, 'Product', 'product_id'),
'profile' => array(self::BELONGS_TO, 'Profile', 'profile_id'),
'rating' => array(self::BELONGS_TO, 'Rating', 'rating_id'),
'reviewcomments' => array(self::HAS_MANY, 'Reviewcomment', 'review_id'),
'reviewhelpfuls' => array(self::HAS_MANY, 'Reviewhelpful', 'review_id'),
'commentCounts' => array(self::STAT, 'Reviewcomment', 'review_id'),
);
Controller code below:
$criteria = new CDbCriteria();
$criteria->addCondition("t.seoUrl = :seoUrl");
$criteria->addCondition("t.published = 1");
$criteria->params = array("seoUrl"=>$seoUrl);
$data['product'] = Product::model()->find($criteria);
if($data['product']){
$data['product']->hits = $data['product']->hits+1;
$data['product']->save();
} else{
throw new CHttpException(404, "Invalid Request for product");
}
$product_id = $data['product']->id;
$criteria = new CDbCriteria();
$criteria->addCondition("t.product_id = '$product_id'");
if(isset($_GET['rating']))
{
$rating_id = $_GET['rating'];
$criteria->addCondition("t.rating_id = '$rating_id'");
}
$criteria->addCondition('t.status = "approved"');
if(isset($_GET['sort']))
{
$sorting = $_GET['sort'];
if($sorting=='newest')
$criteria->order = "t.postDate DESC";
elseif($sorting=='oldest')
$criteria->order = "t.postDate ASC";
elseif($sorting=='rating_high')
$criteria->order = "t.rating_id DESC";
elseif($sorting=='rating_low')
$criteria->order = "t.rating_id ASC";
}
$data['reviews'] = new CActiveDataProvider('Review', array(
'criteria'=>$criteria,
'pagination'=>array('pagesize'=>4)
));
Now when I am running this code, I am getting CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't.rating_id' in 'where clause'
Thanks for your help
Intstead of using 't' as alias for your tables, start using getTableAlias(false, false)
In your case it would be:
$criteria->addCondition(Review::model()->getTableAlias(false, false).".rating_id = '$rating_id'");
More importantly this code screams SQL injection
$rating_id = $_GET['rating'];
$criteria->addCondition("t.rating_id = '$rating_id'");
Never use user input without processing it. Yii allows you to bind values which would be escaped for you - no threat of SQL injection(atleast as far as i know). You should be creating your criteria like so:
$criteria->addCondition(Review::model()->getTableAlias(false, false).".rating_id = :rating_id");
$criteria->params = array(
':rating_id' => $rating_id,
);
I want to display email inbox with read and unread status and also display them in descending order. Here is my code:
$criteria = new CDbCriteria;
$criteria->order = 'emailid DESC';
$model = Email::model()->findAllByAttributes(
array(
'to_userid' => Yii::app()->user->id,
),
array(
'condition' => 'email_status=2 OR email_status=1',
), $criteria
);
$this->render('inbox', array(
'model' => $model,
));
But this is not working properly.
I don't think thats the right way of using cdbcriteria. Here, try this:
$criteria = new CDbCriteria();
$criteria->condition = 'to_userid=:userId AND (email_status=2 OR email_status=1)';
$criteria->params = array(':userId'=>Yii::app()->user->id);
$criteria->order = "emailid DESC";
$model = Email::model()->findAll($criteria);
$this->render('inbox', array(
'model' => $model,
));
This should work. Hope that helps. :)
I am not sure why am I receiving this error. I've been doing this, doesn't seem to have an error elsewhere. Why is this showing an error?
Invalid parameter number: number of bound variables does not match number of tokens.
in model:
public function read()
{
$criteria = new CDbCriteria;
$criteria->compare('status_receiver',"N",true);
$data = new CActiveDataProvider($this, array('criteria'=>$criteria,));
return $data->getData();
}
public function defaultScope()
{
$id = Yii::app()->user->user_id;
return array(
'condition'=>"id ='".$id ."'",
'params' => array(':id' => $id ),
);
}
return array(
'condition'=>"id =:id",
'params' => array('id' => $id ),
);
return array(
'condition'=>"id = :id",
'params' => array(':id' => $id ),
);
I have relations in Yii:
public function relations() {
return array(
'applicant' => array(
self::BELONGS_TO,
'ApplicantProfile',
array('applicant_id' => 'id'),
),
'filePair' => array(
self::BELONGS_TO,
'DocumentCategoryFile',
array('file_pair_id' => 'id'),
'joinType' => 'inner join'
)
);
}
How i can create addCondition in CDbCriteria() for aplicant, for example i want do is_deleted = 0. I am tried it $criteria->with = array('applicant' => array('join'=>'left join','condition'=>'aplicant.is_deleted = 0')); but i have sql syntax error
Thanks
Try this -
$criteria = new CDbCriteria();
$criteria->with = array('applicant');
$criteria->together = true;
$criteria->addCondition('applicant.is_deleted=0');
You should refer the link here to learn how to use it.
i'm new in Yii I want to show new inserted entry at top in CGridView in Yii, but dont know how it is possible?
public function search()
{
$criteria=new CDbCriteria;
$criteria->alias = "jb";
$criteria->compare('title', $this->title, true);
$criteria->compare('notes', $this->notes, true);
$criteria->compare('createdon', $this->createdon, true);
$criteria->compare('expirydate', $this->expirydate, true);
$criteria->join= 'INNER JOIN company co ON (co.companyid=jb.companyid)';
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
Why don't you use relations for your join?
$criteria=new CDbCriteria;
$criteria->alias = "jb";
$criteria->compare('title', $this->title, true);
$criteria->compare('notes', $this->notes, true);
$criteria->compare('createdon', $this->createdon, true);
$criteria->compare('expirydate', $this->expirydate, true);
$criteria->with('company');
$criteria->together=>true;
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
and then defaultScope for jb model
public function defaultScope()
{
$alias = $this->getTableAlias(false,false);
return array(
'order'=>"`$alias`.`id` DESC",
);
}