I want to update multiple rows of a specific model at once, so I have bellow code and data structure. Whenever I tried to update the record every time record gets inserted instead of update.
in my controller
function products($cat_id = null){
$products = $this->Products->find()->where(['category_id' => $cat_id]);
$productPatch = $this->Products->patchEntities($products, $this->request->data);
$this->Products->saveMany($productPatch); //Always creates new record
}
Here is the data in the different array,
In $products
[0] => Array
(
[id] => 13 //product_id
[category_id] => 17
[slug] => onScreen
[status_id] => 1
)
[1] => Array
(
[id] => 14
[category_id] => 17
[slug] => pdf
[status_id] => 1
)
In $this->request->data
[0] => Array
(
[id] => 13 //product_id
[category_id] => 17
[slug] => onScreen
[status_id] => 2 //Data changes here
)
[1] => Array
(
[id] => 14
[category_id] => 17
[slug] => pdf
[status_id] => 2 //Data changes here
)
You can use updateAll() method to update data in Bulk :
$this->modelClass->updateAll(
['published' => true], // fields
['published' => false]); // conditions
Related
It is my first time working with an external API. I have already fixed that it gives me the right output. In a controller I have the following code:
public function index() {
$api = new Wefact();
$parameters = [
];
$api_response = $api->sendRequest('product', 'list', $parameters);
print_r($api_response);
}
This shows me the array on the page. But I do not know how to use this array to get it into a foreach in the blade. The output of the print_r is as follow:
Array
(
[controller] => product
[action] => list
[status] => success
[date] => 2022-05-05T04:20:03+02:00
[totalresults] => 2
[currentresults] => 2
[offset] => 0
[products] => Array
(
[0] => Array
(
[Identifier] => 1
[ProductCode] => P0001
[ProductName] => SIM ONLY 5GB
[ProductKeyPhrase] => SIM ONLY 5GB
[ProductDescription] =>
[NumberSuffix] =>
[PriceExcl] => 25
[TaxCode] => V21
[TaxPercentage] => 21
[PricePeriod] => m
[Modified] => 2022-05-05 03:49:57
)
[1] => Array
(
[Identifier] => 2
[ProductCode] => P0002
[ProductName] => SIM ONLY 10GB
[ProductKeyPhrase] => SIM ONLY 10GB
[ProductDescription] =>
[NumberSuffix] =>
[PriceExcl] => 35
[TaxCode] => V21
[TaxPercentage] => 21
[PricePeriod] => m
[Modified] => 2022-05-05 04:03:47
)
)
)
As you can see there are two products. I want to have these in a datatable with an foreach.
Since it is my first time, I really do not know how to do this.
Anyone that can help me out?
If you want to access the array of products it would be like this
return view('your_view', [
'products' => $api_response['products']
]);
I have the following array and I would like to be able to organize it through collection
$collection = Collection($total);
$filter = $collection->groupBy('date_general')->toArray();
[2017-11-14] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 164
)
[1] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 112
)
)
Up to this point I already have the array organized by dates. Now within each date I need to organize it by user, that is to say that it is:
[2017-11-14] => Array
(
[164] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 164
)
)
[112] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 112
)
)
)
You'll have to process each date group and individually group their contents.
$collection = collection($total)
->groupBy('date_general')
->map(function ($data) {
return collection($data)->groupBy('user_id')->toArray();
});
See also
Cookbook > Collections
Hi i don't know what is wrong with the threaded of cakephp 2 please help
when i use threaded without condition it will show its children
$category = $this->Category->find('threaded');
output:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
[name] => Affinity Collection
[parent_id] => 0
[lft] => 25
[rght] => 30
[alias] => affinity-collection
[status] => 0
)
[children] => Array
(
[0] => Array
(
[Category] => Array
(
[id] => 113
[name] => Core samples
[parent_id] => 11
)
)
)
but when i use this with condition it won't show its parent
$this->Category->find('threaded',array('conditions'=>array('Category.id'=>11)));
output:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
[name] => Affinity Collection
[parent_id] => 0
[lft] => 25
[rght] => 30
[alias] => affinity-collection
[status] => 0
)
[children] => Array
(
)
)
)
please help why it is not showing the children when use with condition
I think the problem is because you are asking for all categories with an id of 11. Insted you should ask for all categories that are children of categorie 11.
You could use your left and right columns to get a nested array (keyword Tree behaviour). Try this:
$parentCategorie = $this->Categorie->find('first', array(
'conditions' => array(
'Categorie.id' => 11
)
);
$children = $this->Categorie->find('first', array(
'conditions' => array(
'Categorie.lft BETWEEN ? AND ?' => array($parentCategorie['Categorie']['lft'], $parentCategorie['Categorie'])['rght']
)
);
Probably the best chance you have to achive what you want is to use the children method of the Tree behavior.
You coul use it as follows
$parent=$this->Category->find('first',
array('conditions'=>array('Category.id'=>11)));
$this->Category->children($parent['Category']['id']);
I need to get multiple records from model, then put them into request->data for a view to render into a form with multiple input fieldsets for example name='data[Applicant][0][display_name]'. name='data[Applicant][1][display_name]'...data value goes in for each applicant.
Actually I've already done what i want, but i do not think it is a good method to do so.
Appreciate if anyone can guide me
foreach ($this->Applicant->data['Applicant'] as $key=>$item){
$data['Applicant'][] = $item['Applicant'];
}
$this->request->data = $data;//set Model to data
$this->set('data' , $this->Applicant->data);
$this->Applicant->data is the following:
Array
(
[Applicant] => Array
(
[0] => Array
(
[Applicant] => Array
(
[id] => 1
[application_id] => 17
[name] => User
[first_name] =>
...
)
)
[1] => Array
(
[Applicant] => Array
(
[id] => 3
[application_id] => 17
[name] =>
[first_name] =>
the following is the desired output (less one level):
Array
(
[Applicant] => Array
(
[0] => Array
(
[id] => 1
[application_id] => 17
[name] => User
[first_name] =>
...
)
[1] => Array
(
[id] => 3
[application_id] => 17
[name] =>
[first_name] =>
thanks
This should suffice:
$this->request->data['Applicant'] = Hash::extract( $this->Applicant->data, 'Applicant.{n}.Applicant' );
How would i save the following;
I want the first 4 items to apply to all the "products to be saved", given that this data is at the top of my form and "global" to the products that get saved how would i tell cakephp to save it to each product or do i have to create a foreach loop and manually insert the data?
Global data to each product.
[discount_id] => 17
[range_id] => 21
[category_id] => 6
[user_id] => 104
Array
(
[Product] => Array
(
[discount_id] => 17
[range_id] => 21
[category_id] => 6
[user_id] => 104
[0] => Array
(
[product_code] => ffff
[colour] => red
[lead_time_weeks] =>
[description] =>
[height] => 11111
[width] => 22222
[depth] =>
[price] =>
[discount] => 50
[discounted_price] =>
[quantity] =>
)
[1] => Array
(
[product_code] => fgfgfggf
[colour] => red
[lead_time_weeks] =>
[description] =>
[height] => 123
[width] => 123
[depth] =>
[price] =>
[discount] => 50
[discounted_price] =>
[quantity] =>
)
)
)
Save method in controller
$this->Product->saveAll($this->request->data['Product']
I would personally use a foreach loop, saveAll is meant for saving associated data.
e.g.
foreach($this->request->data['Product'] as $product){
// Don't want to add the generic items
if(is_array($product)){
$newProduct = $this->Product->create();
$newProduct['Product'] = $product; // To save added them seperately
// Then add the generic items into the array
$newProduct['Product']['discount_id'] = $this->request->data['Product']['discount_id'];
etc...
$this->Product->save($newProduct);
}
}