【问题标题】:Phalcon - Implement One-To-Many self-referencing relationship in modelPhalcon - 在模型中实现一对多自引用关系
【发布时间】:2018-01-14 01:10:40
【问题描述】:

如何在 Phalcon 中实现这个功能?教义有this。我想要类似的东西。我在数据库中的office表:

Id (PK) | ParentId | Name

我想要一个类似的函数:

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

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

【问题讨论】:

    标签: php doctrine-orm orm model phalcon


    【解决方案1】:

    在您的模型中:

    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

    【讨论】:

    • 能否请您插入ProductCategories模型的完整代码?
    • Note the full namespace.,尝试使用ProductCategories::class,这比将命名空间写成字符串要好。如果你将来要重构,你不必担心命名空间引用!
    猜你喜欢
    • 2015-03-03
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    相关资源
    最近更新 更多