【问题标题】:Octobercms child relationship of relationshipsOctobercms 子关系的关系
【发布时间】:2018-06-07 03:38:31
【问题描述】:

我正在使用 octobercms 插件,其中包含以下模型:

  • 房屋
  • 支付
  • 所有者

在房屋模型中,我有以下关系:

public $hasManyThrough = [
      'owner' => [
        'Author\Plugin\Models\Owner',
        'table' => 'author_plugin_houses_owners',
        'key' => 'houses_id',
        'otherKey' => 'owners_id'
      ]
    ];

这个关系有一个中间表'author_plugin_houses_owners':

+------------+-------------+--------+
| hoses_id   |  owners_id  | active |
+------------+-------------+--------+

在支付模型中,我有以下关系:

public $belongsTo = [
        'houses' => ['Author\Plugin\Models\Houses']
];

这个模型对应的表格是这样的:

+----+----------+---------+-------+------+
| id | hoses_id | amounth | payed | debt |
+----+----------+---------+-------+------+

在表中我有一列“houses_id”,问题是,我如何访问“Houses”模型中声明的“所有者”关系,因为“Pays”模型?我需要访问它,因为我需要在插件后端的列表视图中打印“owner_name”。

所有者表:

+----+------------+--------+
| id | owner_name | active |
+----+------------+--------+

非常感谢!

【问题讨论】:

    标签: php laravel octobercms


    【解决方案1】:

    我假设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]

    所以这将返回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
    

    确保将路径替换为您自己的插件作者姓名和路径。

    现在在部分内部,你可以编写你的实际逻辑 [这必须在 PHP 中而不是在树枝中]

    <?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 ?>
    

    如果只有一个所有者,它将显示 Hardik 一个名称,只有当有多个所有者时,它才会显示 Hardik, Noe 逗号分隔值。

    如有任何疑问,请发表评论。

    【讨论】:

    • 还有一件事,如果我想将此列转换为可搜索的列,它可以工作还是我需要执行其他功能?谢谢。
    • 不,我认为它不会直接起作用,您可能需要为此扩展列表查询,如果我有更多时间,我会发布如何做到这一点。但这我们必须手动完成,因为它的深层关系领域:)
    猜你喜欢
    • 2019-08-16
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 2020-01-15
    • 2016-10-11
    • 1970-01-01
    • 2019-12-05
    • 2020-02-20
    相关资源
    最近更新 更多