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

妖精的绣舞 提交于 2019-12-12 05:42:00

问题


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.


回答1:


In your model:

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', 
            ]
        ]);
    }
 }

Note the full namespace.

Usage:

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

More info: https://docs.phalconphp.com/en/3.1/db-models-relationships



来源:https://stackoverflow.com/questions/45534869/phalcon-implement-one-to-many-self-referencing-relationship-in-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!