<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ### 定義一個基礎模型 * 模型文件放在模塊下的`model`目錄內(事實上,可以放在任何位置),并且文件名以首字母大寫的駝峰命名+`Model`。 ~~~php <?php namespace module\index\model; class PeopleModel { public $head; public $hand; public $foot; public function buildPeople() { return $this->head . $this->hand . $this->foot; } } ~~~ 如上,我定義了一個最簡單的模型`People`。 ### 定義一個數據庫模型 當我的一個模型和數據庫關聯,我定義的模型就可以繼承`lying\db\ActiveRecord`: ~~~php <?php namespace module\index\model; use lying\db\ActiveRecord; class UserModel extends ActiveRecord { } ~~~ 如上,我定義了一個模型,模型默認關聯`\Lying::$maker->db`(參見[db組件](db.md))中的表`user`。 如果你的模型為`UserLogModel`,那么你默認對應的表為`user_log`。 再或者,想自定義關聯的數據庫和關聯的表,你可以如下定義: ~~~php <?php namespace module\index\model; use lying\db\ActiveRecord; class UserModel extends ActiveRecord { /** * 設置數據庫連接 * @return \lying\db\Connection */ public static function db() { return \Lying::$maker->db2; } /** * 設置模型對應的表名 * @return string */ public static function table() { return 'myuser'; } } ~~~ #### 數據庫模型初始化 你可以在模型實例化的時候執行一些操作,因為數據庫模型繼承服務類基類,所以不能定義構造函數,但是你可以用重寫`init()`方法來實現初始化操作: ~~~php <?php namespace module\index\model; use lying\db\ActiveRecord; class UserModel extends ActiveRecord { public function init() { //對象實例化的時候進行你的一些操作 } } ~~~ ### 數據庫模型操作 數據庫的一條記錄對應一個數據庫模型實例,數據庫的字段對應模型實例的屬性。 #### 查詢 ~~~php $user = UserModel::find()->where(['id'=>1])->one(); $user = UserModel::find()->where(['<', 'id', '10'])->all(); ~~~ 如上,`UserModel::find()`返回一個`lying\db\ActiveQuery`實例,操作方法和`lying\db\Query`一致(參見[查詢生成器](query.md)),但是默認已經調用`from()`方法,設置了查詢的表為當前模型對應的表,所以不用再次調用`from`。`one()`方法返回一個`UserModel`實例,如果查詢的數據庫記錄存在的話。`all()`方法返回所有滿足查詢條件的`UserModel`數組。 ~~~php $user = UserModel::findOne(['id'=>1]); $user = UseModelr::findAll(['<', 'id', 10]); ~~~ 如上,`findOne()`查詢滿足條件的一個`UserModel`實例,參數為數組查詢條件。`findAll()`的參數可以省略,或者為數組的查詢條件,并且返回所有滿足查詢條件的`User`實例數組。 > 通過以上查詢出來的`UserModel`實例為舊記錄,如果`$user = new UserModel();`,則`$user`為新記錄。 #### 更新記錄 如果你查詢出了一條記錄,此時這條記錄為舊記錄,那么你可以直接對其進行更改,然后調用`save()`方法進行更新記錄: ~~~php $user = UserModel::findOne(['id'=>1]); $user->name = 'lying'; $user->sex = 1; $user->save(); //或者 UserModel::updateAll(['id'=>1], ['sex'=>'0']); ~~~ 也可以用`load`函數來裝載數據: ~~~php $user = UserModel::findOne(['id'=>1]); $user->load(['name'=>'lying']); $user->save(); ~~~ #### 插入記錄 你想要在`UseModelr`表插入一條記錄: ~~~php $user = new UserModel(); $user->name = 'lying'; $user->money = 100; $user->save(); ~~~ 也可以用`load`函數來裝載數據: ~~~php $user = new UserModel(); $user->load(['name'=>'lying']); $user->save(); ~~~ > `save()`方法能判斷你的當前對象是否為新記錄,如果為新記錄就執行插入操作,舊記錄執行更新操作。需要注意的是,舊記錄查詢結果必須包含主鍵,否則更新失敗,save()方法返回0。 #### 刪除記錄 如果你的記錄為舊記錄,那么你可以這樣刪除這條記錄: ~~~php $user = UserModel::findOne(1); $user->delete(); //你也可以這樣: UserModel::deleteAll(['id'=>1]); ~~~ #### 其他操作 判斷模型是否存在某個字段: ~~~php $user = new UserModel(); $user->load(['name'=>'lying', 'sex'=>null]); $user->exists('name'); //true $user->exists('sex'); //true $user->exists('addr'); //false ~~~ ### 數據庫模型返回值 > 一般執行出錯都會拋出異常,執行失敗返回false,成功返回更改的行數。具體請看代碼的注釋。
                  <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>

                              哎呀哎呀视频在线观看