CakePHP: Joining calculated fields from another model to a model - php

I've been trying to figure out how to add some computed fields to a model in CakePHP. I'm able to achieve the desired result with the following query:
SELECT project_id,
COUNT(*) AS clicks_total,
SUM(CASE WHEN clicks.redirect_url = projects.url THEN 1 ELSE 0 END) AS clicks_redirected,
SUM(CASE WHEN clicks.redirect_url = projects.url THEN 0 ELSE 1 END) AS clicks_not_redirected
FROM clicks
LEFT JOIN projects ON clicks.project_id = projects.id
GROUP BY project_id
If I attempt to execute it as a custom query Cake transforms the result in such a way that it would require much array manipulation to be usable. I tried to do it the Cake way with the following code, but for some reason the calculated fields end up in a separate array which causes strange behavior in the view:
$this->paginate = array(
'Project' => array(
'fields' => array(
'id', 'name', 'url', 'url_mac', 'url_mobile',
'COUNT(*) AS clicks_total',
'SUM(CASE WHEN Click.redirect_url = Project.url THEN 1 ELSE 0 END) AS clicks_redirected',
'SUM(CASE WHEN Click.redirect_url = Project.url THEN 0 ELSE 1 END) AS clicks_not_redirected'
),
'joins' => array(
array(
'table' => 'clicks',
'alias' => 'Click',
'type' => 'LEFT',
'conditions' => array('Click.project_id = Project.id')
)
),
'group' => 'project_id'
));
$this->set('projects', $this->paginate());
Produces the following result:
array(
(int) 0 => array(
'Project' => array(
'id' => '508705c8-126c-48f9-bd9a-6d79d13bb9ea',
'name' => 'Test Project',
'url' => 'http://www.test.com',
'url_mac' => 'http://www.mac.com',
'url_mobile' => 'http://www.mobile.com'
),
(int) 0 => array(
'clicks_total' => '80',
'clicks_redirected' => '35',
'clicks_not_redirected' => '45'
)
),
(int) 1 => array(
'Project' => array(
'id' => '508b1073-2aa8-4895-b8d9-152ed13bb9ea',
'name' => 'Another Project',
'url' => 'http://another.com',
'url_mac' => 'http://anothermac.com',
'url_mobile' => 'http://anothermobile.com'
),
(int) 0 => array(
'clicks_total' => '134',
'clicks_redirected' => '70',
'clicks_not_redirected' => '64'
)
)
)
Does anyone have any ideas for getting the calculated click counts to show up under the Project array?

You can add virtual fields to your model:
Check it out: http://book.cakephp.org/1.3/view/1608/Virtual-fields
In Project model:
public $virtualFields = array('clicks_total' => 'COUNT(*)');

Related

saveall on habtm creates duplicate new rows

I have a simple HABTM with a lessons/Students table. i simply create a new lesson with an existing student via a while loop , so I should get 2 new rows. I am testing this function. What I get is 4 new rows in the lesson table instead of 2. I have no idea why the extra 2 rows are created as they are a duplicates of the 2 new rows which are outputted in debug. Everything is correctly saved like the entries in join lesson/student table and all the FK exist.
Just to add confusion, sometimes the same code produces the desired 2 rows . This is unstable so I am doing something wrong. I followed the array setup for habtm as in the manual for saves.
http://book.cakephp.org/2.0/en/models/saving-your-data.html
private function book_lessons($lesson=null) {
// debug( $lesson);
$i=0;
while ($i<2)
{
$date=date('Y-m-d');
$data[$i]=array();
$this->Lesson->create();
$data[$i]['Lesson']['lesson_date']= $date;
$data[$i]['Lesson']['start_time']= $lesson['Lesson']['start_time'];
$data[$i]['Lesson']['end_time']=$lesson['Lesson']['end_time'];
$data[$i]['Lesson']['schedule_rec']= 1;
$data[$i]['Lesson']['subject_id']= $lesson['Lesson']['subject_id'];
$data[$i]['Lesson']['tutoring_type_id']= 1;
$data[$i]['Lesson']['tutor_id']= $lesson['Lesson']['tutor_id'];
$data[$i]['Lesson']['subject_id']= $lesson['Lesson']['subject_id'];
$data[$i]['Lesson']['term_id']= $lesson['Lesson']['term_id'];
$data[$i]['Student']['id']=$lesson['Student']['id'];
$i=$i+1;
}
$this->Lesson->saveAll($data);
public $hasAndBelongsToMany = array(
'Student' => array(
'className' => 'Student',
'joinTable' => 'lessons_students',
'foreignKey' => 'lesson_id',
'associationForeignKey' => 'student_id',
'unique' => 'keepExisting',
)
);
array(
(int) 0 => array(
'Lesson' => array(
'lesson_date' => '2015-06-11',
'start_time' => '16:00:00',
'end_time' => '17:00:00',
'schedule_rec' => (int) 1,
'subject_id' => '16',
'tutoring_type_id' => (int) 1,
'tutor_id' => '12',
'term_id' => '10'
),
'Student' => array(
'id' => '206'
)
),
(int) 1 => array(
'Lesson' => array(
'lesson_date' => '2015-06-11',
'start_time' => '16:00:00',
'end_time' => '17:00:00',
'schedule_rec' => (int) 1,
'subject_id' => '16',
'tutoring_type_id' => (int) 1,
'tutor_id' => '12',
'term_id' => '10'
),
'Student' => array(
'id' => '206'
)
)
)
Move the $this->Lesson->create(); out of the loop. Cake will still save multiple records when you have only in create() method.

Cakephp: How to get data with specific fields on associated model

I'm using cakephp framework. I have two tables on my DB, courses and modules table. Those tables are related, courses has many modules. Basically I want to get data on courses and on modules but with specific fields. I want to get just the id and the title of the course and the id and the title of the module.
This code:
$courses_taken = $this->Course->find('all', array(
'conditions' => array('Course.id' => $course_id_list),
'fields' => array('Course.id', 'Course.title')
));
gives me:
array(
(int) 0 => array(
'Course' => array(
'id' => '1',
'title' => 'course 1'
),
'Module' => array(
(int) 0 => array(
'id' => '1',
'course_id' => '1',
'title' => 'module 1',
'image' => null,
'content' => 'Lorem ipsum',
'voice_over' => null,
'created' => '2014-09-03 14:02:25',
'modified' => '2014-09-03 14:02:28'
),
(int) 1 => array(
'id' => '2',
'course_id' => '1',
'title' => 'module 2',
'image' => null,
'content' => 'Sasdas',
'voice_over' => null,
'created' => null,
'modified' => null
)
)
),
(int) 1 => array(
'Course' => array(
'id' => '2',
'title' => 'course 2'
),
'Module' => array()
)
)
But I wanna select a specific field for module as well so I tried this code:
$courses_taken = $this->Course->find('all', array(
'conditions' => array('Course.id' => $course_id_list),
'fields' => array('Course.id', 'Course.title', 'Module.id', 'Module.title')
));
but gives me an error: Column not found: 1054 Unknown column 'Module.id' in 'field list'.
Please help me guys thanks.
Got the answer, To make sgt's answer work, $actsAs property should be added in Course model. Reference : http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
Either you use some behavior like Containable and your find() will look like this :
$courses_taken = $this->Course->find('all', array(
'conditions' => array('Course.id' => $course_id_list),
'fields' => array('Course.id', 'Course.title'),
'contain' => array(
'Module' => array(
'fields' => array('Module.id', 'Module.title'),
),
),
));
try this -
$courses_taken = $this->Course->find('all', array(
'conditions' => array('Course.id' => $course_id_list),
'fields' => array('Course.id', 'Course.title'),
'contain' => array('Module.id', 'Module.title')
));

Sorting CSqlDataProvider in Yii

I have a rather complex query, that I'm using CSqlDataProvider with:
$sql = "
SELECT c.id AS id,
c.enabled AS enabled,
s.store_name AS store_name,
s.store_url AS store_url,
c.verified AS verified,
r.rating_total AS rating
FROM compete_settings c
LEFT JOIN stores s
ON c.compete_id = s.id
LEFT JOIN (
(SELECT store_id, rating_total FROM amazon_sellers)
UNION
(SELECT store_id, rating_total FROM ebay_sellers)
) AS r
ON c.compete_id = r.store_id
WHERE c.store_id = :store_id
";
$itemCount = Yii::app()->db->createCommand("SELECT COUNT(id) FROM compete_settings WHERE store_id = $store_id")->queryScalar();
return new CSqlDataProvider($sql, array(
'totalItemCount' => $itemCount,
'params' => array(
'store_id' => $store_id,
),
'sort' => array(
'attributes' => array ( 'enabled, store_name, rating' ),
'defaultOrder' => array('store_name'=>false)
),
'pagination' => array('pageSize' => Yii::app()->user->getState('pageSize_cs', Yii::app()->params['defaultPageSize']),),
));
The query works great. However, the columns are not sortable.
I've made sure to follow Yii CGridview sorting with CSqlDataProvider, but they are still not clickable column headers.
Here are my column arrays:
'columns' => array(
array(
'header' => 'Enabled',
'type'=>'raw',
'name'=>'enabled',
'htmlOptions'=>array('class'=>'enabled-column'),
'value' => 'CHtml::checkbox("", $data["enabled"], array("id"=>"c{$data[\'id\']}", "class"=>"enable-switch"))',
),
array(
'header' => 'Name',
'htmlOptions' => array('class' => 'account-name-column'),
'type' => 'raw',
'name'=>'store_name',
),
array(
'header' => 'Store Rating',
'name'=>'rating',
),
),
Help? ):
Turns out unlike what was mentioned in the question I followed, Yii accepts the attributes param as an array of values and not an array with one CSV value.
So:
'sort' => array(
'attributes' => array ( 'enabled', 'store_name', 'rating' ),
'defaultOrder' => array('store_name'=>false)
),
And not:
'sort' => array(
'attributes' => array ( 'enabled, store_name, rating' ),
'defaultOrder' => array('store_name'=>false)
),

Using Containable behavior to filter results in Cakephp 2.2.4

I'm using containable behavior and the result of my find('all') is:
array(
(int) 0 => array(
'User' => array(
'id' => '106',
'email' => 'daje#daje.it',
'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
'role_id' => '3',
'active' => '1'
),
'Lead' => array(
'id' => '6'
),
'Estimate' => array(
(int) 0 => array(
'lead_id' => '6',
'Estimate' => array(
(int) 0 => array(
'TOT_count' => '2'
)
)
)
)
)
)
I need to to count how many estimates there are in the lead.
The total (2) is correct, but i see nested 'Estimated' array, why ?
The result i would like to get is:
array(
(int) 0 => array(
'User' => array(
'id' => '106',
'email' => 'daje#daje.it',
'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
'role_id' => '3',
'active' => '1'
),
'Lead' => array(
'id' => '6'
),
'Estimate' => array(
'TOT_count' => '2'
)
)
)
This is the find:
$options = array(
'contain' => array(
'User',
'Estimate' => array(
'fields' => 'COUNT(*) AS TOT_count'
)
),
'conditions' => array('Lead.id' => 6),
'fields' => 'User.*',
'limit' => 1
);
debug($this->Lead->find('all', $options));
How can i do it?
Thanks!
When you use a "custom" AS statement, in your case TOT_count, Cake will always put this in a result key called 0. You can avoid this by defining TOT_count as a virtualField in your model. That way it will be nested directly under the model name in your resultset.
Secondly, the lead_id is forcedly retrieved, because it is "needed" to make the join with the Lead model. It can not properly retrieve all the data without that piece of information there.

CakePHP: joins inside contain

As the title says, I'm having troubles with joins in my find-query, with errors appearing like:
preg_match() expects parameter 2 to be string, array given [CORE/cake/libs/model/behaviors/containable.php, line 301]
I've tried adding joins to the array over there, but it didn't change anything.
These are the options I'm passing to the find method.
$options = array(
'contain' => array(
'Answer' => array(
'conditions' => array('Answer.type' => 'answer'),
'joins' => array(
$this->votesJoin()
),
'Comment' => array(
'conditions' => array('Comment.type' => 'comment'),
'joins' => array(
$this->votesJoin()
)
)
),
'Comment' => array(
'conditions' => array('Comment.type' => 'comment'),
'joins' => array(
$this->votesJoin()
)
),
'User',
'Tag' => array()
),
'joins' => array(
$this->votesJoin()
),
'conditions' => array(
'Question.id' => $id
)
);
return $this->find('first', $options);
with votesJoin() returning the following array.
(
[table] => (SELECT Vote.node_id, SUM(value) as total FROM votes AS `Vote` WHERE 1 = 1 GROUP BY `Vote`.`node_id` )
[alias] => Vote
[conditions] => Vote.node_id = Question.id
)
What I'm trying to do:
Each user can up/downvote a node (question/answer/comment). With the join I'm trying to add the sum of those votes.
database http://github.com/navale/QA/wiki/img/datamodel.png
You should use "joins" only for things that can't be done with Cake model relationship and Containable. I don't know the details of your database, but I think the find operation can be simplified. Why don't you post the schema for these tables on here?
try this:
$options = array(
'contain' => array(
'Answer' => array(
'conditions' => array('Answer.type' => 'answer'),
'Vote' => array(
'fields' => array('SUM(Vote.value)'),
'group' => array('Vote.parent_id')
),
'Comment' => array(
'conditions' => array('Comment.type' => 'comment'),
'Vote' => array(
'fields' => array('SUM(Vote.value)'),
'group' => array('Vote.parent_id')
)
)
),
'Comment' => array(
'conditions' => array('Comment.type' => 'comment'),
'Vote' => array(
'fields' => array('SUM(Vote.value)'),
'group' => array('Vote.parent_id')
)
),
'User',
'Tag' => array()
),
'conditions' => array(
'Question.id' => $id
)
);
You get the Vote sum value for each answer, comment, and comment for answer. (You might need to add 'hasMany' Vote in the Node model if you haven't done that yet)
If instead you want to get one single total sum of Vote for the question, then I'd suggest:
get the list of the answers and comments of the question:
$lvl1 = find('list','fields'=>array('id'),'conditions'=>array('Node.parent_id'=>$id))
then get list of the comments of the answers
$lvl2 = find('list','fields'=>array('id'),'conditions'=>array('Node.parent_id'=>$lvl1))
then just combine the 2 array then do a sum over that.

Categories