duankong8998 2016-11-21 20:33
浏览 46
已采纳

在CakePHP中进行sql查询后访问数据

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条回答 默认 最新

  • douzhi2988 2016-11-22 00:46
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示