通过连接表获取不同数组中的提取结果
Get fetched result in different arrays by joined tables
我想制作一个 Post
对象,其中包含其关联的 table User
的实例,最简单的方法是我可以访问用户结果和 post结果分开。
查询:
$query = $this->connection->newQuery()->from('post');
$query->select(
[
'post.message',
'post.created_at',
'user.name',
'user.email',
'user.role',
'user.status',
]
)->join(['table' => 'user', 'conditions' => 'post.user_id = user.id']);
$postsRows = $query->execute()->fetchAll('assoc');
上面的查询输出结果行的数组,我无法知道该列是来自 user
table 还是 post
table。
$postsRows = array (
0 =>
array (
'message' => 'This is a test post',
'created_at' => '2021-01-01 00:00:01',
'name' => 'Admin Example',
'email' => 'admin@example.com',
'role' => 'admin',
'status' => 'active',
),
1 =>
array (
'message' => 'This is another test post',
'created_at' => '2021-01-01 00:00:01',
'name' => 'User Example',
'email' => 'user@example.com',
'role' => 'user',
'status' => 'active',
),
)
当然我可以为每一列制作别名
$query = $this->connection->newQuery()->from('post');
$query->select(
[
'post.*',
'user_name' => 'user.name',
'user_email' => 'user.email',
'user_role' => 'user.role',
'user_status' => 'user.status',
]
)->join(['table' => 'user', 'conditions' => 'post.user_id = user.id']);
$postsRows = $query->execute()->fetchAll('assoc');
但是后来我必须遍历它们并稍后手动拆分数组,我无法使用表达式 *
为 post 列名称起别名,并且当加入相同的 table.
我想理想的是以下输出。这样的事情可能吗?
$postsRows = array(
0 => array(
'post' => array(
'message' => 'This is a test post',
'created_at' => '2021-01-01 00:00:01',
),
'user' => array(
'name' => 'Admin Example',
'email' => 'admin@example.com',
'role' => 'admin',
'status' => 'active',
)
),
1 => array(
'post' => array(
'message' => 'This is another test post',
'created_at' => '2021-01-01 00:00:01',
),
'user' => array(
'name' => 'User Example',
'email' => 'user@example.com',
'role' => 'user',
'status' => 'active',
),
),
)
如讨论的那样 here, the workflow that interests me is more "use case" based, and then it doesn't make sense to do what I requested (as it would kind of be reinventing ORM which I did first)。
对我有用的是创建具有高内聚力的特定 DTO,例如 UserPost
,在这种情况下,table 的来源不相关,因为字段是单独选择的,非常精确。
我想制作一个 Post
对象,其中包含其关联的 table User
的实例,最简单的方法是我可以访问用户结果和 post结果分开。
查询:
$query = $this->connection->newQuery()->from('post');
$query->select(
[
'post.message',
'post.created_at',
'user.name',
'user.email',
'user.role',
'user.status',
]
)->join(['table' => 'user', 'conditions' => 'post.user_id = user.id']);
$postsRows = $query->execute()->fetchAll('assoc');
上面的查询输出结果行的数组,我无法知道该列是来自 user
table 还是 post
table。
$postsRows = array (
0 =>
array (
'message' => 'This is a test post',
'created_at' => '2021-01-01 00:00:01',
'name' => 'Admin Example',
'email' => 'admin@example.com',
'role' => 'admin',
'status' => 'active',
),
1 =>
array (
'message' => 'This is another test post',
'created_at' => '2021-01-01 00:00:01',
'name' => 'User Example',
'email' => 'user@example.com',
'role' => 'user',
'status' => 'active',
),
)
当然我可以为每一列制作别名
$query = $this->connection->newQuery()->from('post');
$query->select(
[
'post.*',
'user_name' => 'user.name',
'user_email' => 'user.email',
'user_role' => 'user.role',
'user_status' => 'user.status',
]
)->join(['table' => 'user', 'conditions' => 'post.user_id = user.id']);
$postsRows = $query->execute()->fetchAll('assoc');
但是后来我必须遍历它们并稍后手动拆分数组,我无法使用表达式 *
为 post 列名称起别名,并且当加入相同的 table.
我想理想的是以下输出。这样的事情可能吗?
$postsRows = array(
0 => array(
'post' => array(
'message' => 'This is a test post',
'created_at' => '2021-01-01 00:00:01',
),
'user' => array(
'name' => 'Admin Example',
'email' => 'admin@example.com',
'role' => 'admin',
'status' => 'active',
)
),
1 => array(
'post' => array(
'message' => 'This is another test post',
'created_at' => '2021-01-01 00:00:01',
),
'user' => array(
'name' => 'User Example',
'email' => 'user@example.com',
'role' => 'user',
'status' => 'active',
),
),
)
如讨论的那样 here, the workflow that interests me is more "use case" based, and then it doesn't make sense to do what I requested (as it would kind of be reinventing ORM which I did first)。
对我有用的是创建具有高内聚力的特定 DTO,例如 UserPost
,在这种情况下,table 的来源不相关,因为字段是单独选择的,非常精确。