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.
Related
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'm using api soap v1 and calling catalog_product.list everytime I add the website_ids in filter It cause an error.
$filter = array(
'status' => array( '=' => 1 ),
'type_id' => array( '=' => 'simple' ),
'website_ids' => array('6'),
);
2nd question, the args of catalog_product.list is filter and storeView, if I add the store view id or code I display all the product I guess It ignores what I add on it.
$proxy->call($sessionId, 'catalog_product.list', $filter = null, '6');
Thanks
I found this solution.
//you're website's id
$result = $client->call($session, 'store.info', '6');
$code = $result['code'];
//here all filters, in 'filters' you can add others filters (like price for example)
$filters = array(
'filters' => array(
'status' => 1,
'type_id' => 'simple',
),
'storeView' => "$code"
);
try{
$result = $client->call($session, 'catalog_product.list',$filters);
} catch (Exception $e){
print_r($e);
}
print_r($result);
now it works? :)
Model Search Method
$criteria->alias = 'c';
$criteria->select = 'c.*,max(ca.date) AS lastactivity';
$criteria->join = 'LEFT JOIN tbl_contact_action AS ca ON (ca.contact_id=c.contact_id)';
$criteria->condition = 'c.status<>"Deleted"';
$criteria->group = 'c.contact_id';
$criteria->order = 'lastactivity DESC';
$sort = new CSort;
$sort->defaultOrder = array('lastactivity' => CSort::SORT_DESC); //'name ASC';
$sort->attributes = array(
'name' => 'name',
'email' => 'email',
'status' => 'status',
'createdon' => 'createdon',
'lastactivity' => 'lastactivity',
);
$sort->applyOrder($criteria);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => $sort,
));
Basically, I have a 1:n relationship where in I need only latest record from child table. The parent table data will be displayed based on the comment that is done latest in child table. How to make this field sortable ?
Error
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'c.lastactivity' in 'order clause'.
Just a theory:
because you have a join with together sql, all the data will be together in 1 result. I am not sure if you can still use $data->ca->date because your data is not a known active record type.
Try putting
$criteria->select = 'maintable.*,ca.date as ca_date';
then you should be able to use
array(
'header' => 'Last Activity',
'class' => 'gridDataColumn',
'value' => '$data->ca_date',
),
Below is the Model which will allow to have a custom/computational field that will be SORTABLE. Instead of simply writing 'lastactivity'=>'lastactivity' in sort array, passing whole array did the trick for me. Hope it helps someone :)
class Contact extends CActiveRecord {
public $verifyCode;
public $lastactivity;
public static function model($className = __CLASS__) {
return parent::model($className);
}
public function tableName() {
return '{{contact}}';
}
public function rules() {
return array(
array('name, email, subject, message', 'required', 'message' => Yii::t('app', 'MSG_ATTRIBUTE_BLANK')),
array('email', 'email'),
array('verifyCode', 'CaptchaExtendedValidator', 'allowEmpty' => !CCaptcha::checkRequirements(), 'on' => 'fContact'),
array('name, email,subject,message,lastactivity', 'safe', 'on' => 'search'),
array('name, subject, email, message, status,createdon,updatedon,verifyCode,lastactivity', 'safe'),
);
}
public function relations() {
return array(
'ca' => array(self::HAS_MANY, 'ContactAction', 'contact_id'),
);
}
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('name', CommonFunctions::escapeOperator($this->name), true, 'OR');
$criteria->compare('subject', CommonFunctions::escapeOperator($this->subject), true, 'OR');
$criteria->compare('email', CommonFunctions::escapeOperator($this->email), true, 'OR');
$criteria->compare('status', CommonFunctions::escapeOperator($this->status), true,'OR');
$lastactivity_sql = '(select max(date) from tbl_contact_action ca where ca.contact_id=t.contact_id)';
$criteria->select = array('*', $lastactivity_sql . ' as lastactivity');
$criteria->addCondition('status<>"Deleted"');
$criteria->compare($lastactivity_sql, $this->lastactivity);
$sort = new CSort;
$sort->defaultOrder = array('lastactivity' => CSort::SORT_DESC); //'title ASC';
$sort->attributes = array(
'name' => 'name',
'email' => 'email',
'status' => 'status',
'createdon' => 'createdon',
'lastactivity' => array(
'asc' => 'lastactivity ASC',
'desc' => 'lastactivity DESC',
),
);
$sort->applyOrder($criteria);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => $sort,
'pagination' => array(
'pageSize' => Yii::app()->user->getState('contactPageSize', Yii::app()->params['RECORDPERPAGE_ADMIN']),
'currentPage' => Yii::app()->user->getState('Contact_page', 0),
),
));
}
}
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 ),
);
My Clistview Pagination is not working properly.I tried every thing but still not getting success.Here is my model function:
public function getallone()
{
$criteria = new CDbCriteria;
$criteria->select = "t.id";
$criteria->condition = "t.featured=1 AND t.status = 1";
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination' => array('pageSize' => 25),
));
}
Am still getting only 10 items per page but i want 25.I tried to 2 items but its also not working. Whats wrong with that?Didnt find.
And this is my view code:
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$model->getallone(),
'itemView'=>'_allone',
'enablePagination' => true,
)); ?>
You shoud try this for better pagination option
$condition='type=:type';
$dataArray[':type']=$type;
$dataProvider=new CActiveDataProvider('modelname', array(
'criteria'=>array(
'select'=>'*',
'condition'=>$condition,
'params'=>$dataArray,
'order'=>'id DESC',
'offset'=>($page-1)*$limit,
'limit'=>$limit,
),
'pagination'=>array(
'pageSize'=>$limit,
),
));
$totalActivity= $dataProvider->getTotalItemCount();
http://www.yiiframework.com/doc/api/1.1/CActiveDataProvider for reference
try and use CArrayDataProvider
here is how do it:
public function actionIndex() {
$command = Yii::app()->db->createCommand("
SELECT u.id as u_id, u.link as u_link, u.title as u_title, u.description as u_description, u.public as u_public, u.created as u_created, u.status as u_status, w.id as w_id, w.domain as w_domain, w.status as w_status
from url as u
left join website as w
on w.id = u.website_id
where u.title!='null' and u.status = 1 and w.status = 1 and u.public = 1;
");
$news = $command->queryAll();
$dataProvider = new CArrayDataProvider($news, array(
'id' => 'user',
'keyField' => 'u_id',
'sort' => array(
'defaultOrder' => 'u_id desc',
),
'pagination' => array(
'pageSize' => 10,
),
));
$model_website = Website::model()->findAllByAttributes(array('status' => 1), array('limit' => 30, 'order' => 'id desc'));
$this->render('index', array(
'model_website' => $model_website,
'dataProvider' => $dataProvider,
));
}
and the clistview:
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'template' => "{summary}\n{pager}\n{items}\n{summary}\n{pager}",
'itemView' => '_index',
'pager' => array(
'maxButtonCount' => 10,
),
)
);