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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 微博模型 和之前創建 User 模型的步驟一致,我們需要 1. 創建數據表 Posts 2. 建立微博模型 PostModel ## 創建數據表 打開命令行,鍵入命令并打開 `php think migrate:create Posts` `database\migrations\20190728094237_posts.php` ~~~php <?php use think\migration\Migrator; class Posts extends Migrator { /** * Migrate Up. */ public function up() { // create the table $table = $this->table('posts'); $table->addColumn('content', 'text') ->addColumn('user_id', 'integer') ->addTimestamps('created_at', 'updated_at') ->create(); } /** * Migrate Down. */ public function down() { $this->dropTable('posts'); } } ~~~ 運行命令 `php think migrate:run` 其中,`user_id` 是為了和 `User` 表中的 `id` 字段關聯,當我們創建關系模型時,只用一個 `id` 即可讀取該用戶發送過的所有推文。 ## 創建微博模型 和之前一致,只用在控制臺中鍵入命令 `php think make:model User/Post` 創建完成后并打開文件 `application\user\model\Post.php` ~~~php <?php namespace app\User\model; use think\Model; class Post extends Model { // 定義數據表名 protected $table = 'posts'; // 定義時間戳字段名 protected $createTime = 'created_at'; protected $updateTime = 'updated_at'; } ~~~ 這樣,基本的模型我們已經定義。 ## 模型關聯 在用戶推文獲取的過程中,我們一個用戶對應多條推文,如果不通過模型進行關聯,在查詢時則需要使用用戶的 `id` 查詢兩遍模型,如 ~~~php User::get($user_id) Post::get($user_id) ~~~ 這樣的工作非常繁瑣并且多次讀庫增加數據庫壓力,所以我們需要對模型進行關聯定義。 1. 一個用戶有多條推文 2. 一條推文只有一個用戶 定義正向關聯 `application\user\model\User.php` ~~~php <?php ... class User extends Model { // 正向關聯 Post 模型 public function posts() { return $this->hasMany('Post', 'user_id'); } ... ~~~ 定義反向關聯 `application\user\model\Post.php` ~~~php <?php ... class Post extends Model { 反向關聯 User 模型 public function user() { return $this->belongsTo('User'); } ... ~~~ 非常簡單的,兩個關聯操作已經定義,下一節將展示出關聯數據。
                  <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>

                              哎呀哎呀视频在线观看