Cakephp show where 1=1 - php

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);
}

Related

CakePHP model->save() not storing in DB

I'm trying to save some data into a model.
This is the table:
CREATE TABLE "user_dashboard_configs"(
"user_dashboard_config_id" serial NOT NULL,
"user_id" integer,
"module_id" integer,
"graphic_id" integer,
"dc_show" boolean,
"graphic_type" varchar(20),
CONSTRAINT "user_dashboard_configs_pkey" PRIMARY
KEY("user_dashboard_config_id"),
)
This is the Model:
class UserDashboardConfig extends AppModel {
public $name = 'UserDashboardConfig';
//public $useTable = 'user_dashboard_configs';
public $primaryKey = 'user_dashboard_config_id';
var $belongsTo = array(
'Module' => array(
'className' => 'Module',
'foreignKey' => 'module_id',
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
And the add() function
...
$this->UserDashboardConfig->create();
$save['UserDashboardConfig']['user_id'] = intval($request['user_id']);
...
$save['UserDashboardConfig']['graphic_type'] = $value;
$this->UserDashboardConfig->save($save);
...
// also tried with
// $this->UserDashboardConfig->set(array(
// 'user_id' => intval($request['user_id'],
// ...
// )));
This debug:
debug($this->UserDashboardConfig->getDataSource()->getLog(false, false));
Returns:
(int) 8 => array(
'query' => 'BEGIN',
'params' => array(),
'affected' => (int) 0,
'numRows' => (int) 0,
'took' => (float) 1
),
(int) 9 => array(
'query' => 'INSERT INTO "public"."user_dashboard_configs" ("user_id", "module_id", "graphic_id", "dc_show", "graphic_type") VALUES (4, 57, 6, 'FALSE', 'bar_line')',
'params' => array(),
'affected' => (int) 1,
'numRows' => (int) 1,
'took' => (float) 0
),
(int) 10 => array(
'query' => 'COMMIT',
'params' => array(),
'affected' => (int) 1,
'numRows' => (int) 1,
'took' => (float) 0
)
The INSERT instruction when pasted into pgAdmin stores the data correctly, but CakePHP does not store it, despite not throwing errors.
Just noticed this: the sequence on the DB is increasing, so there is kind of an insert, but the data is not shown on the table.
Could this be a postgresql / phAdmin problem?
Can someone help on how to solve this?
Already tried:
https://stackoverflow.com/a/10198674/5525602
https://stackoverflow.com/a/20502819/5525602
https://stackoverflow.com/a/20824941/5525602
http://headynation.com/cakephp-model-save-function-returning-false-without-an-error-or-sql-statements/

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 paginate with join on tables

On PeoplesController.php, I wanted to add the Events table by left join, Could someone guide me as i am still learning. Here's the original and working code.
public function constructClasses() {
parent::constructClasses();
$this->loadModel('People');
$this->loadModel('Event');
}
public function view() {
$num_per_page = 8;
if($tab == "images") {
$conditions = array();
$this->paginate['Tagged'] = array(
'tagged',
'model' => 'Image',
'by' => $people['People']['slug'],
'conditions' => $conditions,
'order' => array('Image.created'=>'DESC'),
'limit' => $num_per_page,
'paramType' => 'querystring',
);
$related = $this->paginate('Tagged');
}
}
here's the output:
array(
(int) 0 => array(
'Image' => array(
'object_id' => '1098',
)
),
....
....
(int) 7 => array(
'Image' => array(
'object_id' => '1082',
)
)
)
the goal is to get the slug from the events table by left join
Event.id = Image.object_id
Events table
-----------------------
id | slug
-----------------------
1098 | slug-of-1098
1082 | slug-of-1082
Goal Output:
array(
(int) 0 => array(
'Image' => array(
'object_id' => '1098',
'slug' => 'slug-of-1098'
)
),
....
....
(int) 7 => array(
'Image' => array(
'object_id' => '1082',
'slug' => 'slug-of-1082'
)
)
)

Prevent Count query from executing in cakephp

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?

SQL select from three Tables and save in PHP array

I am working on a web application and would like to improve my code a little bit.
This is the SQL table structure I made for the application:
Now my question: Is there an easy way in PHP to select (with join or whatever?) the information from one specific event_id on all tables and save them in an array with followed structure for example:
$events = array(
'event_id' => 1,
'event_name' => '{"de":"Weltwirtschaftsforum","fr":"Forum \u00e9conomique mondial","it":"Forum Economico Mondiale"}',
'event_description' => '{"de":"Description DE","fr":"Description FR","it":"Description IT"}',
'event_lastedit' => 2011-12-01 10:23:35,
'locations' => array(
array(
'location_id' => 1,
'location_name' => 'Bern',
'location_date' => '01.01.2012',
'location_deadline' => 1323340607,
'timestamps' => array(
array(
'timestamp_id' => 1,
'timestamp_time' => '10:30',
'timestamp_seats' => 30
),
array(
'timestamp_id' => 2,
'timestamp_time' => '16:30',
'timestamp_seats' => 40
)
)
),
array(
'location_id' => 2,
'location_name' => 'Davos',
'location_date' => '02.02.2012',
'location_deadline' => 1323340607,
'timestamps' => array(
array(
'timestamp_id' => 3,
'timestamp_time' => '12:30',
'timestamp_seats' => 50
),
array(
'timestamp_id' => 4,
'timestamp_time' => '15:30',
'timestamp_seats' => 60
)
)
)
)
);
I hope the question is clear enough.
Greetings
Spinne
Edit:
What i did so far:
$event = $event_db->querySingle("SELECT * FROM rf_events WHERE event_id={$event_id}", true)
$rf_locations = $event_db->query("SELECT * FROM rf_locations WHERE event_id={$event_id}");
$locations = array();
$timestamps = array();
$counter = 0;
// Loop sql query result and save entries in $events array.
while ( $row = $rf_locations->fetchArray() ){
$locations[$counter] = array(
'location_id' => $row['location_id'],
'location_name' => json_decode($row['location_name'], true),
'location_date' => $row['location_date'],
'location_deadline' => $row['location_deadline']
);
$rf_timestamps = $event_db->query("SELECT * FROM rf_timestamps WHERE location_id={$row['location_id']}");
$counter2 = 0;
while ( $row2 = $rf_timestamps->fetchArray() ){
$locations[$counter]['timestamps'][$counter2] = array(
'timestamp_id' => $row2['timestamp_id'],
'timestamp_time' => $row2['timestamp_time'],
'timestamp_seats' => $row2['timestamp_seats']
);
$counter2++;
}
$counter++;
}
SELECT * FROM rf_events, rf_locations, rf_timestamps WHERE
rf_locations.event_id = rf_events.event_id AND
rf_timestamps.location_id = rf_locations.event_id
Then, use add the data into php accordingly.
You can refer to: MYSQL JOIN

Categories