Phalcon - 在模型中实现一对多自引用关系

Phalcon - Implement One-To-Many self-referencing relationship in model

如何在Phalcon中实现这个功能?主义有this。我想要类似的东西。我的 办公室 table 在数据库中:

Id (PK) | ParentId | Name

我想要这样的函数:

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

我试图在我的模型中定义多对一关系,但它总是 returns 一个空数组。

在您的模型中:

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