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

                ## 創建數據對象之:setAttr方法 #### 1. 作用:如果數據對象中已存在指定數據,則更新;如果不存在,則創建。 >[info] setAttr()方法也叫修改器 #### 2. setAttr()源碼: >[info] 本教程如無特別說明:源碼均指:Model.php 類文件中的內容。 文件位置:/thinkphp/library/think/Model.php ~~~ /** * 修改器 設置數據對象值 * @access public * @param string $name 屬性名 * @param mixed $value 屬性值 * @param array $data 數據 * @return $this */ public function setAttr($name, $value, $data = []) { if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) { // 自動寫入的時間戳字段 $value = $this->autoWriteTimestamp($name); } else { // 檢測修改器 $method = 'set' . Loader::parseName($name, 1) . 'Attr'; if (method_exists($this, $method)) { $value = $this->$method($value, array_merge($data, $this->data)); } elseif (isset($this->type[$name])) { // 類型轉換 $value = $this->writeTransform($value, $this->type[$name]); } } // 標記字段更改 if (!isset($this->data[$name]) || ($this->data[$name] != $value && !in_array($name, $this->change))) { $this->change[] = $name; } // 設置數據對象屬性 $this->data[$name] = $value; return $this; } ~~~ * * * * * #### 3. 參數與返回值 * 參數: | 序號 | 參數 | 說明 | | --- | --- | --- | | 1 | $name/ 字符串 | 數據對象$data[ ]的數組元素的鍵名 | | 2 | $value / 復合類型 | 數據對象$data[ ]的數組元素的值 | | 3 | $data=[ ] / 數組 | 數據對象$data,通常不需要傳入,保持默認即可 | * 返回值:數據對象 * * * * * #### 4. 實例演示 1. 任務:給模型對象逐個賦值,創建數據對象 >[info] Staff模型類創建與前面一樣的,目前我們使用一個空的模型類就可以完成測試 * 自定義模型類: Staff.php ~~~ <?php namespace app\index\model; //導入模型類 use think\model; class Staff extends model { //自定義模型類代碼 } ~~~ * 控制器Index.php: ~~~ <?php namespace app\index\controller; //導入模型類 use app\index\model\Staff; class Index { public function index(){ //1.創建模型對象$model $model = new Staff(); //2.setAttr()方法操作數據對象 //如果該屬性不存在,則創建該屬性 //如果該屬性已存在,則更新該屬性 $model -> setAttr('id',10); $model -> setAttr('name','Jack'); $model -> setAttr('age',20); //3.查看該數據對象 dump($model); } } ~~~ * 瀏覽器查看結果: tp5.com/index.php/index/index > tp5.com 是我們之前配置好的一個本地域名 ~~~ object(app\index\model\Staff)#5 (28) { //這里略去與本例無關的屬性 ······ ["data":protected] => array(3) { ["id"] => int(10) ["name"] => string(4) "Jack" ["age"] => int(20) } //這里略去與本例無關的屬性 ······ } ~~~ #### 5. 總結: >[success] setAttr( )方法,實際開發中并不常用,但我們在Model類源碼中,卻隨處可見他的身影。該方法不僅可以創建數據對象,還可以更新數據對象。但是該方法只能一個個逐個處理數據,多個數據需要同時處理,就必須多次調用才可以實現。
                  <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>

                              哎呀哎呀视频在线观看