Yii2中多表关联查询hasOne hasMany的方法

前端之家收集整理的这篇文章主要介绍了Yii2中多表关联查询hasOne hasMany的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

表positionContent

Box-sizing: border-Box; border-top: rgb(238,238,238) 1px solid; border-right: rgb(238,238) 1px solid; width: 946px; white-space: normal; word-spacing: 0px; border-collapse: collapse; border-bottom: rgb(238,238) 1px solid; text-transform: none; color: rgb(0,0); text-align: left; font: 14px/26px 'microsoft yahei'; border-spacing: 0px; border-left: rgb(238,238) 1px solid; widows: 1; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px"> Box-sizing: border-Box"> Box-sizing: border-Box"> Box-sizing: border-Box"> Box-sizing: border-Box">
Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; vertical-align: top; border-bottom: rgb(238,238) 1px solid; padding-bottom: 8px; padding-top: 8px; padding-left: 8px; border-left: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">id Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">position_id Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">content_title Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">content_id Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">is_recommend Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">list_sort Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">update_time Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">create_time
Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">10Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">14Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">大成成长Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">160910Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">1Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">2017-02-09 11:51:56Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">2017-02-09 11:51:56Box-sizing: border-Box"> Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">11Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">15Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">创新成长Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">2017-02-09 11:52:08Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">2017-02-09 11:52:08

position表

Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">name
Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">title Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">type Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">num Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">remark Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">列表推荐一Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">五星推荐Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">3Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">2017-02-09 00:00:00Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">2017-02-09 00:00:00Box-sizing: border-Box"> Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">列表推荐二Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">热销基金Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">4Box-sizing: border-Box; border-top: rgb(238,238) 1px solid; line-height: 20px; padding-right: 8px">2017-02-09 00:00:00

positionContent表的position_id对就position表的id。positionContent跟position是一对一的关系,反过来则是多对一。

model

hasOne(Position::className(),['id'=>'position_id']); } ...

然后在Controller调用,这样就能拿到position表中的数据了。

position; } }

其实hasOne是执行了两次查询,并不是真正的连表查询

PHP;"> SELECT * FROM `position_content` WHERE `id`=10 SELECT * FROM `position` WHERE `id`='14'

以上所述是小编给大家介绍的Yii2中多表关联查询hasOne hasMany,希望对大家有所帮助。程序员遇到问题都会上(编程之家jb51.cc)查找问题解答方法!如果觉得站点还不错,随手转发给程序员朋友一下!

原文链接:https://www.f2er.com/php/17959.html

猜你在找的PHP相关文章