0

I have performed the sql statement below and it has obtained the relevant row from the table.

sql;

$user = $this->UserVerifications
                ->find()
                ->where(['code' => $hash])
                ->all();

It is getting the row from the table 'UserVerifications' where the column 'code' is equal to the value of 'hash'. This bit works, see below for output.

debug($user);
\src\Controller\UsersController.php (line 57) object(Cake\ORM\ResultSet) {

'items' => [
    (int) 0 => object(App\Model\Entity\UserVerification) {

        'id' => (int) 23,
        'code' => '4206e38996fae4028a26d43b24f68d32',
        'date_created' => object(Cake\I18n\FrozenTime) {

            'time' => '2016-11-21T18:48:45+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        '[new]' => false,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'UserVerifications'

    }
]

}

According to the CakePHP website, or at least the way I'm reading it, I should be able to access the value for id by doing the following;

echo $user->id;

Notice (8): Undefined property: Cake\ORM\ResultSet::$id [APP/Controller\UsersController.php, line 58]

Am I missing something simple?

2 Answers 2

3

Your $user is a collection/ResultSet, not a UserVerifications instance.

http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html

You should iterate over it, like:

foreach($user as $u) {
    echo $u->id;
}
Sign up to request clarification or add additional context in comments.

Comments

2

If you need particular row only from table,try this:

$user = $this->UserVerifications
            ->find()
            ->where(['code' => $hash])
            ->first();

And now you can use:

$user->id;

to access value of id and so on. And if you need multiple rows from the table go with @Felippe's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.