我正在使用CakePHP 3.3并尝试通过连接表获取数据
$this->find()
->select([
'TableA.id',
'TableA.user_id',
'TableA.note',
'TableB.log_date'
])
->where([
'TableA.user_id' => $userId
])
->join([
'table' => 'table_b',
'alias' => 'TableB',
'conditions' => 'TableA.table_b_id = TableB.id',
]);
我尝试var_dump()结果var_dump($result->toArray());die;
我的数组显示如下:
"data": [
{
"id": 1317,
"user_id": 331,
"note": "Take note",
"TableB": {
"log_date": "2017-11-16"
}
},
{
"id": 1318,
"user_id": 331,
"note": "Take note",
"TableB": {
"log_date": "2017-11-16"
}
}
],
我的结果已嵌套在TableB
中。我如何删除嵌套和显示如下:
"data": [
{
"id": 1317,
"user_id": 331,
"note": "Take note",
"log_date": "2017-11-16"
},
{
"id": 1318,
"user_id": 331,
"note": "Take note",
"log_date": "2017-11-16"
}
],
我可以获取阵列中同一级别的所有数据吗?谢谢你的阅读!
答案 0 :(得分:0)
您可以使用select()
$result = $this
->Articles
->find()
->select([
'Articles.id',
'user_id' => 'users.id'
])
->join([
'table' => 'users',
'conditions' => 'users.id = Articles.user_id'
])
->toArray();
结果样本:
[{"id":26,"user_id":2},{"id":27,"user_id":2}]