Octobercms 子关系之关系
Octobercms child relationship of relationships
我在 octobercms 插件中工作,其中有以下模型:
- 房子
- 支付
- 所有者
在房屋模型中,我有以下关系:
public $hasManyThrough = [
'owner' => [
'Author\Plugin\Models\Owner',
'table' => 'author_plugin_houses_owners',
'key' => 'houses_id',
'otherKey' => 'owners_id'
]
];
这个关系有一个中间table 'author_plugin_houses_owners':
+------------+-------------+--------+
| hoses_id | owners_id | active |
+------------+-------------+--------+
在付费模型中,我有以下关系:
public $belongsTo = [
'houses' => ['Author\Plugin\Models\Houses']
];
这个模型对应一个table是这样的:
+----+----------+---------+-------+------+
| id | hoses_id | amounth | payed | debt |
+----+----------+---------+-------+------+
在 table 中,我有一列 "houses_id",问题是,我如何访问 "Houses" 模型中声明的 "owner" 关系,因为 "Pays" 模型?我需要访问它,因为我需要在插件后端的列表视图中打印 "owner_name"。
所有者table:
+----+------------+--------+
| id | owner_name | active |
+----+------------+--------+
非常感谢!
我假设Pay
只属于一所房子。
public $belongsTo = [
'house' => ['Author\Plugin\Models\Houses']
// ^ you can use house instead houses for relation name
];
现在我们可以通过关系链访问所有者
$payModel->house->owner
因为you made relation hasMany
一个房子可以有多个owners
[hasManyThrough]
所以这将 return 你的列表 owners
。
因为在list view
你需要使用它,你可以定义partial
类型的列
columns:
id:
label: id
type: number
searchable: true
sortable: true
.... other fields ...
owner_name:
label: Owner
type: partial
path: $/hardiksatasiya/demotest/models/sort/_ownername_column.htm
Make sure to replace the path with your own plugin author name and path.
现在在 partial 里面,你可以写你的实际逻辑 [这必须在 PHP 而不是在 twig ]
<?php
// this is hasmany relation so we expect multiple owners
$owners = $record->house->owner;
$ownerArr = [];
foreach($owners as $ownr) {
$ownerArr[] = $ownr->owner_name;
}
$output = implode($ownerArr, ', ');
?>
<?php echo $output ?>
If there is only one owner it will show Hardik
one name only if there are multiple owners it will show Hardik, Noe
comma separated values.
如有疑问请评论。
我在 octobercms 插件中工作,其中有以下模型:
- 房子
- 支付
- 所有者
在房屋模型中,我有以下关系:
public $hasManyThrough = [
'owner' => [
'Author\Plugin\Models\Owner',
'table' => 'author_plugin_houses_owners',
'key' => 'houses_id',
'otherKey' => 'owners_id'
]
];
这个关系有一个中间table 'author_plugin_houses_owners':
+------------+-------------+--------+
| hoses_id | owners_id | active |
+------------+-------------+--------+
在付费模型中,我有以下关系:
public $belongsTo = [
'houses' => ['Author\Plugin\Models\Houses']
];
这个模型对应一个table是这样的:
+----+----------+---------+-------+------+
| id | hoses_id | amounth | payed | debt |
+----+----------+---------+-------+------+
在 table 中,我有一列 "houses_id",问题是,我如何访问 "Houses" 模型中声明的 "owner" 关系,因为 "Pays" 模型?我需要访问它,因为我需要在插件后端的列表视图中打印 "owner_name"。
所有者table:
+----+------------+--------+
| id | owner_name | active |
+----+------------+--------+
非常感谢!
我假设Pay
只属于一所房子。
public $belongsTo = [
'house' => ['Author\Plugin\Models\Houses']
// ^ you can use house instead houses for relation name
];
现在我们可以通过关系链访问所有者
$payModel->house->owner
因为you made relation hasMany
一个房子可以有多个owners
[hasManyThrough]
所以这将 return 你的列表 owners
。
因为在list view
你需要使用它,你可以定义partial
类型的列
columns:
id:
label: id
type: number
searchable: true
sortable: true
.... other fields ...
owner_name:
label: Owner
type: partial
path: $/hardiksatasiya/demotest/models/sort/_ownername_column.htm
Make sure to replace the path with your own plugin author name and path.
现在在 partial 里面,你可以写你的实际逻辑 [这必须在 PHP 而不是在 twig ]
<?php
// this is hasmany relation so we expect multiple owners
$owners = $record->house->owner;
$ownerArr = [];
foreach($owners as $ownr) {
$ownerArr[] = $ownr->owner_name;
}
$output = implode($ownerArr, ', ');
?>
<?php echo $output ?>
If there is only one owner it will show
Hardik
one name only if there are multiple owners it will showHardik, Noe
comma separated values.
如有疑问请评论。