代码之家  ›  专栏  ›  技术社区  ›  soroush gholamzadeh

Phalcon-在模型中实现一对多自参考关系

  •  0
  • soroush gholamzadeh  · 技术社区  · 7 年前

    如何在Phalcon中实现此功能?条令 this 办公室 数据库中的表:

    Id (PK) | ParentId | Name
    

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

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

    1 回复  |  直到 7 年前
        1
  •  2
  •   Nikolay Mihaylov Raghu    7 年前

    在您的模型中:

    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