问题
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