Use IN clause in updateAll() method in CakePHP 3.x - mysql

I am using Cakephp 3.x and i want to update my single field for multiple ids. Something like this..
UPDATE mytable SET `status` = '1' WHERE ID IN (1,2,3)
Currently i am using query to perform this action using cakpehp
$this->Leaveregisters->query()
->update()
->set(['leaveregister_status' => $this->request->data('status')])
->where(['leaveregister_id IN ('.$this->request->data('final_ids_string').')'])
->execute();
Well this does the trick for me but i want this to be performed using cakephp 3.xs' ORM method .. so i am trying to use this instead
$table->updateAll(['field' => $newValue], ['id' => $entityId]);
But this code is for single id only which i do not want.. i also do not want to use foeach loop to perform the same action. Instead i want an ID to be passed via array or comma seperated in any case and want to perform the same action.
Is there any way i can perform the same thing using ORM method using cakephp 3.x
Thanks

usinga updateAll or a query() objects to do a bulk update is the same thing as you can read in the manual at the end of this paragraph
so you can do
$this->Leaveregisters->query()
->update()
->set(['leaveregister_status' => $this->request->data('status')])
->where(['leaveregister_id IN' => [1,2,3])
->execute();
or
$this->Leaveregisters->updateAll(
['leaveregister_status' => $this->request->data('status')]
['leaveregister_id IN' => [1,2,3]);
remember then when usin IN clause you have to pass an array. Read this part of the manual on how to create IN clause

You have to use array datatype for pass in IN CLAUSE
$in_condition= explode(",",$this->request->data('final_ids_string'));
$this->Leaveregisters->query()
->update()
->set(['leaveregister_status' => $this->request->data('status')])
->where(['leaveregister_id IN' => $in_condition])
->execute();
With updateAll() of Model
$table->updateAll(array(
// new values
),
array('id' => array(1,2,3,4,5,6))
);

Related

Multiple Fields with a GroupBy Statement in Laravel

Already received a great answer at this post
Laravel Query using GroupBy with distinct traits
But how can I modify it to include more than just one field. The example uses pluck which can only grab one field.
I have tried to do something like this to add multiple fields to the view as such...
$hats = $hatData->groupBy('style')
->map(function ($item){
return ['colors' => $item->color, 'price' => $item->price,'itemNumber'=>$item->itemNumber];
});
In my initial query for "hatData" I can see the fields are all there but yet I get an error saying that 'colors', (etc.) is not available on this collection instance. I can see the collection looks different than what is obtained from pluck, so it looks like when I need more fields and cant use pluck I have to format the map differently but cant see how. Can anyone explain how I can request multiple fields as well as output them on the view rather than just one field as in the original question? Thanks!
When you use groupBy() of Laravel Illuminate\Support\Collection it gives you a deeper nested arrays/objects, so that you need to do more than one map on the result in order to unveil the real models (or arrays).
I will demo this with an example of a nested collection:
$collect = collect([
collect([
'name' => 'abc',
'age' => 1
]),collect([
'name' => 'cde',
'age' => 5
]),collect([
'name' => 'abcde',
'age' => 2
]),collect([
'name' => 'cde',
'age' => 7
]),
]);
$group = $collect->groupBy('name')->values();
$result = $group->map(function($items, $key){
// here we have uncovered the first level of the group
// $key is the group names which is the key to each group
return $items->map(function ($item){
//This second level opens EACH group (or array) in my case:
return $item['age'];
});
});
The summary is that, you need another loop map(), each() over the main grouped collection.

laravel 5 update multiple columns in one line?

It's possible update multiple columns in one line with laravel 5.4?
I try to this
DB::table('Home_Content')->where('id',1)->update(
[$_POST['name'] => $_POST['content']],
[$_POST['title'] => $_POST['titleMsg']]
);
only 1st part is work([$_POST['name'] => $_POST['content']])
server not return any error, but only 1st part is success.
Update method syntax in Laravel is like so :
DB::table('users')
->where('id', 1)
->update(['options->enabled' => true]);
Which means the values we want to update are inside an array, what I've seen is that you separated what you want to update into two arrays
DB::table('Home_Content')->where('id',1)->update(
[$_POST['name'] => $_POST['content']],
[$_POST['title'] => $_POST['titleMsg']]
);
so now you are out of the array
Your code should be like this
DB::table('Home_Content')->where('id',1)->update([
$_POST['name'] => $_POST['content'],
$_POST['title'] => $_POST['titleMsg']
]);
Meaning each pair has a key and a value and separated with comma
If you want to update record in multiple row with id as Array() use following query.
DB::table('users')
->whereIn('id', [1,2,3])
->update(['status' => 1]);

Cakephp 3: Modifying Results from the database

In my database there is a content table and when fetching data from this table I would like to append field url to the results, which is based on slug field which is contained in the table. Anyway, I have seen a way to do this in the previous versions of cakephp using behavior for the model of this table and then modifying results in afterFind callback in the behavior class. But in version 3 there is no afterFind callback, and they recommend using mapReduce() method instead in the manual, but this method is poorly explained in the manual and I cant figure out how to achieve this using mapReduce().
After little bit of research I realized that the best way to append the url field field to find results is using formatResults method, So this is what I did in my finders:
$query->formatResults(function (\Cake\Datasource\ResultSetInterface $results) {
return $results->map(function ($row) {
$row['url'] = array(
'controller' => 'content',
'action' => 'view',
$row['slug'],
$row['content_type']['alias']
);
return $row;
});
});

Delete multiple rows in YII2

I have an array of objects fetched from database:
$masterListContacts = MasterListContacts::find()
->select('master_list_contacts.*')
->innerJoin('master_contacts', '`master_contacts`.`id` = `master_list_contacts`.`master_contact_id`')
->with('masterContact')
->where(['user_id' => \Yii::$app->user->identity->id, 'slug' => $slug])
->all();
Under certain circumstances, I need to delete all rows from the database represented in this array. But with both delete() and deleteAll() methods I got an error Call to a member function ... on array. Could someone tell me please which one is the best way to accomplish this?
UPDATE:
Here is my database structure.
Found better solution:
\Yii::$app
->db
->createCommand()
->delete('master_contacts', ['id' => $deletableMasterContacts])
->execute();
Where $deletableMasterContacts is array of master_contacts ids, which should be deleted
You can painlessly remove ->select('master_list_contacts.*').
->innerJoin('master_contacts', '`master_contacts`.`id` = `master_list_contacts`.`master_contact_id`')
performs the same work that ->joinWith('masterContact').
For delete entites try use this code:
MasterListContacts::deleteAll(['user_id' => \Yii::$app->user->identity->id, 'slug' => $slug]);

CakePhp mysql raw query error

I'm new to cakephp. I'm trying to search through mysql tables. I want to use nested query.
class TableController extends AppController{
.
.
public function show(){
$this->set('discouns', $this->DiscounsController->query("SELECT * FROM discoun as Discoun WHERE gcil_id = 1"));//(SELECT id FROM gcils WHERE genre = 'Shoes' AND company_name = 'Adidas')"));
}
}
Error:
Error: Call to a member function query() on a non-object
I've also tried
public function show(){
$this->DiscounsController->query("SELECT * FROM count as Count WHERE ctr_id = (SELECT id FROM ctrs WHERE genre = 'Shoes' AND company_name = 'Adidas')");
}
Error:
Error: Call to a member function query() on a non-object
File: C:\xampp\htdocs\cakephppro\myapp\Controller\CountsController.php
Please help. I've been trying this for last few hours. :/
As mentioned in the comments there are a few problems with your code.
Firstly, you are trying to call the query() method on a Controller, whereas you should be executing it on a Model, as it is models that handle database queries and the controller should simply be used to call these methods to get the data and pass them to the view.
The second thing is that you are executing a very simple SQL query raw instead of using CakePHPs built in functions <- Be sure to read this page in full.
Now for your problem, as long as you have setup your model relationships correctly and followed the correct naming conventions, this should be your code to run your SQL query from that controller:
public function show(){
$this->set('discouns', $this->Discouns->find('all', array(
'conditions' => array(
'gcil_id' => 1,
'genre' => 'shoes',
'company_name' => 'Adidas'
)
));
}
query() is not a Controller, but a Model method. That's what the error (Call to a member function on a non-object) is trying to tell you.
So the correct call would be:
$this->Discount->query()
But you are calling this in a TableController, so unless Table and Discount have some type of relationship, you won't be able to call query().
If the Table does have a relationship defined you will be able to call:
$this->Table->Discount->query()
Please not that query() is only used when performing complex SQL queries in scenarios where the standard methods (find, save, delete, etc.) are less practical.
$this->Counts->find('all',array(
'conditions' => array(
'ctrs.genre' => 'Shoes',
'ctrs.company_name' => 'Adidas'
), 'recursive' => 1
));
The above is with tables named counts and ctrs.
This is assuming you have the model set up to have some sort of relationship between the counts table and the ctrs table. It's kind of hard to tell in your code exactly what you tables are.
The CakePHP book should have all the answers you need. One of the reasons to run CakePHP over regular PHP is the FIND statement. Once you have your models set up correctly, using the find statement should be really easy.
http://book.cakephp.org/2.0/en/models.html