附上我对官方内容的理解
官方链接地址:https://docs.phalconphp.com/zh/latest/db-models-relationships#aliases
我这里先简单说一下 有哪些关联和含义
方法 | 含义 | 参数 |
---|---|---|
hasMany | 一对多 | $fields, $referenceModel, $referencedFields, $options |
hasOne | 一对一 | $fields, $referenceModel, $referencedFields, $options |
belongsTo | 多对一 | $fields, $referenceModel, $referencedFields, $options |
hasManyToMany | 多对多 | $fields, $intermediateModel, $intermediateFields, $intermediateReferencedFields, $referenceModel, $referencedFields, $options |
上面表中 参数含义
$fields
本模型 关联的字段
$referenceModel
除了多对多中是 远端 表模型 其他是 对端表的模型
$referencedFields
除了多对多中是 远端表的关联字段(远端表中字段) 其他是 对端表关联字段
$intermediateModel
多对多中的中间表模型
$intermediateFields
多对多中 中间表与本模型对应表的关联字段(中间表字段)
$intermediateReferencedFields
多对多中 中间表与远端模型对应表的 关联字段(中间表的字段)
$options
设定选项功能 比如alias
只获取指定字段就用到了 $options
我看官方文档时没有看到多对多的条件设定,就按照官方文档提供的代码 直接把表的字段写在 $options 上了,
$this -> hasManyToMany(
'id',
'\App\Model\PostTag',
'tag_id',
'post_id',
'\App\Model\Posts',
'id',
[
'alias' => 'getPosts',
'params' => [
'columns' => 'id,slug,title,summary,comment_count,view_count,favorite_count,created_at',
'conditions' => "published = :status:",
'bind' => ['status' => '1'],
]
]
);
结果 给了这个错误:
Can't obtain model's source from models list: 'App\Model\Posts', when preparing: SELECT id,slug,title,summary,comment_count,view_count,favorite_count,created_at FROM [\App\Model\Posts] INNER JOIN [\App\Model\PostTag] ON [\App\Model\PostTag].[post_id] = [\App\Model\Posts].[id] WHERE (published = :status:) AND ([\App\Model\PostTag].[tag_id] = :APR0:)
#0 [internal function]: Phalcon\Mvc\Model\Query->_getQualified(Array)
#1 [internal function]: Phalcon\Mvc\Model\Query->_getExpression(Array)
#2 [internal function]: Phalcon\Mvc\Model\Query->_getSelectColumn(Array)
#3 [internal function]: Phalcon\Mvc\Model\Query->_prepareSelect()
#4 [internal function]: Phalcon\Mvc\Model\Query->parse()
#5 [internal function]: Phalcon\Mvc\Model\Query->execute()
#6 [internal function]: Phalcon\Mvc\Model\Manager->getRelationRecords(Object(Phalcon\Mvc\Model\Relation), NULL, Object(App\Model\Tags), NULL)
#7 F:\www\phalcon\app\controllers\Api\CategoryController.php(22): Phalcon\Mvc\Model->__get('getPosts')
#8 [internal function]: App\Controller\Api\CategoryController->TagAction('2')
#9 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(App\Controller\Api\CategoryController), 'tagAction', Array)
#10 [internal function]: Phalcon\Dispatcher->dispatch()
#11 F:\www\phalcon\public\index.php(43): Phalcon\Mvc\Application->handle()
#12 {main}
我一看,前面自己指定的字段怎么和框架的格式化时候的显示的字段不一致呢。
后来我就把指定字段按照框架格式化的样子改了下: 这里差点忘了 条件中还有一个字段
$this -> hasManyToMany(
'id',
'\App\Model\PostTag',
'tag_id',
'post_id',
'\App\Model\Posts',
'id',
[
'alias' => 'getPosts',
'params' => [
'columns' => '[\App\Model\Posts].id,[\App\Model\Posts].slug,[\App\Model\Posts].title,[\App\Model\Posts].summary,[\App\Model\Posts].comment_count,[\App\Model\Posts].view_count,[\App\Model\Posts].favorite_count,[\App\Model\Posts].created_at',
'conditions' => "[\App\Model\Posts].published = :status:",
'bind' => ['status' => '1'],
]
]
);
这样就可以得到想要的结果了。
框架自动把 [\App\Model\Posts]
替换成这个模型对应的表名,毕竟多对多有一个中间表。
PS
- 首先自己的耐心需要训练,不要看一点文档就自己开搞了,这样自己需要买单的。
- 解决问题的时候看了下 cphalcon 的源码 发现
cphalcon\phalcon\
下的zep
格式的文件和 PHP 非常像。可能用phalcon-devtools
的ide/gen-stubs.php
脚本生成 可供提示函数用的吧 - 看文档细心细心细心……。
评论已关闭