CakePHP Find Condition does not appear in SQL Query - cakephp

My Find conditions don't seem to be making their way into the SQL based on Debugkits SQL log. Can someone point out to me what I'm doing wrong? This is my first time trying to filter my Find results.
BillingCenters controller
$conditions = array(
'BillingCenter.isactive' => '1'
);
$this->set(
'billingcenters',
$this->BillingCenter->find('all', array($conditions))
);
The resulting SQL Query shown by DebugKit is.. (missing the WHERE clause???)
SELECT
`BillingCenter`.`id`,
`BillingCenter`.`startdate`,
`BillingCenter`.`enddate`,
`BillingCenter`.`name`,
`BillingCenter`.`isactive`,
`BillingCenter`.`created`,
`BillingCenter`.`modified`
FROM
`bm`.`billing_centers` AS `BillingCenter`
WHERE
1 = 1
If i changed the controller code to
$this->set(
'billingcenters',
$this->BillingCenter->find('all')
);
The resulting SQL is STILL the same.
SELECT
`BillingCenter`.`id`,
`BillingCenter`.`startdate`,
`BillingCenter`.`enddate`,
`BillingCenter`.`name`,
`BillingCenter`.`isactive`,
`BillingCenter`.`created`,
`BillingCenter`.`modified`
FROM
`bm`.`billing_centers` AS `BillingCenter`
WHERE
1 = 1

This bites me all the time.
Change your find line to this:
$this->set(
'billingcenters',
$this->BillingCenter->find('all', array('conditions' => $conditions))
);

Related

How to dump the query in Phalcon Framework using Model

$content = Content::findFirst([
'conditions' => 'state = :state: AND URLid = :url: AND city = :city:',
'bind' => [
'state' => $geodata_usstates->statecode,
'url' => $company,
'city' => $geodata_geocity->city
]
]);
I want to dump the query generated for this. If I were using Laravel, I would simply do
$content->toSql();
But here I'm using Phalcon. How can I achieve the same thing in Phalcon?
Query is not available in your model. Query is build based on model using query builder, passed to Query instance and executed against your db connection.
What you could do is use the events manager and read using the db:beforeQuery event
Example here https://forum.phalconphp.com/discussion/18371/check-the-connection-before-querying-into-database
I don't believe you can output the complete query, because it's a prepared query - thus the best you'd get is:
SELECT * FROM `content` WHERE state = ? AND URLid = ? AND city = ? LIMIT 1
Personally, I don't bother trying to log queries in code. I've enabled the query log on my MariaDB server, and just check the log. The query logged is guaranteed to be the query run.

Pagination of query ignoring the LIMIT clause in CakePHP 3.x

I am trying to create a query that would only return 2 results, and by following the documentation I get the query to run, however the limit is still set to 20 by default.
Here is how the query is built:
$upcomingMeetings = $this->Meetings->find('all')
->where(['Meetings.user_id' => $this->Auth->User('user_id')])
->andWhere(["Meetings.date >= " => date('Y-m-d') ])
->order(['Meetings.date' => 'ASC'])
->limit(2);
The result is being passed to the view like following:
$this->set('upcomingMeetings', $this->paginate($upcomingMeetings));
Here is the query that is being run on the database:
SELECT
Meetings.id AS `Meetings__id`,
Meetings.date AS `Meetings__date`,
Meetings.user_id AS `Meetings__user_id`,
FROM
meetings Meetings
WHERE
(
Meetings.group_id = 7
AND Meetings.date >= '2016-01-14'
)
ORDER BY
Meetings.date ASC
LIMIT
20 OFFSET 0
Any help or guidance is much appreciated.
When paginating a Query object, CakePHP will ignore the limit() clause and use the value defined in the $paginate configuration array instead.
This is what can be concluded after inspecting the source code.
Try adding the following to your controller:
public $paginate = [
'limit' => 2,
];

cakephp - order by timestamp DESC not working

I have a table called posts that stores all the posts . It has two columns "Created" and "Modified" .
Below is my query in the model :
$options = [
'conditions' => [
'circle_id' => $my_circle_list,
'team_id' => $this->current_team_id,
'modified BETWEEN ? AND ?' => [$start, $end],
],
'order' => ['modified'=> 'desc'],
'limit' => $limit,
'fields' => ['post_id'],
];
$res = $this->find('list', $options);
Now i want the latest edited posts on top and below is what my mysql dump reads like :
SELECT `Post`.`id` FROM `db`.`posts` AS `Post` WHERE `Post`.`id` IN (125, 124) AND `Post`.`del_flg` = '0' ORDER BY `Post`.`modified` desc LIMIT 20
If i run this query in my database editor ,it gives my the correct output , but in my controller the ordering changes again , this is something i figured after i debugged the array values.
Would be helpful if anyone could tell me if there's any specific reason behind this . The call to this model method from the controller is done in a conventional manner .
When using $this->find() after Cake has queried the database with the generated SQL it calls the afterFind() method on the model where it can manipulate the data. This is the mostly likely place for the ordering to have been modified.

whats wrong with this query

I have this in my table model called Table
$test = $this->find('first', array(
'conditions' => array('table.test_id is NULL'),
'order'=> array('table.created ASC'),
)
);
it doesnt work. Tryingt to get the latest row with some criteria
Well, first of all, to get the latest row, you would want to organize by the created field descending, rather than ascending. Also, there are some problems with your syntax, that I have cleaned up below.
$this->find('first', array('conditions'=>array('Table.test_id'=>NULL), 'order'=>array('Table.created'=>'desc')));

Cakephp find method produces weird SQL

I'm trying to do a Cakephp Find query, and I'm having a little where clausule:
$pending = $this->Transaction->find('all', array('conditions' => array('Transaction.amount >' => 'Transaction.recieved')));
I'd expect it would generate something like this:
SELECT * From `transactions` as `Transaction` WHERE `Transaction`.`amount` > `Transaction`.`recieved`
However, it's producing the following SQL:
SELECT * From `transactions` as `Transaction` WHERE `Transaction`.`amount` > 'Transaction.recieved'
Notice the small difference between
`Transaction`.`recieved`
and
'Transaction.recieved'
Why is this? My SQL query is failing now.
Cake has no way of knowing that you didn't intend to use a string (i.e. it's syntactically identical to for example array( 'Transaction.name' => 'foo' )). It works if you give the condition as a single string:
'conditions' => array( 'Transaction.amount > Transaction.received' )

Resources