2016-11-21 44 views
0

SQL查询后,我已经进行下面的SQL语句,并已获得表中的相关行。Access数据在CakePHP中

SQL;

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

它从'UserVerifications'表中获得'code'列等于'hash'值的行。这个位有效,见下面的输出。

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' 

    } 
] 

} 

根据CakePHP的网站,或者至少我读的方式,我应该能够通过执行以下操作来访问值ID;

echo $user->id; 

通知(8):未定义的属性:蛋糕\ ORM \的ResultSet :: $ ID [APP /控制器\ UsersController.php 58行]

我失去了一些东西简单?

回答

2

,试试这个:

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

现在你可以使用:

$user->id; 

来访问id的值等等。如果你需要表中的多行,请使用@ Felippe的答案。

+0

正是我需要的,谢谢! – ChrisBull