phalcon 模型关系的处理 一对一,一对多,多对多

xiaoxiao2021-02-28  41

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

转载请注明原文地址: https://www.6miu.com/read-77788.html

最新回复(0)