phalcon 模型關係的處理 一對一,一對多,多對多
阿新 • • 發佈:2019-01-02
Model:
表與表之間的關係:hasOne 一對一( $fields, $referenceModel, $referencedFields : 當前表中的欄位, 對應關係模型, 對應關係模型中表的字欄位 )
hasMany 一對多 ( $fields, $referenceModel, $referencedFields : 當前表中的欄位, 對應關係模型, 對應關係模型中表的字欄位 )
hasManyToMany 多對多
belongsTo 多對一( 屬於 ) ( $fields, $referenceModel, $referencedFields : 當前表中的欄位, 對應關係模型, 對應關係模型中表的字欄位 )
如專案中存在名稱空間 則 要在對應的關係中新增alias引數 array( 'alias' => 'namespace' )
例:多對一
$this->belongsTo('company_id', 'App\Models\ErpCompanys', 'id', ['alias' => 'ErpCompanys']);
取得關聯表中name資訊的phalcon程式碼:
public function userCarAction(){ if($this->request->isPost()){ $this->view->disable(); $info = ErpCompanyCar::findFirst(); $name = $value->ErpCompanys->name; } }
官方文件地址:https://docs.phalconphp.com/zh/latest/reference/model-relationships.html#relationships-between-models