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

                ## 模型初始化 initialize( ) #### 1. 作用:創建模型對象之前做的預處理工作。 >[info] 在ThinkPHP5中,創建數據模型是一個非常容易的過程,整個過程是透明的。 #### 2. 實例演示:創建模型對象前,初始化部分模型類屬性 >[info] 模型對象創建時,除了模型名$name和所繼承的類$class(命名空間表示)之外,其它所有的模型屬性都是空的或只有默認值。隨著以后的操作,這些屬性會根據操作要求自動完成初始化。 * 第一步:在上節課創建的模型類Staff.php里,添加初始化方法 >[info] 本教程如無特別說明:源碼均指:Model.php 類文件中的內容。 文件位置:/thinkphp/library/think/Model.php ~~~ <?php namespace app\index\model; //導入模型類 use think\model; class Staff extends model { //在子類重寫父類的初始化方法initialize() protected function initialize(){ //繼承父類中的initialize() parent::initialize(); //初始化數據表名稱,通常自動獲取不需設置 $this->table = 'tp5_staff'; //初始化數據表字段信息 $this->field = $this->db()->getTableInfo('', 'fields'); //初始化數據表字段類型 $this->type = $this->db()->getTableInfo('', 'type'); //初始化數據表主鍵 $this->pk = $this->db()->getTableInfo('', 'pk'); } } ~~~ >[info] 在自定義的模型初始化方法中,我們對模型中的相關屬性進行初始化:$field,$pk,$table,$type(屬性含義上節課已詳細說明) * 第二步:控制器Index.php中進行調用: ~~~ <?php namespace app\index\controller; //導入模型類 use app\index\model\Staff; class Index { public function index(){ //創建模型類Staff $model = new Staff(); //查看Staff類實例$model dump($model); } } ~~~ * 第三步:瀏覽器地址欄輸入:tp5.com/index.php/index/index/ 查看這個自定義模型對象 ~~~ object(app\index\model\Staff)#5 (28) { ["connection":protected] => array(0) { } ["query":protected] => NULL ["name":protected] => string(5) "Staff" //這是我們初始化獲取的數據表名稱 ["table":protected] => string(9) "tp5_staff" ["class":protected] => string(21) "app\index\model\Staff" ["error":protected] => NULL ["validate":protected] => NULL //這是我們初始化獲取的數據表主鍵信息 ["pk":protected] => string(2) "id" //這是我們初始化獲取的數據表字段名信息 ["field":protected] => array(7) { [0] => string(2) "id" [1] => string(4) "name" [2] => string(3) "sex" [3] => string(3) "age" [4] => string(6) "salary" [5] => string(4) "dept" [6] => string(8) "hiredate" } ["readonly":protected] => array(0) { } ["visible":protected] => array(0) { } ["hidden":protected] => array(0) { } ["append":protected] => array(0) { } ["data":protected] => array(0) { } ["change":protected] => array(0) { } ["auto":protected] => array(0) { } ["insert":protected] => array(0) { } ["update":protected] => array(0) { } ["autoWriteTimestamp":protected] => bool(false) ["createTime":protected] => string(11) "create_time" ["updateTime":protected] => string(11) "update_time" ["dateFormat":protected] => string(11) "Y-m-d H:i:s" //這是我們初始化獲取的數據表字段類型信息 ["type":protected] => array(7) { ["id"] => string(15) "int(4) unsigned" ["name"] => string(11) "varchar(30)" ["sex"] => string(19) "tinyint(2) unsigned" ["age"] => string(19) "tinyint(3) unsigned" ["salary"] => string(19) "float(8,2) unsigned" ["dept"] => string(19) "tinyint(2) unsigned" ["hiredate"] => string(4) "date" } ["isUpdate":protected] => bool(false) ["updateWhere":protected] => NULL ["relation":protected] => NULL ["failException":protected] => bool(false) ["useGlobalScope":protected] => bool(true) } ~~~ * * * * * #### 3、總結: >[success] 模型初始化,絕大多數情況下會自動完成,并不需要我們去做。
                  <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>

                              哎呀哎呀视频在线观看