yii1 CGridview with custom SQL - mysql

i need to use yii1 CGridView with the custom SQL.
i have followed this article
inside model I've created
public function searchUserStats()
{
$count = Yii::app()->db->createCommand()
->select('count(DISTINCT user_id)')
->from('casino_sg_sessions')
->queryScalar();
$sql = '
SELECT
SUM(bet) bet_sum,
SUM(win) win_sum,
SUM(bet_count) bet_count,
user_id,
userName,
modified
FROM
(SELECT
(SELECT IFNULL(SUM(tr1.amount), 0) FROM casino_sg_transactions tr1 WHERE t.id = tr1.session_id AND tr1.action = \'bet\' ) AS bet,
(SELECT IFNULL(SUM(tr2.amount), 0) FROM casino_sg_transactions tr2 WHERE t.id = tr2.session_id AND tr2.action = \'win\' ) AS win,
(SELECT count(0) FROM casino_sg_transactions tr3 WHERE t.id = tr3.session_id) AS bet_count,
t.user_id,
u.userName,
t.modified
FROM `casino_sg_sessions` t
INNER JOIN user u ON u.id = t.user_id
ORDER BY modified DESC
) user_stats
GROUP BY user_id
ORDER BY modified DESC
';
$command = Yii::app()->db->createCommand($sql);
$dataProvider = new CSqlDataProvider($command, array(
'totalItemCount'=>$count,
'pagination'=>array(
'pageSize'=>15,
),
));
return $dataProvider;
}
in the controller:
$model = new CasinoSgSessions('searchUserStats');
$this->render('index', array(
'model' => $model,
));
inside the view i call it this way:
$this->widget('application.widgets.grid.CGridView', array(
'dataProvider' => $model->searchUserStats(),
'filter' => $model,
'columns' => array(
[
'header' => 'User ID',
'name' => 'user_id',
'value' => '$data->user_id',
'htmlOptions' => array('width'=>'150px'),
],
.....
.....
i receive paginated grid without data.
spent huge amount of time and now gave up, what i did wrong ?
sql query itself works good.
PS: i cant use mysql views and create model for the view.
PPS: query will be a bit more complex, so the only way i see is using CGridView through the sql query.

in the template instead of
$data->user_id
i had to use
$data['user_id']
absolutely not expected behavior. Seems to me they didn't hear about SOLID

Related

Wordpresss - WP_query count

I need to count how many times my date recorded in the meta key:metakey_AMC_data, in format (d-m-Y) it is contained in the database by comparing it with the current date
$mostra_data_corrente = date('d-m-Y');
$query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')");
$conta_risultati = count($query);
and this I can do perfectly.but now my need is to execute the first query by linking another AND, and specify when the term slug is equal to the category of the event (terms taxonomy), obviously the query is incorrect
SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')
AND(slug = 'aperitivi') "
how can i do this?
You can get that count as well. You need to modify query (code) like follow:
$qry = array(
'post_type' => 'post', // mention your post type to narrow down searching through postmeta table
'meta_query' => array(
array(
'meta_key' => 'metakey_AMC_data',
'meta_value' => $mostra_data_corrente,
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy' => 'nameoftaxonomy', // Write the name of taxonomy that you have assinged while you created a CPT (custom post type)
'field' => 'slug',
'terms' => 'aperitivi',
)
)
)
$the_query = WP_Query($qry);
echo $the_query->post_count;
You have to make some necessary changes in above code to suite your requirements. I've added comment where you have to do changes.

Zend Framework 2 SQL Join Issue

I am trying to use two left outer joins with Zend Framework 2's SQL classes but for some reason it is not returning one result but the other one is working fine. I've ran the actual SQL in MySQL Workbench and it returns just like I want but it is not doing it with Zend Framework. Here is my code:
Pure SQL:
SELECT groups.group_name, members.username, groups.id FROM groups
LEFT OUTER JOIN group_admins ON groups.id = group_admins.group_id
LEFT OUTER JOIN members ON group_admins.user_id = members.id
WHERE group_admins.user_id = " . parent::getUserId()['id']
This returns the result I wish, (which can be seen here: http://imgur.com/8ydmn4f)
Now, here is the Zend Framework 2 code I have in place:
$select_admins = new Select();
$select_admins->from(array(
'g' => 'groups',
))
->join(array(
'ga' => 'group_admins'
), 'g.id = ga.group_id')
->join(array(
'm' => 'members'
), 'ga.user_id = m.id', array('username'))
->where(array('ga.user_id' => parent::getUserId()['id']));
$query_group_admin = parent::$sql->getAdapter()->query(parent::$sql->buildSqlString($select_admins), Adapter::QUERY_MODE_EXECUTE);
$group_admins = array();
foreach ($query_group_admin as $group_admin) {
$group_admins[] = $group_admin;
}
// get the group members
$select = new Select();
$select->from(array(
'g' => 'group_members'
))
->join(array(
'm' => 'members'
), 'g.member_id = m.id')
->join(array(
'grp' => 'groups'
), 'g.group_id = grp.id')
->where(array(
'g.group_id' => $group_id
));
$query = parent::$sql->getAdapter()->query(parent::$sql->buildSqlString($select), Adapter::QUERY_MODE_EXECUTE);
$member_username = array();
foreach ($query as $member) {
$member_username[] = $member['username'];
}
// get the rest of the group info
$fetch = $this->gateway->select(array(
'id' => $group_id
));
$row = $fetch->current();
if (!$row) {
return false;
}
return array(
'admins' => implode(", ", $group_admins),
'members' => implode(", ", $member_username),
'info' => $row
);
Controller:
public function grouphomeAction()
{
$id = $this->params()->fromRoute('id', 0);
if (0 === $id) {
return $this->redirect()->toRoute('members/groups', array('action' => 'index'));
}
if (!$this->getGroupsService()->getGroupInformation($id)) {
return $this->redirect()->toRoute('members/groups', array('action' => 'index'));
}
return new ViewModel(array('group_info' => $this->getGroupsService()->getGroupInformation($id)));
}
However, this only shows the group name, group creator and group members but leave the group admins field empty.
Here is the print_r result of the array returned:
Array ( [admins] => [members] => jimmysole, fooboy [info] => ArrayObject Object ( [storage:ArrayObject:private] => Array ( [id] => 2 [group_name] => Tim's Group [group_creator] => timlinden [group_created_date] => 2017-01-16 17:39:56 ) ) )
If it helps, here is a screenshot as well of the page - http://imgur.com/xUQMaUu
Any help would be appreciated!
Thanks.
Basically your joins are INNER JOINS...I know....you must hate Zend right now :p . By default they are INNER JOINS so i assume that is what is wrong. SO try to specify the type of join and you should be fine. You can find more examples here: examples
$select12->from('foo')->join('zac', 'm = n', array('bar', 'baz'), Select::JOIN_OUTER);

cakephp 3.x Sub select to get a count value

after some research I created this find statement, but it doesn't work as expected:
public $virtualFields = array(
'count' => "SELECT COUNT(plans_trainings.id) FROM plans_trainings, trainings, projects WHERE plans_trainings.training_id = trainings.id AND projects.location_id = locations.id AND plans_trainings.project_id = projects.id"
);
public function trainingsPerLocationChartData(){
$this->loadModel('Locations');
$locationsDiagramData = $this->Locations->find('all', array(
'fields' => array(
'locations.id',
'locations.description',
'locations.count' /* virtuel field */
)
));
The statement will look like this:
'sql' => 'SELECT locations.id AS `locations__id`, locations.description AS `locations__description`, locations.count AS `locations__count` FROM locations Locations'
The virtual field is not recognized at all.
What do I do wrong?
UPDATE:
I found out that I have to call it like this:
'count' => "(SELECT COUNT(plans_trainings.id) FROM plans_trainings, trainings, projects WHERE plans_trainings.training_id = trainings.id AND projects.location_id = locations.id AND plans_trainings.project_id = projects.id) AS `counter`"
I added () around the select.
$locationsDiagramData = $this->Locations->find('all')
->select(['locations.id', 'locations.description', $this->virtualFields['count']]);
But I still get a wrong sql syntax:
SELECT locations.id AS `locations__id`, locations.description AS `locations__description`, (SELECT COUNT(plans_trainings.id) FROM plans_trainings, trainings, projects WHERE plans_trainings.training_id = trainings.id AND projects.location_id = locations.id AND plans_trainings.project_id = projects.id) AS `counter` AS (SELECT COUNT(plans_trainings__id) FROM plans_trainings, trainings, projects WHERE plans_trainings FROM locations Locations
The "last" problem seem only this part after AS counter:
AS (SELECT COUNT(plans_trainings__id) FROM plans_trainings, trainings,
projects WHERE plans_trainings
This is what the statement breaks!
Any ideas how to solve that?
If I run the statement in phpmyadmin and remove this mentioned part, the statement works fine and brings the right results!
UPDATE 2:
this is the plain statement:
select
locations.id,
locations.description,
(
select
count(plans_trainings.id)
from
plans_trainings,
trainings,
projects
where
plans_trainings.training_id = trainings.id
and projects.location_id = locations.id
and plans_trainings.project_id = projects.id
) as 'Anzahl'
from
locations;
try this in controller
$this->loadModel('Locations');
$this->loadModel('PlansTrainings');
$this->loadModel('Trainings');
$this->loadModel('Projects');
$sub_query=$this->PlansTrainings->find()->select(['cnt1'=>'count(PlansTrainings.id)'])->join([
[
'table' => 'trainings',
'alias' => 'Trainings',
'type' => 'INNER',
'conditions' => 'PlansTrainings.training_id=Trainings.id',
],
[
'table' => 'projects',
'alias' => 'Projects',
'type' => 'INNER', //LEFT, RIGHT...
'conditions' => ['Projects.location_id=PlansTrainings.id','PlansTrainings.project_id=Projects.id'],
],
]);
$locationsDiagramData = $this->Locations->find()->select(['id', 'description','count'=>$sub_query]);

Message list: order by and group by query on cakephp and mysql

I want to change my message list page/inbox. Currently it shows all the messages from the DB like if someone send me a message 3 times, it will all appear in my inbox. Now i want to make it simple like every sender will appear only once in the list and will show the latest message that he/she sent.
This is my code in cakephp:
$this->paginate = array(
'Message'=> array(
'conditions'=>array('Message.recipient_id'=>$this->user_id),
'group' => array('Message.sender_id'),
'order'=>'message_sent_date DESC',
'limit'=> 20,
)
);
The problem with this code is it shows the oldest message content. I think it's because group by always first before order by.
I was able to do it on mysql query but i don't know how to make it on cakephp.
Here's my query:
SELECT *
FROM(
SELECT *
FROM messages WHERE recipient_id = 114
ORDER BY message_viewed_date DESC
) AS messages
GROUP BY sender_id
ORDER BY message_viewed_date DESC
Please help me with this.. Thanks.
This should do the trick (make sure your models are set up correctly):
$results = $this->Message->find('all', array(
'conditions' => array(
'recipient_id' => 114
),
'group' => 'sender_id',
'order' => 'message_viewed_date DESC'
));
No need for the sub query there, it's not doing anything fancy. The manual will help you with queries like this.
I've solve this problem using the Model::query() http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-query
here's my code:
$messages = $this->Message->query("SELECT id FROM(SELECT * FROM messages WHERE recepient_id = ". $this->user_id ." ORDER BY message_sent_date DESC) AS Message GROUP BY sender_id ORDER BY message_sent_date DESC");
for($i=0; $i<count($messages); $i++){
$message_id[$i] = $messages[$i]['Message']['id'];
}
$this->paginate = array(
'Message'=> array(
'conditions'=>array('Message.id' => $message_id, 'Message.recepient_id'=>$this->user_id, 'Message.hidden_on_to'=>false),
'limit'=>20,
'order'=>array('Message.message_sent_date' => 'DESC'),
'recursive'=>0,
'fields'=>array('Message.id', 'Message.message_content', 'Message.message_sent_date', 'Message.message_viewed_date', 'Sender.id', 'Sender.username')
)
);

Zend Framework 2: LEFT JOIN issue

public function getInterests($userID) {
$result = $this->tableGateway->select(function (Select $select) use ($userID) {
$select->join('interests', 'users_interests.interest_id = interests.interest_id', array('*'), 'left');
$where = new Where();
$where->equalTo('user_id', $userID);
$select->where($where);
});
return $result;
}
Here is my method. It simply selects all records from users_interests with user_id = $userID and joins the 'interests' table. So far, so good, but when trying to display the fetched results, the fields from the joined table just do not exist. Here is the dump of the $result:
Zend\Db\ResultSet\ResultSet Object
(
[allowedReturnTypes:protected] => Array
(
[0] => arrayobject
[1] => array
)
[arrayObjectPrototype:protected] => Object\Model\UsersInterests Object
(
[settings_id] =>
[user_id] =>
[interest_id] =>
)
[returnType:protected] => arrayobject
[buffer:protected] =>
[count:protected] => 2
[dataSource:protected] => Zend\Db\Adapter\Driver\Pdo\Result Object
(
[statementMode:protected] => forward
[resource:protected] => PDOStatement Object
(
[queryString] => SELECT `users_interests`.*, `interests`.* FROM `users_interests` LEFT JOIN `interests` ON `users_interests`.`interest_id` = `interests`.`interest_id` WHERE `user_id` = :where1
)
[options:protected] =>
[currentComplete:protected] =>
[currentData:protected] =>
[position:protected] => -1
[generatedValue:protected] => 0
[rowCount:protected] => 2
)
[fieldCount:protected] => 6
[position:protected] =>
)
I badly need help on this because I am supposed to finish my project until Sunday. Thanks in advance.
You can use the following to apply left join. $select::JOIN_LEFT instead of 'left'.
public function getInterests($userID) {
$result = $this->tableGateway->select(function (Select $select) use ($userID) {
$select->join('interests', 'users_interests.interest_id = interests.interest_id', array('*'), $select::JOIN_LEFT);
$where = new Where();
$where->equalTo('user_id', $userID);
$select->where($where);
});
return $result;
}
It seems you have a problem in the WHERE clause of the join. This also shows in the error here:
[queryString] => SELECT `users_interests`.*, `interests`.* FROM `users_interests` LEFT JOIN .
`interests` ON `users_interests`.`interest_id` = `interests`.`interest_id`
WHERE `user_id` = :where1
Try this:
$select->from($this->table)
->join('interests', 'users_interests.interest_id = interests.interest_id',
array('*'), 'left');
$where = new Where();
$where->equalTo('user_id', $userID) ;
$select->where($where);
I can not follow your code completely, like here:
$this->tableGateway->select(function (Select $select) use ($userID) {
But, here is a very nice article on this. I think, you can simplify your code a little.
Have you iterated over the resultset? You can see there's two matching rows:
[rowCount:protected] => 2
You have a ResultSet object, but it will not load any of the rows until requested, they are "lazy loaded" when you iterate over the object.
You can force the resultset to get them all for you:
var_dump($resultSet->toArray()); // force load all rows
or iterate over the ResultSet:
foreach($resultset as $row) {
var_dump($row); // each row loaded on request
}
I have written about this before and maybe it will help you as well.
TableGateway with multiple FROM tables