<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 關系型 Active Record # https://www.yiichina.com/doc/guide/1.1/database.arr BELONGS_TO(屬于): 如果表 A 和 B 之間的關系是一對多,則 表 B 屬于 表 A (例如 Post 屬于 User); HAS_MANY(有多個): 如果表 A 和 B 之間的關系是一對多,則 A 有多個 B (例如 User 有多個 Post); HAS_ONE(有一個): 這是 HAS_MANY 的一個特例,A 最多有一個 B (例如 User 最多有一個 Profile); MANY_MANY: 這個對應于數據庫中的 多對多 關系。 由于多數 DBMS 不直接支持 多對多 關系,因此需要有一個關聯表將 多對多 關系分割為 一對多 關系。 在我們的示例數據結構中,tbl_post_category 就是用于此目的的。在 AR 術語中,我們可以解釋 MANY_MANY 為 BELONGS_TO 和 HAS_MANY 的組合。 例如,Post 屬于多個(belongs to many) Category ,Category 有多個(has many) Post. ## 項目示例 D:\wamp\www\weike.test\weiketest\protected\models\weike\wkeTeachingV3.php ``` public function tableName() { return WK::DBweike.'.tbTeachingV3'; } public function relations() { return array( 'prepare' => array(self::BELONGS_TO, 'wkePrepare', 'fdPrepareID'), 'class'=>array(self::BELONGS_TO,'wkeClass','fdClassID') ,//by liupf 2016/7/9 'record'=>array(self::HAS_MANY,'wkeTeachingV3Student','fdTeachingV3ID'),//by liupf 2016/9/28 'content'=>array(self::BELONGS_TO,'Content','fdPrepareID'),//by liupf 2016/9/28 'user'=>array(self::BELONGS_TO,'User','fdTeacherID'),//by liupf 2016/10/19 'operations' => array(self::HAS_MANY,'wkeTeachingV3Operation','fdTeachingV3ID') , 'schoolMap'=>array(self::HAS_ONE,'SchoolMap','','on'=>'t.fdTeacherID = schoolMap.fdUserID'), ); } ``` D:\wamp\www\weike.test\weiketest\protected\models\weike\wkePrepare.php ``` public function tableName() { return WK::DBweike . '.tbPrepare'; } public function relations() { return array( 'user' => array(self::BELONGS_TO, 'User', 'fdTeacherID'), 'prepareItems' => array(self::HAS_MANY, 'wkePrepareItem', 'fdPrepareID'), 'itemCnts' => array(self::HAS_MANY, 'wkePrepareItemContent',array('id'=>'fdPrepareItemID'),'through'=>'prepareItems'), 'bookTag' => array(self::BELONGS_TO, 'Tag', 'fdTextbookID'), 'tag' => array(self::BELONGS_TO, 'Tag', 'fdTagID'), 'content' => array(self::BELONGS_TO, 'Content', 'fdContentID'), 'cntPreHw' => array(self::STAT, 'wkeAssignment', 'fdPrepareID', 'condition' => 't.fdType=' . WK::PREPARE_ITEM_TYPE_PREVIEW), 'cntAssHw' => array(self::STAT, 'wkeAssignment', 'fdPrepareID', 'condition' => 't.fdType=' . WK::PREPARE_ITEM_TYPE_ASSIGNMENT), 'cntTch' => array(self::STAT, 'wkeInClass', 'fdPrepareID'), 'groupMap' => array(self::HAS_MANY, 'wkePrepareGroupMap', 'fdPrepareID'), 'boards' => array(self::HAS_MANY, 'wkePrepareBoard', 'fdPrepareID'), 'ctnTagIndex' => array(self::HAS_MANY, 'wkeCtnTagIndex', '', 'on' => 't.fdContentID=ctnTagIndex.fdContentID'), ); } ``` ## 多對多關聯模型 **D:\wamp\www\xue.test\xuetang\protected\models\service\User.php** ``` public function relations(){ 'usercourse' => array(self::MANY_MANY, 'xtCourse', 'wkxuetang.tbCourseStudent(fdUserID, fdCourseID)'), } ``` **D:\wamp\www\xue.test\xuetang\protected\modules\courseCenter\controllers\StuCourseController.php** ``` /** * 獲取當前用戶的課程 */ public function actionGetMyCourse() { $userId = RequestUtils::getNormalRequest('userId'); //19 if (!$userId) { ResponseUtils::json(null, 1, 'fail'); } $criteria = new CDbCriteria(); $criteria->with = ['usercourse']; $criteria->addCondition("t.id=" . $userId); $list = User::model()->findAll($criteria); if ($list) { ResponseUtils::json($list); } ResponseUtils::json(null, 1, 'fail'); } ``` ### SQL ``` SELECT `t`.`id` AS `t0_c0`, `t`.`fdNumber` AS `t0_c1`, `t`.`fdLogin` AS `t0_c2`, `t`.`fdName` AS `t0_c3`, `t`.`fdRealName` AS `t0_c4`, `t`.`fdPassword` AS `t0_c5`, `t`.`fdActive` AS `t0_c6`, `t`.`fdQuestion` AS `t0_c7`, `t`.`fdAnswer` AS `t0_c8`, `t`.`fdPortrait` AS `t0_c9`, `t`.`fdGender` AS `t0_c10`, `t`.`fdApprove` AS `t0_c11`, `t`.`fdApproverID` AS `t0_c12`, `t`.`fdCreate` AS `t0_c13`, `t`.`fdStatus` AS `t0_c14`, `t`.`fdTypeID` AS `t0_c15`, `t`.`fdCardID` AS `t0_c16`, `t`.`fdPoints` AS `t0_c17`, `t`.`fdMoney` AS `t0_c18`, `t`.`fdCash` AS `t0_c19`, `t`.`fdDisabled` AS `t0_c20`, `t`.`fdShutup` AS `t0_c21`, `t`.`fdExpired` AS `t0_c22`, `t`.`fdRgType` AS `t0_c23`, `t`.`fdVipType` AS `t0_c24`, `t`.`fdOutsideUID` AS `t0_c25`, `t`.`fdOutsideHost` AS `t0_c26`, `t`.`fdIsXuetang` AS `t0_c27`, `t`.`fdTeacherID` AS `t0_c28`, `t`.`fdParentID` AS `t0_c29`, `t`.`fdTeamType` AS `t0_c30`, `t`.`fdOutsideUUID` AS `t0_c31`, `usercourse`.`id` AS `t1_c0`, `usercourse`.`fdUserID` AS `t1_c1`, `usercourse`.`fdName` AS `t1_c2`, `usercourse`.`fdCourseType` AS `t1_c3`, `usercourse`.`fdSubjectID` AS `t1_c4`, `usercourse`.`fdSchoolTypeID` AS `t1_c5`, `usercourse`.`fdViewType` AS `t1_c6`, `usercourse`.`fdDiff` AS `t1_c7`, `usercourse`.`fdFee` AS `t1_c8`, `usercourse`.`fdThumb` AS `t1_c9`, `usercourse`.`fdVideo` AS `t1_c10`, `usercourse`.`fdVideoThumb` AS `t1_c11`, `usercourse`.`fdInfo` AS `t1_c12`, `usercourse`.`fdStatus` AS `t1_c13`, `usercourse`.`fdCreate` AS `t1_c14`, `usercourse`.`fdUpdate` AS `t1_c15` FROM `wksvc`.`tbUser` `t` LEFT OUTER JOIN `wkxuetang`.`tbCourseStudent` `usercourse_usercourse` ON ( `t`.`id` = `usercourse_usercourse`.`fdUserID` ) LEFT OUTER JOIN `wkxuetang`.`tbCourse` `usercourse` ON ( `usercourse`.`id` = `usercourse_usercourse`.`fdCourseID` ) WHERE (t.id = 19) ``` ### 圖示 ![](./img/2018-11-21_180305.png) ![](./img/2018-11-21_180346.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看