Cakephp 3.3 - 如何在加入时删除嵌套级别 table
Cakephp 3.3 - How to remove nested level when join table
我正在使用 CakePHP 3.3 并尝试通过连接获取数据 table
$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"
}
],
我可以在数组中获取同一级别的所有数据吗?感谢阅读!
您可以在 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}]
我正在使用 CakePHP 3.3 并尝试通过连接获取数据 table
$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"
}
],
我可以在数组中获取同一级别的所有数据吗?感谢阅读!
您可以在 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}]