Phalcon-在模型中实现一对多自引用关系 [英] Phalcon - Implement One-To-Many self-referencing relationship in model

查看:128
本文介绍了Phalcon-在模型中实现一对多自引用关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Phalcon中实现此功能?教义具有.我想要类似的东西.我在数据库中的办公室表:

How to implement this feature in the Phalcon? Doctrine has this. I want something similar like that. My office table in the database:

Id (PK) | ParentId | Name

我想要一个像这样的功能

I want a function like:

Office::findFirst()->children();

我试图在模型中定义多对一关系,但是它总是返回一个空数组.

I've tried to define a Many-to-One relationship in my model but it always returns an empty array.

推荐答案

在您的模型中:

namespace Models;
class ProductCategories extends BaseModel
    public function initialize()
    {
        $this->hasMany('id', 'Models\ProductCategories', 'parent_id', [
            'alias' => 'children',
            'params' => [
                'order' => 'position ASC',
                'conditions' => 'active = 1', 
            ]
        ]);
    }
 }

注意完整的名称空间.

用法:

$parent = \Models\ProductCategories::findFirst();
print_r($parent->children->toArray());

更多信息: https://docs.phalconphp.com/en/3.1/db-models-relationships

这篇关于Phalcon-在模型中实现一对多自引用关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆