2017-08-06 130 views

回答

2

在你的模型:

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

+0

請你插入'ProductCategories'模型​​的完整代碼? –

+0

這就是你所需要的,無論如何都要更新代碼。 –

+1

'請注意full namespace.',而不是使用'ProductCategories :: class',它比在字符串中寫入命名空間要好。如果將來要重構,則不必擔心命名空間引用! – Timothy

相關問題