A Car HABTM Image in the the table cars_images
Array
(
[Car] => Array
(
[country_id] => 1
[body_type_id] => 7
[published] => 0
[variant] => 4 cyl AWD
)
[Image] => Array
(
[image_url] => files/cars/subaru/outback/2012/Outback-3.6R3.jpg
)
)
The car is being saved. The image is not (no record is being created, not in the images table nor in the cars_images table.
Thanks
You need a form looks like this:
echo $this->Form->input('Image.0.image_url');
echo $this->Form->input('Image.1.image_url');
echo $this->Form->input('Image.2.image_url');
So, produce this:
Array
(
[Car] => Array
(
[country_id] => 1
[body_type_id] => 7
[published] => 0
[variant] => 4 cyl AWD
)
[Image] => Array
(
[0] => Array
(
[image_url] => files/cars/subaru/outback/2012/Outback-3.6R3.jpg
)
[1] => Array
(
[image_url] => files/cars/subaru/outback/2012/Outback-3.6R3.jpg
)
[2] => Array
(
[image_url] => files/cars/subaru/outback/2012/Outback-3.6R3.jpg
)
)
)
And use saveAll:
$this->Car->saveAll($this->request->data);
You can find more information here.
For HABTM the array structure must(in your case)
(
[Car] => Array
(
[country_id] => 1
[body_type_id] => 7
[published] => 0
[variant] => 4 cyl AWD
)
[Image] => Array
(
[Image] => Array
(
[0] => Array
(
[image_url] => files/cars/subaru/outback/2012/Outback-3.6R3.jpg
)
)
)
)
And to "save the data" need to use
$this->Car->saveAll($data);
Related
I have the following stdClass Object contained within $response:
stdClass Object ( [domain] => stdClass Object ( [id] => d1111111 [spamscore] => 75 [rejectscore] => 200 ) [domainalias] => Array ( ) [wildcard] => Array ( ) [catchall] => Array ( ) [forward] => Array ( ) [mailbox] => Array (
[0] => stdClass Object ( [highEmailNotification] => [id] => m1111111 [lastPasswordChange] => 2020-02-19T22:41:12+00:00 [local] => mailbox1 [lowEmailNotification] => [quotaMB] => 10240 [receive] => 1 [rejectscore] => [send] => 1 [spamscore] => [usageMB] => 0 [enabled] => 1 )
[1] => stdClass Object ( [highEmailNotification] => [id] => m2222222 [lastPasswordChange] => 2020-02-17T15:46:21+00:00 [local] => mailbox2 [lowEmailNotification] => [quotaMB] => 10240 [receive] => 1 [rejectscore] => [send] => 1 [spamscore] => [usageMB] => 0 [enabled] => 1 )
[2] => stdClass Object ( [highEmailNotification] => [id] => m3333333 [lastPasswordChange] => 2020-02-19T15:00:36+00:00 [local] => mailbox3 [lowEmailNotification] => [quotaMB] => 1024 [receive] => 1 [rejectscore] => 0 [send] => 1 [spamscore] => 75 [usageMB] => 0 [enabled] => 1 ) ) [spamblacklist] => Array ( ) [spamwhitelist] => Array ( ) [responder] => Array ( ) [name] => domain.com )
I need to convert it into an array and extract particular values, i.e. [id] and [local] from it.
Speed is also an issue, as this array will grow to thousands of items, so if there is other, quicker way than 'foreach' it would be better.
I used some suggestions from here, such as:
$array = json_decode(json_encode($response), True);
foreach ($array as $var)
{
echo $var['id'] . ' - ' . $var['local'] . "<br>";
}
and got partial success with the results:
d1111111 -
-
-
-
-
i - i
(so it found the very first [id] value)
it however missed the most important values I am after.
What I need to get is:
m1111111 - mailbox1
m2222222 - mailbox2
m3333333 - mailbox3
Any suggestions are greatly appreciated.
The values you're trying to print is in a nested array within $response.
Try this.
$array = json_decode(json_encode($response['mailbox']), True);
foreach ($array as $var)
{
echo $var['id'] . ' - ' . $var['local'] . "<br>";
}
In my cakephp Controller when I retrieve my data by find clause I get this array
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 1
[Category-name] => Arts
)
[Course] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[degree] => UG
[course-name] => BSc-Maths
)
[1] => Array
(
[id] => 5
[category_id] => 1
[degree] => PG
[course-name] => MSc Math
)
[2] => Array
(
[id] => 6
[category_id] => 1
[degree] => UG
[course-name] => Bsc Stats
)
[3] => Array
(
[id] => 7
[category_id] => 1
[degree] => PG
[course-name] => Msc-Stats
)
)
)
[1] => Array
(
[Category] => Array
(
[id] => 2
[Category-name] => Science and technology
)
[Course] => Array
(
[0] => Array
(
[id] => 2
[category_id] => 2
[degree] => UG
[course-name] => BSc-CS
)
)
)
[2] => Array
(
[Category] => Array
(
[id] => 3
[Category-name] => Commerce
)
[Course] => Array
(
[0] => Array
(
[id] => 3
[category_id] => 3
[degree] => PG
[course-name] => Msc-Finance
)
)
)
[3] => Array
(
[Category] => Array
(
[id] => 4
[Category-name] => Law
)
)
)
I want to show All Courses for a particular Category in ctp file as in form. As For Category Arts there are 4 Courses.
I want to display these 4 Courses for Arts Category.
I am able to Display Categories using the same array.
But not able to display Courses using same array in y ctp file as dropdown in a form.
Is there a way to Access this Courses data? Or Do I have to use different query for to access Courses??
Please I need your help with this.
Thanks in advance.
I would solve it like this:
An improved retrieving Query which only outputs the course names.
$cats = $this->Category->find('all',array(
'conditions'=>array('Category.Category-name'=>'Arts')
'recursive'=>1,
'contain'=>array('Course'),
'fields'=>'Course.course-name'))[0]['Course'];
Now the $cats variable should contain an array with 4 entries which can be used in a dropdown select
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 save my array with multiple indexing array values.
Demo code
Array
(
[CodeConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] =>
)
)
[ObjectAccountConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] => 2
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
)
)
)
I want to save this array values direct into table .Table name is audit_trail_details.
so please suggest mi proper solution for how to save this data into table.
You need to use saveAssociated and parse your array as the first parameter:-
$this->AuditTrailDetail->saveAssociated($data);
You will also need to make sure $data contains the AuditTrailDetail you are saving the associated data against. For example:-
Array
(
[AuditTrailDetail] => Array
(
[id] => 1
)
[CodeConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] =>
)
)
[ObjectAccountConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] => 2
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
)
)
)
I have this array result and want to sort based on name
$arr = array
(0 => array
(
(name) => Medianas empresas
(count) => 17
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Medianas+empresas
)
1 => array
(
[name] => Grandes empresas y gobierno
[count] => 8
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Grandes+empresas+y+gobierno
)
2 => array
(
[name] => Microempresas
[count] => 9
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Microempresas
)
)
The result should be like,
$arr = array
(
1 => array
(
[name] => Grandes empresas y gobierno
[count] => 8
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Grandes+empresas+y+gobierno
)
0 => array
(
(name) => Medianas empresas
(count) => 17
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Medianas+empresas
)
2 => array
(
[name] => Microempresas
[count] => 9
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Microempresas
)
)
The desired result is in fact identical to the one you have, so, there is nothing to do.
If you confounded indices 0 and 1 in your desired result, just
sort($arr);
would do the job.