Yii 1.1:在两个模型之间获取 HAS_MANY
Yii 1.1: Fetching HAS_MANY between two models
我们的平台是使用 Yii 1.1 构建的。我们有具有 staff
角色的用户,他们被授予了权限。
client_staff
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(20) | YES | | NULL | |
| clientId | int(11) unsigned | NO | MUL | NULL | |
| emailAddress | text | NO | | NULL | |
| firstName | varchar(45) | YES | | NULL | |
| lastName | varchar(45) | YES | | NULL | |
| archivedAt | datetime | YES | | NULL | |
| createdAt | datetime | YES | | NULL | |
| updatedAt | datetime | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
staff_permissions
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(20) | YES | UNI | NULL | |
| staffId | int(11) unsigned | NO | MUL | NULL | |
| permissionId | int(11) unsigned | NO | MUL | NULL | |
| createdAt | datetime | YES | | NULL | |
| updatedAt | datetime | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
permissions
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(20) | YES | UNI | NULL | |
| description | varchar(100) | NO | | NULL | |
| createdAt | datetime | YES | | NULL | |
| updatedAt | datetime | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
一个client_staff
有多个staff_permissions
,属于permission
.
使用框架,我想获取 staff_permissions
和 client_staff
,所以这就是我尝试在 ClientStaff
中配置 relations
的方式:
return [
'client' => [self::BELONGS_TO, User::class, 'clientId'],
'staffPermission' => [self::HAS_MANY, StaffPermission::class, 'id'],
]
在StaffPermission
中:
return [
'staff' => [self::BELONGS_TO, ClientStaff::class, 'staffId'],
'permissions' => [self::HAS_MANY, Permission::class, 'permissionId'],
]
在Permission
中:
return [
'staffPermissions' => [self::BELONGS_TO, StaffPermission::class, 'id'],
]
我们有一个 toAPICopy
方法将数据带到 JSON 中的客户端,在这个方法中,对于 类 BELONGS_TO
,我们利用每个类' toAPICopy
在返回的数组中。下面是 ClientStaff
的情况
public function toAPICopy()
{
return [
'id' => $this->id,
'client' => $this->client->toAPICopy(),
...,
'staffPermission' => $this->staffPermission->toAPICopy()
];
}
但没有任何反应,应用程序挂起。没有获取数据,也没有显示错误,api.log
文件中没有任何东西可以帮助我。我不知道我做错了什么。
我觉得你在建立关系方面遇到了一些问题。
这些是根据您的 table 结构的关系。
ClientStaff 应该是:
return [
'client' => [self::BELONGS_TO, User::class, 'clientId'],
'staffPermission' => [self::HAS_MANY, StaffPermission::class, 'staffId'],
]
在 StaffPermission 中应该是:
return [
'staff' => [self::BELONGS_TO, ClientStaff::class, 'staffId'],
'permissions' => [self::BELONGS_TO, Permission::class, 'permissionId'],
]
并且在 Permission 中应该是:
return [
'staffPermissions' => [self::BELONGS_TO, StaffPermission::class, 'permissionId'],
]
因此,HAS_MANY
将 return 一个对象数组,因此,您不能直接在其上调用方法。对我有用的解决方案是:
public function toAPICopy() {
foreach ($this->staffPermissions as $p) {
array_push($permissions, $p->toAPICopy();
}
return [ ... 'staffPermissions' => $permissions, ... ];
}
我们的平台是使用 Yii 1.1 构建的。我们有具有 staff
角色的用户,他们被授予了权限。
client_staff
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(20) | YES | | NULL | |
| clientId | int(11) unsigned | NO | MUL | NULL | |
| emailAddress | text | NO | | NULL | |
| firstName | varchar(45) | YES | | NULL | |
| lastName | varchar(45) | YES | | NULL | |
| archivedAt | datetime | YES | | NULL | |
| createdAt | datetime | YES | | NULL | |
| updatedAt | datetime | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
staff_permissions
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(20) | YES | UNI | NULL | |
| staffId | int(11) unsigned | NO | MUL | NULL | |
| permissionId | int(11) unsigned | NO | MUL | NULL | |
| createdAt | datetime | YES | | NULL | |
| updatedAt | datetime | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
permissions
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(20) | YES | UNI | NULL | |
| description | varchar(100) | NO | | NULL | |
| createdAt | datetime | YES | | NULL | |
| updatedAt | datetime | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
一个client_staff
有多个staff_permissions
,属于permission
.
使用框架,我想获取 staff_permissions
和 client_staff
,所以这就是我尝试在 ClientStaff
中配置 relations
的方式:
return [
'client' => [self::BELONGS_TO, User::class, 'clientId'],
'staffPermission' => [self::HAS_MANY, StaffPermission::class, 'id'],
]
在StaffPermission
中:
return [
'staff' => [self::BELONGS_TO, ClientStaff::class, 'staffId'],
'permissions' => [self::HAS_MANY, Permission::class, 'permissionId'],
]
在Permission
中:
return [
'staffPermissions' => [self::BELONGS_TO, StaffPermission::class, 'id'],
]
我们有一个 toAPICopy
方法将数据带到 JSON 中的客户端,在这个方法中,对于 类 BELONGS_TO
,我们利用每个类' toAPICopy
在返回的数组中。下面是 ClientStaff
public function toAPICopy()
{
return [
'id' => $this->id,
'client' => $this->client->toAPICopy(),
...,
'staffPermission' => $this->staffPermission->toAPICopy()
];
}
但没有任何反应,应用程序挂起。没有获取数据,也没有显示错误,api.log
文件中没有任何东西可以帮助我。我不知道我做错了什么。
我觉得你在建立关系方面遇到了一些问题。
这些是根据您的 table 结构的关系。
ClientStaff 应该是:
return [
'client' => [self::BELONGS_TO, User::class, 'clientId'],
'staffPermission' => [self::HAS_MANY, StaffPermission::class, 'staffId'],
]
在 StaffPermission 中应该是:
return [
'staff' => [self::BELONGS_TO, ClientStaff::class, 'staffId'],
'permissions' => [self::BELONGS_TO, Permission::class, 'permissionId'],
]
并且在 Permission 中应该是:
return [
'staffPermissions' => [self::BELONGS_TO, StaffPermission::class, 'permissionId'],
]
因此,HAS_MANY
将 return 一个对象数组,因此,您不能直接在其上调用方法。对我有用的解决方案是:
public function toAPICopy() {
foreach ($this->staffPermissions as $p) {
array_push($permissions, $p->toAPICopy();
}
return [ ... 'staffPermissions' => $permissions, ... ];
}