PHP Error
Description
Trying to get property of non-object
Source File
C:\wamp\www\test\protected\controllers\AccountController.php(256)
00244:
00245: /**
00246: * Reset a user password.
00247: */
00248: public function actionReset()
00249: {
00256: $email = $success->emails->email;
Please advise what I am doing wrong.
I simply want to get the user email address and store it, as I use this later in my behaviour.
$success shouldn’t be empty. Here is the sql when queried;
SELECT `t`.`id` AS `t0_c0`, `t`.`is_active` AS `t0_c1`,
`t`.`role_id` AS `t0_c2`, `t`.`password` AS `t0_c3`, `t`.`nonce` AS
`t0_c4`, `t`.`last_visit` AS `t0_c5`, `t`.`is_super_admin` AS `t0_c6`,
`t`.`expire_at` AS `t0_c7`, `t`.`created_at` AS `t0_c8`, `t`.`updated_at`
AS `t0_c9`, `t`.`created_by` AS `t0_c10`, `t`.`updated_by` AS `t0_c11`,
`e`.`user_id` AS `t1_c0`, `e`.`email` AS `t1_c1`, `e`.`label` AS `t1_c2`,
`e`.`is_primary` AS `t1_c3`, `e`.`is_active` AS `t1_c4`, `e`.`nonce` AS
`t1_c5`, `e`.`expire_at` AS `t1_c6`, `e`.`created_at` AS `t1_c7`,
`e`.`updated_at` AS `t1_c8`, `e`.`created_by` AS `t1_c9`, `e`.`updated_by`
AS `t1_c10` FROM `User` `t` LEFT OUTER JOIN `Email` `e` ON
(`e`.`user_id`=`t`.`id`) WHERE (`t`.`nonce`=:yp0 AND (t.is_active=1 AND
e.is_primary=1)). Bind with parameter :yp0='qveq5fu6cv5p'
When copied into phpmyadmin and replacing :ypo with parameter;
So, as the error states, you try to access a property of a non object: $success->emails is an array, that has no properties. You should fetch the first entry from that array to get your desired record. Quick way to make this safe:
Per definition a HAS_MANY relation has many related objects. An Array is already the easiest representation of such a list. If that’s still too complicated, you can define another relation for the primary email to the same table:
public function relations() {
return array(
'primary_email'=>array(self::HAS_ONE,'PEmail','user_id',array(
'condition'=>'primary_email.id_active=1 AND e.is_primary=1',
)),
...
);
}