Prevent Count query from executing in cakephp - php

is there is any way in cake php to prevent second query which is count from executing, my query log is something like this
array(
'log' => array(
(int) 0 => array(
'query' => 'SELECT DISTINCT `Medicine`.`Name`, `City`.`Name`, `Company`.`Name`, `Medicine`.`id`, `Type`.`Type` FROM `pharma`.`medicines` AS `Medicine` LEFT JOIN `pharma`.`companies` AS `Company` ON (`Company`.`id` = `Medicine`.`company_id`) LEFT JOIN `pharma`.`types` AS `Type` ON (`Type`.`id` = `Medicine`.`type_id`) LEFT JOIN `pharma`.`cities` AS `City` ON (`City`.`id` = `Company`.`city_id`) WHERE 1 = 1 LIMIT 30',
'params' => array(),
'affected' => (int) 30,
'numRows' => (int) 30,
'took' => (float) 2
),
(int) 1 => array(
'query' => 'SELECT COUNT(*) AS `count` FROM `pharma`.`medicines` AS `Medicine` WHERE 1 = 1',
'params' => array(),
'affected' => (int) 1,
'numRows' => (int) 1,
'took' => (float) 50
)
),
'count' => (int) 2,
'time' => (float) 52
)
I want to remove this (int) 2 "Count" query is this possible in cake php?

Related

Query count and group by in cakephp 2.6.8

In cakephp 2.6.8, I am trying to query a table named "mytable" in order to count rows having common values.
So, this is my query :
$zone = $this->MyModel->find('all', array(
'fields' => array('zone_id', 'COUNT(zone_id) as count'),
'group' => 'zone_id'
));
Cakephp returns this result :
zone => array(
0 => array(
MyModel => array("zone_id" = 1) ),
0 => array("count" = 77)
),
1 => array(
MyModel => array("zone_id" = 2) ),
0 => array("count" = 55)
),
2 => array(
MyModel => array("zone_id" = 3) ),
0 => array("count" = 23)
)
);
All I need is something like this :
zone = array(
0 => array("zone_id" => 1, "count" => 77),
1 => array("zone_id" => 2, "count" => 55),
2 => array("zone_id" => 3, "count" => 23)
);
How could I do to get this working without creating any Virtual Field?
Thank you for your help !

Cakephp show where 1=1

Hello I m creating simple blog site here view blog at landing page but if user choose any category the blog under this category only listed . My problem is when user select category from right sidebar it return all table records.
When i print the query it return :
array(
'log' => array(
(int) 0 => array(
'query' => 'SELECT `Category`.`category_id`, `Category`.`name` FROM `cakeBlog`.`categories` AS `Category` WHERE 1 = 1',
'params' => array(),
'affected' => (int) 13,
'numRows' => (int) 13,
'took' => (float) 0
),
(int) 1 => array(
'query' => 'SELECT `Post`.`id`, `Post`.`category_id`, `Post`.`title`, `Post`.`body`, `Post`.`created`, `Post`.`modified`, `Post`.`user_id` FROM `cakeBlog`.`posts` AS `Post` WHERE 1 = 1 LIMIT 20',
'params' => array(),
'affected' => (int) 13,
'numRows' => (int) 13,
'took' => (float) 0
)
),
'count' => (int) 2,
'time' => (float) 0
)
Here is my controller file
PostsController.php
In index function I will check whether category id exist or not?
Please help me...
Thanks
Use the following block where you want to fetch categories
if(is_null($categoryId)) {
$this->Paginator->setting = array(
'limit' =>'10',
'order' => array('Post.id' =>'Desc')
/*'conditions' => array(
'Post.user_id' => AuthComponent::user(id)
)*/
);
$arrPosts = $this->Paginator->paginate('Post');
$this->set('posts', $arrPosts);
} else {
$condition = array('Post.category_id' => $categoryId,'limit' =>'10');
$da = $this->Paginator->setting = array(
'conditions' => $condition
/*'conditions' => array(
'Post.user_id' => AuthComponent::user(id)
)*/
);
$arrPosts = $this->Paginator->paginate('Post');
$log = $this->Post->getDataSource()->getLog(false, false); debug($log);
$this->set('posts', $arrPosts);
}

How to select the root node using id, parent_id?

$users = $this->db->query("SELECT * FROM `users`)->rows();
$rootUser = $this->db->query("SELECT * FROM `users` WHERE id = 1")->row();
the output array is:
array(
[0] => array(
'id' => 1,
'login' => 'test1',
'parent_id' => 0
),
[1] => array(
'id' => 2,
'login' => 'test2',
'parent_id' => 1
),
[2] => array(
'id' => 3,
'login' => 'test3',
'parent_id' => 2
)
)
How can I select all the nodes starting from the root node, using php, without mysql joins beacuse they have limit as far as I know only 61 joins is allowed,
Can you explain your question little more?
Otherwise if you mean to select only root nodes, then you can use sql below:
SELECT * FROM `users` WHERE parent_id=0

CakePHP: Joining calculated fields from another model to a model

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(*)');

inserting an array

I have the following array that I want to insert into the table with the fields groupi_id, application_id and grant_id
the array is
array(
'ApplicationsGrant' => array(
'group_id' => array(
(int) 0 => '72',
(int) 1 => '72'
),
'application_id' => array(
(int) 0 => '1',
(int) 1 => '2'
),
'grant_id' => array(
(int) 0 => 56,
(int) 1 => 57
)
)
)
I want to insert the rows that every sub array goes with array key. so there will be 2 rows inserted in above case like this
insert into table (group_id. application_id, grant_id) Values (72, 1, 56);
insert into table (group_id. application_id, grant_id) Values (72, 2, 57);
howd I do that?
You can use Cake's Set::classicExtract() to pull out the values. I assume you know how to then save them to the DB.
http://book.cakephp.org/2.0/en/core-utility-libraries/set.html
In your case, something like (untested):
$result1 = Set::classicExtract($a, '{n}.{s}.{s}.0');
$result2 = Set::classicExtract($a, '{n}.{s}.{s}.1');
If you need the keys, you can extract those first:
$fields = Set::classicExtract($a, '{n}.{s}.{s}');
I have tried this one
$d = array(
'ApplicationsGrant' => array(
'group_id' => array(
(int) 0 => '72',
(int) 1 => '72'
),
'application_id' => array(
(int) 0 => '1',
(int) 1 => '2'
),
'grant_id' => array(
(int) 0 => 56,
(int) 1 => 57
)
)
);
for ($i=0; $i<count($d['ApplicationsGrant']['group_id']); $i++) {
echo $d['ApplicationsGrant']['group_id'][$i]."<br/>"; //outputs 72, 72
}

Categories