Order by using Sum Points in Yii? - yii

$resource_cnt = ShopVisituser::model()->findAll(array(
'select' => '*, sum(points) as amt',
'condition' => 'user_id=:user_id AND merchant_id=:merchant_id',
'order' => 'amt DESC',
'params' => array(':user_id' => $datas->user_id, ':merchant_id' => $datas->merchant_id))
);
I can't order by sum(points) as amt Desc.Why?how to order that Sum(points) in Yii.

Adding a function on your Model, for example:
public function scopes() {
return array(
'byamt' => array('order' => 'amt DESC'),
);
}
Now, you can use your query like this:
$resource_cnt = ShopVisituser::model()->byamt()->findAll(array(
'select' => '*, sum(points) as amt',
'condition' => 'user_id=:user_id AND merchant_id=:merchant_id',
'params' => array(':user_id' => $datas->user_id, ':merchant_id' => $datas->merchant_id))
);

Related

How to sub query a select into another select and put inside a variable with laravel?

I want to make a select that solves the fact that I have to perform two validations one for each table, what i'm doing now is validating the $table1 and validating the $table2 ,i just want to validate $table2
$table1 = DB::table('name_table1')
->where('invoice_id','{$table2}.id')
->get();
$table2 = DB::table('name_table2')
->select('name','invoices','{$table} as itens_of_invoice')
->get();
dd($table);
And return me something like;
Array (
[0] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
[1] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
[2] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
)
I don't find any solution if someone ever did this please help me

Yii cactivedataprovider count duplicates record and all record count

$criteria = new CDbCriteria;
$criteria->group = 't.blahah_id';
$result = new CActiveDataProvider('SocialnetworkSpams', array(
'criteria' => $criteria,
));
echo "<pre>";
print_r($result->getData());
Array
(
[0] => SocialnetworkSpams Object
(
[total] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[created_date] => 2015-12-04 03:50:47
[feedtype] => 1
[feedback] =>
[spam_status] => 1
[approveby_id] => 1
[blahah_id] => 1
[bunit_id] => 3
[spamby_id] => 1
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[1] => SocialnetworkSpams Object
(
[total] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 4
[created_date] => 2015-12-04 04:24:07
[feedtype] => 5
[feedback] => a
[spam_status] => 0
[approveby_id] =>
[blahah_id] => 2
[bunit_id] => 3
[spamby_id] => 1
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 4
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
I want duplicate record get using blahah id you can check bellow image:
Thanks
I have been resolved this issue.
$criteria = new CDbCriteria;
$criteria->select = '*, count(*) as total';
$criteria->group = 't.blahah_id';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
View Page :
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'spam-user-grid',
'itemsCssClass' => 'table table-striped table-bordered table-hover',
'dataProvider' => $model->search(),
'template' => "{items}{pager}",
'pager' => array(
'header' => '',
),
'columns' => array(
array(
'header' => 'Blahah',
'type' => 'raw',
'value' => 'ucfirst($data->ratingprofile->name)',
//'filter' => CHtml::textField('User[name]'),
),
array(
'header' => 'Total',
'type' => 'raw',
'value' => '$data->total',
),
),
)
));

not work scopes in Yii

Why my scopes not work?
$criteria = new \CDbCriteria();
$criteria->addCondition('user_id = :user_id');
$criteria->scopes = array(
'applicant' => array(
'scopes' => array('deletedStatus'),
'params' => array(':deletedStatus' => 0)
),
'filePair' => array(
'with' => array(
'category' => array(
'with' => array(
'parent' => array(
'order' => 'parent.order'
)
),
'scopes' => array('enabled')
),
'file' => array(
'scopes' => array('enabled'),
'order' => 'file.order'
)
)
)
);
$criteria->params = array(':user_id' => \Yii::app()->userApp->id);
/** #var \ApplicantDocument[] $models */
$models = \ApplicantDocument::model()->findAll($criteria);
public function scopes()
{
$t = $this->getTableAlias(false, false);
return array(
'deletedStatus' => array(
'condition' => $t.'.is_deleted = :deletedStatus'
),
'applicant' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_APPLICANT)
),
'guarant' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_GUARANTOR)
),
'company' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_COMPANY)
),
'trust' => array(
'condition' => $t.'.role = :role',
'params' => array(':role' => self::APPLICATION_STATUS_TRUST)
),
);
}
This scope does not work, aplicants with is_deleted selected from db
'applicant' => array(
'scopes' => array('deletedStatus'),
'params' => array(':deletedStatus' => 0)
),
Thanks for help!
Scopes not working, coz it call in ApplicantDocument, not in applicant. So i am try it solution and it help me.
$criteria->with = array('applicant');
$criteria->together = true;
$criteria->condition = "applicant.is_deleted = 0";

try to implement the join query in Cakephp

i am working on a cakephp and try to implement the join . or inner join query ... what i am doing right now is this
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'Message.user_id = Contact.user_id',
'Message.mobileNo = Contact.mobileNo'
)
)
)
), false);
return $message_details = $this->find('all', array(
'conditions' => array(),
'fields' => array('DISTINCT mobileNo')
));
this query is doing LEFT JOIN the table .. what i want is join or inner join between two tables
You can specify the type of join in your belongsTo configuration, as stated in the documentation. The default is left, but you can use any valid join type. Simply add 'type' => 'inner' to the configuration array, so you should get something like this:
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'Message.user_id = Contact.user_id',
'Message.mobileNo = Contact.mobileNo'
),
'type' => 'inner' // Simply add this
)
)
), false);
Alternatively, you can add the joins to your query without using bingModel:
return $message_details = $this->find('all', array(
'conditions' => array(),
'fields' => array('DISTINCT mobileNo'),
'joins'=>array(
array(
'table'=>'contacts,
'alias'=>'Contact',
'type'=>'INNER',
'conditions'=>array(
'Message.user_id = Contact.user_id',
'Message.mobileNo = Contact.mobileNo'
)
)
)
));

How to use CakePHP to do a query bounded by data in two indirectly-related tables

Presume the Query works:
SELECT
users.id,
subscriptions.name,
users.user_type
FROM users
LEFT JOIN users_subscriptions
ON users.id = users_subscriptions.user_id
LEFT JOIN subscriptions
ON users_subscriptions.subscription_id = subscriptions.id
WHERE
subscriptions.name = 'advertisers'
AND
users.user_type = 'agent';
How do I do this in a single Query using cakePHP 1.3?
EDIT: Are joins the right approach?
Yes joins are just fine. Just make sure that you add on the fly before searching to your User model a hasOne to UsersSubscription and to the UsersSubscription model as belongsTo Subscription. Then use the containable behavior to include 'UserSubscription' => 'Susbcription'
Your code might look like this:
$this->User->bindModel(array('hasOne' => array('UserSubscription')));
$this->User->UserSubscription->bindModel(array('belongsTo' => array('Subscription')));
$results = $this->User->find('all', array(
'contain' => array(
'UserSubscription' => 'Subscription'
)
));
You can set up the query the "CakePHP way" like this:
$conditions = array(
'Subscriptions.name' => 'advertisers',
'Users.user_type' => 'agent'
);
$joins = array(
array(
'table' => 'users_subscriptions',
'alias' => 'UsersSubscriptions',
'type' => 'LEFT',
'conditions' => array(
'UsersSubscriptions.user_id=Users.id'
)
),
array(
'table' => 'subscriptions',
'alias' => 'Subscriptions',
'type' => 'LEFT',
'conditions' => array(
'Subscriptions.id=UsersSubscriptions.subscription_id'
)
)
);
$fields = array(
'Users.id', 'Users.user_type', 'Subscriptions.name'
);
$options = array(
'conditions' => $conditions,
'joins' => $joins,
'fields' => $fields
);
$results = $this->Users->find('all', $options);