cakephp update field with not(field) - cakephp

I have this query
UPDATE comments SET like=NOT(like) WHERE userid=12345
Howto with cakephp ?
I tried
$update=array('like NOT' => '(like)');
$conditions=array('userid' => 12345);
$res = $this->updateAll($update,$conditions);
but not works.

Try with
$update = array('like' => '1-like');
$conditions = array('userid' => 12345);
$res = $this->updateAll($update, $conditions);

Related

Not store data in database but return Inserted id in laravel 5.7

Here is my Controller. Please have a look :
$date = date('Y-m-d H:i:s');
$ordersData = array(
'order_date' => $date,
'order_delivery_time' => $shipping_time,
'order_extra_fee' => 0,
'inserted' => $date
);
$ordersData['order_payment_method'] = 'cash';
$ordersData['order_payment_status'] = 'done';
DB::table('orders')->insert($ordersData);
$order_id = DB::getPdo()->lastInsertId();
dd($order_id);
Please check it and let me know what's the issue ?
Have you added $fillable on the model yet?
protected $fillable = ['order_date', 'order_delivery_time', 'order_extra_fee', 'inserted', 'order_payment_method', 'order_payment_status'];

Cakephp 2.8 Why does my search_between function give me a warning?

public function search_between() {
$min = $this->request->query['min'];
$max = $this->request->query['max'];
$conditions = array('Ad.price BETWEEN ? AND ?' => array($min,$max));
$ads = $this->Anuncio->find('all',array('conditions' => $conditions));
$this->set('ads',$ads);
}
Warning: strlen() expects parameter 1 to be string, array given.
**Thank's for your help I'm new to asking questions here.**
try this:
public function search_between() {
$min = $this->request->query['min'];
$max = $this->request->query['max'];
$conditions = array('Anuncio.price >=' => $min,'Anuncio.price <=' $max));
$ads = $this->Anuncio->find('all',array('conditions' => $conditions));
$this->set('ads',$ads);
You can use Cakephp mysql query it will works as like as cakephp query
if(!empty($min) && !empty($max)){
$ads = $this->Anuncio->query('SELECT * from Anuncio WHERE Anuncio.price BETWEEN "'.$min.'" AND "'.$max.'"');
}
else{
$ads = $this->Anuncio->query('SELECT * from Anuncio');
}

How to create a `IN` clause in CakePHP query?

How do you make it where in clause in new CakePHP? I'm trying:
$restaurants->find()->where(['id' => $r_ids])->all();
Where $r_ids is array of ids I need in my query, but this doesn't work as expected.
With CakePHP 3.x it's now necessary to either indicate the data type, which for an array of values need to have [] appended to the type:
$query = $articles
->find()
->where(['id' => $ids], ['id' => 'integer[]']);
or to explicitly make use of the IN keyword:
$query = $articles
->find()
->where(['id IN' => $ids]);
See also
Cookbook > Database Access & ORM > Query Builder > Automatically Creating IN Clauses
Try this one for CakePHP 3.x
$query = $restaurants
->find()
->where(['id IN' => $r_ids]);
$query->all();
You can also use the short syntax:
$result = $restaurants->find('all',
['conditions' => ['id IN' =>$r_ids]]
)->all();
In Cakephp 3.x, If you have multiple where conditions then your query will be as below:
return $this
->find()
->contain([
'XYZ.Articles',
'YYY.Managers',
'ZZZ.Readers',
])
->where([
'ID' => $ids,
'READER.STATUS' => $xyz,
],[
'ID' => 'integer[]'
])
;

convert query to cakephp query

where concat(`year`,'-',`month`) BETWEEN '2013-02' AND '2013-03';
how to use this query in cake php's custom query pagination like this way..
$conditions = array('concat('Payroll.year','-','Payroll.month') BETWEEN ? and ?' => array(2013-02, 2013-03));
$staff_list = $this->Payroll->find("all", array("fields" => array("Payroll.id", "Payroll.month", "Payroll.year"),"conditions"=>$conditions));
thank you.
yes, you could do:
in you model:
var $virtualFields = array(
'payroll_date' => 'CONCAT(Payroll.year, " ", Payroll.month)'
);
and in controller:
$staff_list = $this->Payroll->find("all", array(
"fields" => array("Payroll.id", "Payroll.month", "Payroll.year"),
"conditions" => array('Payroll.payroll_date BETWEEN ? AND ?' => array('2013-02', '2013-03'))
));

$this->Model-id not set with CakePHP 1.3

I am trying to retrieve the id of a record in my database in the index() method of my cns_controller.php file. I want to use it for a find(). However, the $this->Cn->id is not returning a value, therefore the find() doesn't work either.
$uses = array('Cn', 'Release');
/*check non-null value in released_user_id,
showing Release tab has been signed off*/
$releaseSignedOff = $this->Release->find('all', array(
'conditions' => array('Release.cn_id =' => $this->Cn->id,
'Release.released_user_id !=' => null)
));
(sizeof($releaseSignedOff) > 0) ? $releaseSignedOff = true : $releaseSignedOff = false;
$this->set('releaseSignedOff', $releaseSignedOff);
Any ideas?
Thanks
I ended up not using my index view, instead I looked up the most recent record in my cns database table and redirected to the view view using the returned value as a parameter:
if (!$id) {
$most_recent_change_note = $this->Cn->find('first', array(
'order' => array('Cn.id DESC')
));
$this->redirect(array('controller' => 'cns',
'action' => 'view/'.$most_recent_change_note['Cn']['id']));
}
For the pagination, I ended up using the $neighbors feature of CakePHP:
function get_neighbors($id) {
$neighbors = $this->Cn->find('neighbors', array('field' => 'id',
'value' => $id));
$this->set('neighbors', $neighbors);
}
Then in my view view, I used those values to create links:
<div class="paging">
<?php echo ($neighbors['next']) ? $html->link('<< Prev',
array('controller'=>'cns',
'action'=>'view/'.$neighbors['next']['Cn']['id'])).' | ' : '<< Prev | ';
echo $html->link('Next >>', array('controller'=>'cns',
'action'=>'view/'.$neighbors['prev']['Cn']['id'])); ?>
</div>
Thanks to Charles and Ross for helping me reach this conclusion. :)

Resources