How to get data of both table using join in codeigniter - php

I have two array
Array
(
[0] => stdClass Object
(
[id] => 14
[employee_id] => ST0011
[emp_name] => Munish Sharma
[gender] =>
[marital_status] =>
[address] =>
[postal_code] =>
[home_phone] =>
[mobile_phone] =>
[work_email] =>
[private_email] =>
[joined_date] => 1970-01-01 00:00:00
[emp_salary] => 0
[confirmation_date] => 0000-00-00 00:00:00
[department] => HRM
[f_name] => fname
[dob] => 1970-01-01
[c_address] =>
[domainName] =>
[emp_status] => active
)
[1] => stdClass Object
(
[id] => 12
[employee_id] => ST001dasd
[emp_name] => Rakesh Negi
[gender] =>
[marital_status] =>
[address] =>
[postal_code] =>
[home_phone] =>
[mobile_phone] =>
[work_email] =>
[private_email] =>
[joined_date] => 2015-08-11 00:00:00
[emp_salary] => 0
[confirmation_date] => 0000-00-00 00:00:00
[department] => HRM
[f_name] => dsad
[dob] => 1970-01-01
[c_address] =>
[domainName] =>
[emp_status] => active
)
[2] => stdClass Object
(
[id] => 13
[employee_id] => ST001
[emp_name] => Rakesh Negi
[gender] =>
[marital_status] =>
[address] =>
[postal_code] =>
[home_phone] =>
[mobile_phone] =>
[work_email] =>
[private_email] =>
[joined_date] => 2015-08-06 00:00:00
[emp_salary] => 0
[confirmation_date] => 0000-00-00 00:00:00
[department] => HRM
[f_name] => Father Name
[dob] => 1970-01-01
[c_address] =>
[domainName] =>
[emp_status] => active
)
)
and the second array is this
Array
(
[0] => stdClass Object
(
[id] => 55
[emp_id] => ST001
[check_in] => 11:38:09
[check_out] => 00:00:00
[total_time] => 00:00:00
[status] =>
[date_time] => 2015-10-20
[emp_name] => Rakesh Negi
)
)
Now I want to make it like this
Array
(
[0] => stdClass Object
(
[id] => 14
[employee_id] => ST0011
[emp_name] => Munish Sharma
[gender] =>
[marital_status] =>
[address] =>
[postal_code] =>
[home_phone] =>
[mobile_phone] =>
[work_email] =>
[private_email] =>
[joined_date] => 1970-01-01 00:00:00
[emp_salary] => 0
[confirmation_date] => 0000-00-00 00:00:00
[department] => HRM
[f_name] => fname
[dob] => 1970-01-01
[c_address] =>
[domainName] =>
[emp_status] => active
)
[1] => stdClass Object
(
[id] => 12
[employee_id] => ST001dasd
[emp_name] => Rakesh Negi
[gender] =>
[marital_status] =>
[address] =>
[postal_code] =>
[home_phone] =>
[mobile_phone] =>
[work_email] =>
[private_email] =>
[joined_date] => 2015-08-11 00:00:00
[emp_salary] => 0
[confirmation_date] => 0000-00-00 00:00:00
[department] => HRM
[f_name] => dsad
[dob] => 1970-01-01
[c_address] =>
[domainName] =>
[emp_status] => active
)
[3] => stdClass Object
(
[id] => 55
[emp_id] => ST001
[check_in] => 11:38:09
[check_out] => 00:00:00
[total_time] => 00:00:00
[status] =>
[date_time] => 2015-10-20
[emp_name] => Rakesh Negi
)
)
I want to remove the value from first array if the em_id in the both array will be same is it possible in?
I tried using where condion with join like this
$date = date('Y-m-d');
$this->db->select('hrm_attendance.*,employees.emp_name');
$this->db->from('hrm_attendance');
$this->db->join('employees','hrm_attendance.emp_id=employees.employee_id','LEFT OUTER');
$this->db->where('hrm_attendance.date_time = "'.$date.'"');
$this->db->order_by('check_in','asc');
$query = $this->db->get();
return $query->result();
and getting this output
(
[0] => stdClass Object
(
[id] => 55
[emp_id] => ST001
[check_in] => 11:38:09
[check_out] => 00:00:00
[total_time] => 00:00:00
[status] =>
[date_time] => 2015-10-20
[emp_name] => Rakesh Negi
)
)
But I want the output I have described above
if I remove $this->db->where('hrm_attendance.date_time = "'.$date.'"'); line then I am getting the all record from both the table I want all the data from the employee table with all ID and only those data from the hrm_attendance where the emp_id is available with where condition
$this->db->where('hrm_attendance.date_time = "'.$date.'"');

Please check the Code igniter Docs
http://www.codeigniter.com/userguide2/database/active_record.html#select
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
// Produces:
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id
in select('*') you can specify your limited fields also.

In your db->where is incorrect $this->db->where('hrm_attendance.date_time = "'.$date.'"');
http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
public function some_name() {
$date = date('Y-m-d');
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'hrm_attendance ha', 'LEFT');
$this->db->join($this->db->dbprefix . 'employees e', 'e.employee_id = ha.emp_id', 'LEFT');
$this->db->where('ha.date_time', $date);
$this->db->order_by('check_in','ASC');
$query = $this->db->get();
return $query->result();
// Or Try With result_array();
// return $query->result_array();
}

I find out the solution by adding some code like this
$date = date('Y-m-d');
$this->db->select('hrm_attendance.*,employees.emp_name');
$this->db->from('hrm_attendance');
$this->db->join('employees','hrm_attendance.emp_id=employees.employee_id','LEFT OUTER');
$this->db->where('hrm_attendance.date_time = "'.$date.'"');
$this->db->order_by('check_in','asc');
$query = $this->db->get();
$result = $query->result();
$count = count($result);
for($i = 0;$i<$count;$i++)
{
$id[] = $data['user_logged_in'][$i]->emp_id;
}
$this->db->select('*');
$this->db->from('employees');
$this->db->where_not_in('employee_id',$id);
$query = $this->db->get();
$data['absent'] = $query->result();
print_r($data['absent']);
By using this I got the all those employee who are not in the table hrm_attendance

Related

Yii 2 how to get all data within current day

Basically ProductOffer table.
Migration
$this->createTable('product_offer', [
'id' => $this->primaryKey(),
'product_id' => $this->bigInteger(20) . ' NOT NULL',
'customer_id' => $this->bigInteger(20),
'coupon_code' => $this->string(),
'offer' => $this->string(),
'customer_name' => $this->string(),
'email_address' => $this->string(),
'phone_number' => $this->string(),
'note' => $this->text(),
'admin_note' => $this->text(),
'status' => $this->tinyInteger(),
'created' => $this->dateTime() . ' NOT NULL',
'modified' => $this->dateTime(),
]);
Now I have data in this table and created query.
$stamp = mktime(0, 0, 0);
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
['>', 'created', date('m-d-Y H:i:s',$stamp)]
]);
echo "<pre>";
print_r($testQuery->all());
echo "</pre>";
The result is
Array
(
[0] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 48
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-16 04:38:28
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 48
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => 0
[created] => 2018-05-16 04:38:28
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[1] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 50
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-17 04:39:55
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 50
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => 0
[created] => 2018-05-17 04:39:55
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[2] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 51
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-17 04:43:29
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 51
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => test
[admin_note] =>
[status] => 0
[created] => 2018-05-17 04:43:29
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[3] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 55
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-17 04:51:57
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 55
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => 0
[created] => 2018-05-17 04:51:57
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
);
Today in our time zone is 5/17/2018, I want to get data that created today from but it seems my query is not excluding the data that created yesterday.
I already try expression this
['<', 'created', new Expression('NOW()')]
and this
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
['>', 'created', new Expression(date('m-d-Y H:i:s', $stamp))]
]);
echo "<pre>";
print_r($testQuery->all());
echo "</pre>";
And now I'm getting an SQL error
Database Exception – yii\db\Exception SQLSTATE[42000]: Syntax error or
access violation: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '00:00:00)' at line 1 The SQL being executed was:
SELECT * FROM product_offer WHERE (status = 0) AND (product_id =
'111') AND (email_address = 'asrockg07#gmail.com') AND (created >
05-17-2018 00:00:00)
You may use raw SQL content with a WHERE call in string format, and it is also possible to and together two WHERE calls. Putting this together, we can try the following:
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address]
])
->andWhere('created >= CURDATE()');
Note that I think you want to use CURDATE here, which corresponds to today at midnight, if you want to restrict to records created at any point today.
You're using incorrect format for date. It should be date('Y-m-d H:i:s', $stamp) for passed stamp or date('Y-m-d 00:00:00', $stamp) to force midnight.
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
['>', 'created', date('Y-m-d 00:00:00', $stamp)]
]);
Also you're using yii\db\Expression in a wrong way. You cannot pass value directly to Expression, this may open you to SQL Injection. You should use params if you need this.
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
new Expression('created > :date', [':date' => date('Y-m-d 00:00:00', $stamp)]),
]);

Remove duplicate item from multidimensional array

How would I go about removing duplicate items from my multidimensional object array?
In my sample below, I have 2 items which contain the id value of 4 (fullname is 'Jodie (Y6Y5)').
I need to modify my array so that there are no duplicate id values.
I've tried doing loops to fix it, and I've also tried array_unique().
Does anyone have any better ideas on how I can achieve this?
Example input array:
Array
(
[0] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 4
[pupil_id] => 1
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Jodie (Y6Y5)
[email] => jodie#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] => 07777777777
[username] => jerrys
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 14:23:02
[updated] => 2018-02-14 14:18:08
[login] => 2018-02-13 15:45:09
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[1] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 3
[pupil_id] => 5
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Karla (Rec Y5)
[email] => karla#email.co.uk
[email_2] => ally#email.com
[telephone] =>
[telephone_2] =>
[username] => rickygutpa
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 10:33:30
[updated] => 2018-02-14 14:16:21
[login] => 0000-00-00 00:00:00
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[2] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 1
[pupil_id] => 4
[pupil_id_1] => 0
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Florence (Y6 2)
[email] => florence#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] =>
[username] => mrslacey
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-01 09:47:34
[updated] => 2018-02-14 14:49:32
[login] => 2018-02-05 11:48:54
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 87
)
[3] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 4
[pupil_id] => 1
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Jodie (Y6Y5)
[email] => jodie#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] => 07777777777
[username] => jerrys
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 14:23:02
[updated] => 2018-02-14 14:18:08
[login] => 2018-02-13 15:45:09
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 87
)
)
Expect Result:
Array
(
[0] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 4
[pupil_id] => 1
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Jodie (Y6Y5)
[email] => jodie#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] => 07777777777
[username] => jerrys
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 14:23:02
[updated] => 2018-02-14 14:18:08
[login] => 2018-02-13 15:45:09
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[1] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 3
[pupil_id] => 5
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Karla (Rec Y5)
[email] => karla#email.co.uk
[email_2] => ally#email.com
[telephone] =>
[telephone_2] =>
[username] => rickygutpa
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 10:33:30
[updated] => 2018-02-14 14:16:21
[login] => 0000-00-00 00:00:00
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[2] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 1
[pupil_id] => 4
[pupil_id_1] => 0
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Florence (Y6 2)
[email] => florence#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] =>
[username] => mrslacey
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-01 09:47:34
[updated] => 2018-02-14 14:49:32
[login] => 2018-02-05 11:48:54
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 87
)
)
Best practice (and a technique that you will find implemented in Stackoverflow questions every day) is to assign temporary keys, overwrite any pre-existing subarrays based on the temporary keys, then remove the temporary keys at the end.
This avoids having to do iterated lookups. For highest efficiency in your codes, try to minimize iterated function calls.
Method #1: Remove earlier duplicate occurrences / Retain last occurrences (Demo)
foreach($guardian->guardians as $subarray){
$result[$subarray->id]=$subarray; // assign temporary keys
}
$guardian->guardians=array_values($result); // re-declare and remove temporary keys (re-index the subarrays)
var_export($guardian);
*Alternatively, here is a functional-style one-liner providing the same effect: (Demo)
$guardian->guardians=array_values(array_column((array)$guardian->guardians,NULL,'id'));
var_export($guardian);
For those who don't know, array_column() chokes on objects, so objects must be temporarily converted to arrays.
Method #2: Remove later duplicate occurrences / Retain first occurrences (Demo)
foreach($guardian->guardians as $subarray){
if(!isset($result[$subarray->id])){
$result[$subarray->id]=$subarray; // only store if first occurrence of id
}
}
$guardian->guardians=array_values($result); // re-index
var_export($guardian);
I've managed to get a working answer by doing as following:
$tempGuardians = array();
foreach($guardian->guardians as $key => $value){
if (in_array($value->id, $tempGuardians)){
unset($guardian->guardians[$key]);
} else {
array_push($tempGuardians, $value->id);
}
}
unset($tempGuardians);
If anyone knows any cleaner ways or better ways of doing this, I'm open to new ways.
The answer is included on your question, you already said it: array_unique:
$newArray=array_unique($a1);

Retrieving attribute from query

How would I go about getting the balance from this result?
Braintree_Customer Object
(
[_attributes:protected] => Array
(
[id] => 425865
[merchantId] => abcddeff
[firstName] => Waka
[lastName] => Flaka
[company] => Waka Flaka Co.
[email] => demoemail#gmail.com
[phone] => 281.330.8004
[fax] => 419.555.1235
[website] => http://example.com
[createdAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[updatedAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[customFields] =>
[creditCards] => Array
(
[0] => Braintree_CreditCard Object
(
[_attributes] => Array
(
[bin] => 400551
[expirationMonth] => 09
[expirationYear] => 2020
[last4] => 0004
[billingAddress] => Braintree_Address Object
(
[_attributes] => Array
(
[id] => gx
[customerId] => 30007705
[firstName] => Flaka
[lastName] => Waka
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
[countryName] =>
[createdAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[updatedAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
)
)
[cardType] => Visa
[cardholderName] => Flaka Waka
[commercial] => Unknown
[countryOfIssuance] =>
[createdAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[customerId] => 30007705
[customerLocation] => US
[debit] => Yes
[default] => 1
[durbinRegulated] => Yes
[expired] =>
[healthcare] => Unknown
[imageUrl] => https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox
[issuingBank] => Unknown
[payroll] => Unknown
[prepaid] => No
[subscriptions] => Array
(
[0] => Braintree_Subscription Object
(
[_attributes] => Array
(
[addOns] => Array
(
)
[balance] => 0.00
My code to receive the cardType is
$cardtype = $customer->creditCards[0]->cardType;
You can try this
$balance = $customer->creditCards[0]->subscriptions[0]->balance;

Get Value from an array with foreach

i've an format array like this
Array (
[0] => stdClass Object ( [subscriptionContractId] => 20878 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-02 03:27:39.000 [endDate] => 2015-07-04 03:27:39.000 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 2 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => 2015-07-03 03:27:39.000 [lastPaymentDate] => 2015-07-02 03:27:39.000 [subscriptionContractHistory] =>Array ( [0] => stdClass Object ( [subscriptionContractId] => 20878 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-07-01 03:27:53.290 ) ) [subscriptionContractLogs] => )
[1] => stdClass Object ( [subscriptionContractId] => 20861 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:31:47.000 [endDate] => 2015-06-30 11:32:05.150 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:31:47.000 [subscriptionContractHistory] => Array ( [0] =>
stdClass Object ( [subscriptionContractId] => 20861 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:31:55.530 ) [1] => stdClass Object ( [subscriptionContractId] => 20861 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:32:05.977 ) ) [subscriptionContractLogs] => )
[2] => stdClass Object ( [subscriptionContractId] => 20860 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:29:37.000 [endDate] => 2015-06-30 11:30:19.887 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:29:37.000 [subscriptionContractHistory] => Array ( [0] => stdClass Object ( [subscriptionContractId] => 20860 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:10.267 ) [1] => stdClass Object ( [subscriptionContractId] => 20860 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:20.687 ) ) [subscriptionContractLogs] => )
[3] => stdClass Object ( [subscriptionContractId] => 20859 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:27:33.000 [endDate] => 2015-06-30 11:27:57.683 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:27:33.000 [subscriptionContractHistory] => Array ( [0] => stdClass Object ( [subscriptionContractId] => 20859 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:42.173 ) [1] => stdClass Object ( [subscriptionContractId] => 20859 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:58.467 ) ) [subscriptionContractLogs] => )
i want to take this value [subscriptionContractId] => 20859
PFB my code
foreach($getContract as $key =>$value) {
$cancel = $getContract[$key]->subscriptionContractId;
$this->data['res'] = $cancel;
}
with that above code only showing single value of record, meanwhile record is around 27 records.
and another code, i try to rectify with initialize array, PFB.
$cancel = array()
foreach($getContract as $key =>$value) {
$cancel[] = $getContract[$key]->subscriptionContractId;
$this->data['res'] = $cancel;
}
i need only the value, of the array such as 20879, etc. eventhough i use double loop to extract particular array it only show the last record on 10663.
Please help
You can modify your code as follows (see php documenation about foreach for more background):
$cancel = array();
foreach($getContract as $value) {
$cancel[] = $value->subscriptionContractId;
...
}
This way, in every iteration $value is assigned one value from your getContract array, with it's key just being omitted. Then you can store the value's subscriptionContractId in your $cancel array to do whatever you are planning to do.
Note: briosheje is right about the line:
$this->data['res'] = $cancel;
$this->data['res'] is overwritten in every iteration. As you are overwriting it with the your $cancel array, your code should still work, but your doing unnecessary work here, as your array might be subjected to further change in the next iteration.
My advice would be to move this line after your loop brackets ended, so that your result is saved just once:
...
foreach($getContract as $value) {
...
}
$this->data['res'] = $cancel;

CakePHP Threaded "Find" with Associated Models

I am using a $this->find('threaded') in my CakePHP application and am trying to pull in an associated model within the find (HABTM). I have tried several methods, such as 'joins', 'recursive' and 'contains' all with no luck. I am using CakePHP 2.3.6
Here is my (working) code.
class EventsController extends AppController {
public function promoters($id = null) {
$options = array('conditions' => array('Event.id' => $id));
$event = $this->Event->find('first', $options);
$this->set('event', $event);
$this->loadModel('EventsPromoter');
$treelistConditions = array(
'conditions' => array(
'event_id' => $id
),
);
$promoterTree = $this->EventsPromoter->find('threaded', $treelistConditions);
$this->set('promoters', $promoterTree);
}
}
This results in the following array output
Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 1
[promoter_id] => 1
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 10
[promoter_id] => 4
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 13
[promoter_id] => 6
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 2
[promoter_id] => 2
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 11
[promoter_id] => 5
[event_id] => 1
[parent_id] => 2
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 6
[promoter_id] => 3
[event_id] => 1
[parent_id] => 11
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 14
[promoter_id] => 7
[event_id] => 1
[parent_id] => 2
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[children] => Array
(
)
)
)
)
)
What I would like my code to output is the following
Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 1
[promoter_id] => 1
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[Promoter] => Array
(
[promoter_id] => 1
[first_name] => 'Bob'
[last_name] => 'Smith'
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 10
[promoter_id] => 4
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 4
[first_name] => 'Sally'
[last_name] => 'Sue'
)
[children] => Array
(
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 13
[promoter_id] => 6
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 6
[first_name] => 'Ben'
[last_name] => 'King'
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 2
[promoter_id] => 2
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[Promoter] => Array
(
[promoter_id] => 2
[first_name] => 'Jack'
[last_name] => 'Sparrow'
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 11
[promoter_id] => 5
[event_id] => 1
[parent_id] => 2
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 5
[first_name] => 'Jane'
[last_name] => 'Doe'
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 6
[promoter_id] => 3
[event_id] => 1
[parent_id] => 11
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 3
[first_name] => 'Mike'
[last_name] => 'Jones'
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 14
[promoter_id] => 7
[event_id] => 1
[parent_id] => 2
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[Promoter] => Array
(
[promoter_id] => 7
[first_name] => 'Spider'
[last_name] => 'Man'
)
[children] => Array
(
)
)
)
)
)
Nothing I have been trying is working, and this seems like something Cake should be able to handle fairly easily. Thanks in advance for any help!
I was taking a slightly wrong approach for what I was trying to accomplish. While I still think this should be possible from my EventsController class, I elected to make it in the EventsPromotersController HABTM class. The code is much simpler using this method
class EventsPromotersController extends AppController {
public function event($event_id = null) {
$options = array('conditions' => array('event_id' => $event_id));
$promoters = $this->EventsPromoter->find('threaded', $options);
$this->set('promoters', $promoters);
}
}

Categories