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);
}
}
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 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
Presently m working with CakePhp..i got some problem with combining two array and prepare for a json encoded array.. i used array_merge() property but its not working.. how can i encoded both of these two array..
M doing like this :
return json_encode(array_merge ($product_list,$price_list));
i have two php array as follows :
array 1:
Array
(
[0] => Array
(
[PriceList] => Array
(
[price_id] => 2
[price_name] => abc
[date_time] => 2015-07-06 16:22:56
[dealer_type] => Dealer
[purpose] => dealer
[status] => ACTIVE
)
)
[1] => Array
(
[PriceList] => Array
(
[price_id] => 3
[price_name] => xyz
[date_time] => 2015-07-06 16:22:56
[dealer_type] => Dealer
[purpose] => dealer
[status] => ACTIVE
)
)
)
array 2:
Array
(
[0] => Array
(
[Product] => Array
(
[cat_id] => 1
[subcat_id] => 3
[brand_id] => 1
[p_code] => PP12567
[name] => akai
[model_no] =>
[specification] => color tv
[color] =>
[quality] =>
[size] =>
[p_unavail] => 1
[demo_avail] => 0
[brochure] =>
[status] => active
)
[ProductPrice] => Array
(
[id] => 154
[p_code] => PP12567
[price_id] => 1
[quantity] => 233
[purchase_price] => 344.00
[selling_price] => 44.00
[discount_price] => 33.00
[tax] => 5.00
[datetime] => 2015-07-23 15:47:11
)
[ProductSubCategory] => Array
(
[subcat_id] => 3
[cat_id] => 1
[subcat_name] => samsung
[status] => active
)
[ProductCategory] => Array
(
[cat_id] => 1
[cat_name] => Electronics
[cat_type] => Product
[status] => active
)
)
[1] => Array
(
[Product] => Array
(
[cat_id] => 1
[subcat_id] => 4
[brand_id] => 1
[p_code] => PBC-676767
[name] => music
[model_no] => 33
[specification] =>
[color] =>
[quality] =>
[size] =>
[p_unavail] => 0
[demo_avail] => 0
[brochure] =>
[status] => active
)
[ProductPrice] => Array
(
[id] => 156
[p_code] => PBC-676767
[price_id] => 1
[quantity] => 767
[purchase_price] => 54.00
[selling_price] => 55.00
[discount_price] => 22.00
[tax] => 3.00
[datetime] => 2015-07-23 15:47:11
)
[ProductSubCategory] => Array
(
[subcat_id] => 4
[cat_id] => 1
[subcat_name] => sony
[status] => active
)
[ProductCategory] => Array
(
[cat_id] => 1
[cat_name] => Electronics
[cat_type] => Product
[status] => active
)
)
)
how to combine these two array into one and encoded into a json array..
You have to use array_merge but not directly on array1 and array2:
$toEncodeArray = [array_merge ($array1[0], $array2[0])] ;
If you only want the associative array (without the wrapping array), simply do:
$toEncodeArray = array_merge ($array1[0], $array2[0]) ;
I want to give criteria where client_id = $this->user['client_id']; so that loggedIn user can see his own data for his Client.
I dont understand how to get client_id from below array.
[Payment] => Array
(
[id] => 1
[location_id] => 138
[cashier_id] => 313
[client_id] => 2
[pay_date] => 2015-02-13 10:51:54
[amount_paid] => 28882685
[total_paid] => 28882685
[change_due] => 0
[pay_method] => CA
[bank] =>
[ref_no] =>
[payment_type] => Bill
[reversal_fee] =>
[reversal_code] =>
[reversal] => 1
[reversal_date] =>
[reversed_by_id] =>
[User] => Array
(
[id] => 313
[username] => tellerb
[client_id] => 8
)
[Location] => Array
(
[id] => 138
[name] => Sector 64
)
)
Your help will be grateful
Use it as nested associative array
$YourArray['Payment']['User']['client_id'];
Always check how you reach that field . Here outer Array is Payment , then you goto User then goto client_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' );