cakephp: notice 8:undefined index: id error - cakephp

i'm getting Undefined index: id [APP\controllers\merry_parents_controller.php, line 31]Code
debug($this->data['MerryParent']);
echo 'id: '.$this->data['MerryParent']['id'];
MerryParentsController::login() - APP\controllers\merry_parents_controller.php, line 31
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 204
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 171
[main] - APP\webroot\index.php, line 83
My code:
merry_parents_controller.php
if ($this->Auth->user()){
//debug($this->Auth->user());
$this->data['MerryParent']=$this->Auth->user();
debug($this->data['MerryParent']);
echo 'id: '.$this->data['MerryParent']['id'];
for debug($this->data['MerryParent']), the record is displaying fine:
Array
(
[MerryParent] => Array
(
[id] => 2
[initial] => Ms
[username] => hong
[email] => hong7#yahoo.com
[password2] => hong7
[landline] =>
[mobile] => 293472389472
[address] => dsaflkjasdlfk
[state_id] => 5
[city_id] => 62
[postal_code] => 825729
[created] => 2011-08-02 10:29:59
[modified] => 2011-09-26 06:42:27
)
)
but when i try
echo 'id: '.$this->data['MerryParent']['id'];
i'm getting notice 8: undefined index error. I have no idea on what is the problem. Can someone please help me?

Your array looks like this:
Array
(
[MerryParent] => Array
(
[MerryParent] => Array
(
[id] => 2
...
The id you're looking for is in $this->data['MerryParent']['MerryParent']['id']. You probably want
$this->data = $this->Auth->user();

Related

check and compare associative array value with in_array?

I am trying to using the in_array function to check the value exists in the second array or not. I want to search 556729685 from the following array:
$_SESSION["cart_item"] =
Array
(
[cart_item] => Array
(
[0] => Array
(
[product_name] => White Sakura Necktie
[id] => 11
[product_auto_id] => 556729685
[quantity] => 2
[product_regular_price] => 95
[product_sale_price] => 95
[product_image] => 556729680Black_Sakura_Necktie.jpg
)
[1] => Array
(
[product_name] => hhhad ba bhdbh
[id] => 10
[product_auto_id] => 951790801
[quantity] => 2
[product_regular_price] => 20
[product_sale_price] =>
[product_image] => 951790801hhhad_ba_bhdbh_.jpg
)
)
)
I am using following functions to check but it is giving wrong out put:
in_array(556729685, array_keys($_SESSION["cart_item"]));
I have tried this one also:
in_array(556729685, array_values($_SESSION["cart_item"]));
None is working so help me to solve this issue.
Associative array consist of key, column and values. So, in order to check existence of value you need to reach key of array.
for($i=0;$i<count($_SESSION["cart_item"]);$i++)
{
if( in_array( 556729685 ,$_SESSION["cart_item"][$i] ) )
{
echo "exist in id:".$i.'<br/>';
}
}

How to use saveMany in cakephp 3.2.7

Having problems with saveMany() on a cake 3.2.7 install. It give an error Unknown method "saveMany".
$cartArrSave = Array
(
[0] => Array
(
[deal_id] => 4
[store_id] => 126
[user_id] => 124
[quantity] => 1
)
[1] => Array
(
[deal_id] => 9
[store_id] => 127
[user_id] => 124
[quantity] => 1
)
)
$this->Deals = TableRegistry::get('Deals');
if(!empty($cartArrSave)){
$this->Deals->newEntities($cartArrSave);
$this->Deals->saveMany($cartArrSave);
}
I have checked that saveMany is supported in even cakephp 2
And also its supported in cakephp 3.x
Then why its giving such error?
Please suggest any alternative to saveMany function in cakephp 2.7.8?

Check duplicate entry into object array php laravel

Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => User Object
(
[original:protected] => Array
(
[user_id] => 123456
[active] => 1
[name] => ABC XYZ
[first_name] => ABC
[last_name] => XYZ
[email] => abc#xyz.com
[username] => abcxyz
[secret_code] => S4#$sdD
)
)
[1] => User Object
(
[original:protected] => Array
(
[user_id] => 987654
[active] => 1
[name] => CBD IHK
[first_name] => CBD
[last_name] => IHK
[email] => abc#xyz.com
[username] => seCdils
[secret_code] => S4#$sdD
)
)
)
)
Identify both object array has same secret_code return boolean if exist true else false tried array_count_values return only string and number please guide thanks
I just want to check if detect secret_code same in array give me true else false
<?php
$dupes = []; // keep track of duplicates
foreach ($users as $user1) { // iterate over all items
$dupeCount = 0; // because we iterate over the same array, we always find at least the item itself (1 dupe minimum)
foreach ($users as $user2) { // check the array again
if ($user1 === $user2) { // if they are exactly the same: http://php.net/manual/en/language.oop5.object-comparison.php
$dupeCount++;
}
if ($dupeCount > 1) { // because we always find at least 1, push only when we find more than that
array_push($dupes, $user1); // add it to the result
}
}
}
However for you comment
I just want to check if detect secret_code same in array give me true
else false
which should be in your question to begin with.
collect($users)->unique(function ($item) {
return $item['secret_code'];
});

cakephp linked model not showing in hasmany

I have three Models in cakephp
//Union
public $hasMany = 'Member';
and
//Member
public $hasOne = 'Post';
where as post contains post names
Now the problem is when I use:
$this->Union->findById(1);
in Union controller it shows linked Members but not name of posts
[Union] => Array
(
[id] => 1
[Name] => Dawa vyapar mandal
[created] => 2014-03-31 14:08:12
)
[Member] => Array
(
[0] => Array
(
[id] => 1
[Name] => Ashish
[post_id] => 1
[union_id] => 1
[created] => 2014-03-31 14:11:02
)
[1] => Array
(
[id] => 2
[Name] => Ashu
[post_id] => 1
[union_id] => 1
[created] => 2014-07-01 15:01:15
)
)
)
so how to achieve Post Model inside Member Model?
You have to make the model to be recursive to the level upto which you need data.
In your case, you need the data upto 2nd level, so make recursive as -
$this->Union->recursive = 2;
And after that find your data -
$this->Union->findById(1)

how to sort array date wise in cakephp

I have 2 table booking and message , now I want to show booking request and message in inbox at a time.
$this->paginate = array(
'conditions' => $conditions,'limit' =>10,
'order'=>array('Booking.created'=>'DESC'));
$bookings = $this->paginate('Booking');
$this->paginate = array(
'conditions' => $conditions,'limit' =>10,
'order'=>array('MessageDetail.created'=>'DESC'));
$messages = $this->paginate('MessageDetail');
i have merge both table data ( array_merge($bookings, $messages); )
now i want to it sort date wise (or any conditions)
Array
(
[0] => Array
(
[Booking] => Array
(
[id] => 4
[host_id] => 21
[place_id] => 10
[room_id] => 13
[user_id] => 12
[message_detail_id] => 16
[created] => 2013-04-23 14:44:03
[accept_date] =>
[cancel_date] =>
)
)
[1] => Array
(
[Booking] => Array
(
[id] => 3
[host_id] => 21
[place_id] => 10
[room_id] => 13
[user_id] => 12
[message_detail_id] => 13
[created] => 2013-04-15 14:10:59
[accept_date] => 2013-04-15 14:40:47
[cancel_date] =>
)
)
[2] => Array
(
[MessageDetail] => Array
(
[id] => 17
[message_id] => 2
[user_id] => 12
[sender_id] => 21
[unread] => 0
[created] => 2013-04-24 12:11:47
)
)
[3] => Array
(
[MessageDetail] => Array
(
[id] => 15
[message_id] => 2
[user_id] => 12
[sender_id] => 21
[booking_id] => 3
[unread] => 0
[created] => 2013-04-15 15:01:12
)
)
)
Thanks in advance.
Option #1:
Create a third Model named "BookingAndMessage". You could use the Model's afterSave method (on both Booking and Message) to create a duplicate record in your new model. You could then query the BookingAndMessage in the proper sort order.
Option #2: To solve the problem, as it stands, you will want to use PHP's usort function (also explained here PHP Sort a multidimensional array by element containing date).
<?php
$BookingsAndMessages = array_merge($bookings, $messages);
function date_compare($a, $b)
{
$modelKeyA = array_key_exists('Booking',$a) ? 'Booking' : 'MessageDetail';
$modelKeyB = array_key_exists('Booking',$b) ? 'Booking' : 'MessageDetail';
$t1 = strtotime($a[$modelKeyA]['created']);
$t2 = strtotime($b[$modelKeyB]['created']);
return $t1 - $t2;
}
usort($BookingsAndMessages, 'date_compare');
// Would sort the merged records by `created` date
?>
The downside to option 2 is, you are going to have to create rules for each field (and sort direction).
Perhaps you should Union your queries first, the Order.
$bookings = $this->Bookings->find('all', [
'conditions' => $conditions,'limit' =>10,
]);
$messages = $this->find('all', [
'conditions' => $conditions,'limit' =>10
]);
$data = $bookings->union($messages);
// This works if your first selected field is the one you want to sort.
$data->epilog('ORDER BY 1 ASC');
reference:
How do you modify a UNION query in CakePHP 3?

Resources