我在文档中没有找到如何选择嵌套属性。 我需要这样的结果:
{
"data": [
{
"Id": 3,
"Fname": "Fname",
"Lname": "Lname",
"Mname": "Mname",
"UserId": 1549,
"Login": "manager",
"Email": "manager@email.ru",
"Password": null,
"UserRole": [
{
UserRole object
},
{
UserRole object
},
{
UserRole object
}
]
}
],
"totalCount": 1
}
但是我的代码
$items = PanelUserQuery::create()
->joinWithUser()
;
$itemsCollection = $items->find();
$totalCount = $items->count();
$itemsArray = $itemsCollection ? $itemsCollection->toArray(null, null, TableMap::TYPE_PHPNAME, true) : [];
var_dump($itemsArray);
返回此数组:
{
"data": [
{
"Id": 3,
"Fname": "Fname",
"Lname": "Lname",
"Mname": "Mname",
"UserId": 1549,
"Login": "manager",
"Email": "manager@email.ru",
"Password": null,
"User": {
"Id": 1549,
"Login": "manager",
"Email": "manager@mail.ru",
"Password": null,
"PanelUsers": [
"*RECURSION*"
]
}
}
],
"totalCount": 1
}
我的架构: 用户表
<table name="user">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="login" type="varchar" size="999"/>
<column name="email" type="varchar" size="999"/>
<column name="password" type="varchar" size="40"/>
</table>
角色表
<table name="role">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="name" type="varchar" size="999" required="true"/>
<column name="guid" type="varchar" size="999" required="true"/>
<unique>
<unique-column name="name"/>
</unique>
<unique>
<unique-column name="guid"/>
</unique>
</table>
用户角色表
<table name="user_role" isCrossRef="true">
<column name="user_id" type="integer" primaryKey="true"/>
<column name="role_id" type="integer" primaryKey="true"/>
<foreign-key foreignTable="user">
<reference local="user_id" foreign="id"/>
</foreign-key>
<foreign-key foreignTable="role">
<reference local="role_id" foreign="id"/>
</foreign-key>
<unique>
<unique-column name="user_id"/>
<unique-column name="role_id"/>
</unique>
</table>
面板用户表
<table name="panel_user">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="fname" type="varchar" size="999"/>
<column name="lname" type="varchar" size="999"/>
<column name="mname" type="varchar" size="999"/>
<column name="user_id" type="integer" required="true" />
<foreign-key foreignTable="user" onDelete="CASCADE">
<reference local="user_id" foreign="id" />
</foreign-key>
<behavior name="delegate">
<parameter name="to" value="user" />
</behavior>
</table>
我不明白如何检索用户角色属性并传递给面板用户对象。请帮助了解查询方法或文本一些代码
您还不必平展整个结果集(->toArray()
(,您可以在$itemsCollection->UserRole上循环执行getUserRole()
。
foreach ($itemsCollection->getUserRole() as $userRole) {
var_dump($userRole->toArray());
}