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')));
Related
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.
Project hasMany Comments
Payment (id, project_id, value, date)
I want to include in my index view a column equal to the sum of payments per project. I want to be able to sort by this field.
I've found some similiar problems here but neither of them concerns pagination.
My current (wrong) solution:
$this->paginate = array('Project' => array(
'conditions' => array('Project.archived =' => $archive),
'order' => 'Project.start_date DESC',
'contain' => array(
'Payment' => array(
'fields' => array('SUM(Payment.value) as Project__value_sum'),
'group' => array('Payment.project_id'),
)
)
));
$data = $this->paginate('Project');
$this->set('projects', $data);
$this->set('archive', $archive);
Seems like you'll probably want to do an afterSave on the Payment model.
Within the afterSave function, you run some MySQL that totals the payments (using MySQL SUM()) for the related Project, and store it in Project.payment_total.
Then, you can easily sort by that field, and it keeps you from having to run complex MySQL on every page, since it will only run when necessary (ie when there's a new or changed payment)
I'm sorry, I am a newbie in CakePHP and I am a little bit confused in this subject, let me explain:
I have a relationship between two tables. One of the table is Dose and the other is tank. So, one Tank belongs to a Dose. A Dose has many Tanks. The table schema is:
CREATE TABLE `doses` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`dose` INT(5) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
In my Tank view I have the following code:
<?php echo $form->input('dose_id', array('class'=>'input', 'label' => ''));?>
Each 'dose' (field) from Dose table correspond to a value, such as 200, 300, and so forth. I need to use these numbers to calculate others numbers before to insert into my database (table tank). For instance, my code in tanks_controllers:
$t_u = $this->data['Tank']['tipo_uso_id'];
if( $t_u == '1'){
$this->data['Tank']['producao_adubo_diaria'] = $this->data['Tank']['dose_id'] * 0.10;
.
.
.
However, it is bringing to me the ID of the Dose and not the value (dose field). Where can I set up this to bring me the correct data (dose)? I tried to set up this way in my model:
'Dose' => array(
'className' => 'Dose',
'foreignKey' => 'dose_id',
'conditions' => '',
'fields' => 'dose',
'order' => ''
)
It did not work.
I appreciate your time helping me.
Thanks in advance.
it is bringing to me the ID of the Dose and not the value (dose field). Where can I set up this to bring me the correct data (dose)?
You need to get it from the db (model), not from the view. So you need to do a find(). If you are new to Cake, you should read the cookbook first to see how it works.
What does $form->input('dose_id') produce? A dropdown? If so; by default cake will produce a dropdown with the value containing (dose_)id, and the text you see as the value of $displayField(usually name/title).
To do this; if I understand you, you would need to first query doses for all the values and store the result in an array using the dose value as the key AND the value, rather than the id as you normally would. You would then be able to access the actual dose value from $this->data.
$doseArray=array();
$doses = $this->Dose->find('all');
foreach($doses['Dose'] as $k => $v) {
$doseArray[$v] = $v;
}
perhaps. Seems a bit redundant so I might be off.
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' )
I am facing a problem while fetching values using paginate function in cakephp. In the "to" field of message I have CSV fields of userid. To search messages for a single user. I am using the code below...
$this->set('message', $this->paginate('Message', array(
'or'=> array(
"Message.to LIKE" => "".$this->Session->read('Auth.User.id').",",
"Message.to LIKE" => ",".$this->Session->read('Auth.User.id').","
)
)));
But the query is formed in this manner which is not what I want.. I want to two conditions with OR condition.
SELECT `Message`.`id`, `Message`.`timestamp`, `Message`.`to`, `Message`.`from`,
`Message`.`message`, `Message`.`subject`, `Message`.`urgent`, `Message`.`read`,
`Message`.`tag`, `Message`.`open`, `Message`.`reply_id`, `User`.`id`,
`User`.`fname`, `User`.`lname`, `User`.`user`, `User`.`password`,
`User`.`photo`, `User`.`created`, `User`.`access`, `User`.`login`,
`User`.`status`, `User`.`role`
FROM `messages` AS `Message`
LEFT JOIN `users` AS `User` ON (`Message`.`to` = `User`.`id`)
WHERE `Message`.`to` LIKE ',1,'
ORDER BY `Message`.`timestamp` desc
LIMIT 5
The problem is that your conditions array stumbles across a PHP limitation: an array can not have two keys with the same name.
'or'=> array(
"Message.to LIKE" => "".$this->Session->read('Auth.User.id').",",
"Message.to LIKE" => ",".$this->Session->read('Auth.User.id').","
)
You are using the key "Message.to LIKE" twice, so only the last one gets used.
The workaround looks like this:
'or'=> array(
array("Message.to LIKE" => $this->Session->read('Auth.User.id') . ","),
array("Message.to LIKE" => "," . $this->Session->read('Auth.User.id') . ",")
)
Oh, and you should seriously think about a) database normalization and b) code readability.
I think part of the problem might be that he is using LIKE with only the literal exact match syntax.
If any of the following assumptions are incorrect then ignore this..
But it seems like you are searching for user#domain.tld in the to string and having to attach an 'or' condition to match the case where the recipient is listed as a single email address in a string of comma separated email addresses...
I don't think cake adds the '%' character to LIKE queries automagically so perhaps you could try adding it to the query. This should make it match both cases with a single condition in the find call.
<?php
... snip ...
'conditions' => array(
"Message.to LIKE" => "%" . $this->Session->read( 'Auth.User.id' ) . "%"
),
... snip ...
?>
Just make the condition into an array
$this->set('message', $this->paginate('Message', array(
"Message.to LIKE" => array("".$this->Session->read('Auth.User.id').",",",".$this->Session->read('Auth.User.id').",")
)));