国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

Eloquent 寫入數(shù)據(jù)結(jié)果不符合預(yù)期

bear

問(wèn)題描述

使用 Eloquant 定義了一個(gè) Model,根據(jù)文檔和測(cè)試寫入一行記錄。數(shù)據(jù)行插入但是不符合預(yù)期。

這個(gè)是我的 Model 文件

<?php

namespace app\model;

use support\Model;

class Staff extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'staff';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 郵箱
     * @var string $email
     */
    private string $email = '';

    /**
     * 密碼
     * @var string $password
     */
    private string $password = '';

    /**
     * 姓名
     * @var string $name
     */
    private string $name = '';

    /**
     * 頭像
     * @var string $avatar
     */
    private string $avatar = '';

    /**
     * 職位
     * @var string $position
     */
    private string $position = '';

    /**
     * @return string
     */
    public function getEmail(): string
    {
        return $this->email;
    }

    /**
     * @param string $email
     * @return Staff
     */
    public function setEmail(string $email): Staff
    {
        $this->email = $email;
        return $this;
    }

    /**
     * @return string
     */
    public function getPassword(): string
    {
        return $this->password;
    }

    /**
     * @param string $password
     * @return Staff
     */
    public function setPassword(string $password): Staff
    {
        $this->password = $password;
        return $this;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     * @return Staff
     */
    public function setName(string $name): Staff
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getAvatar(): string
    {
        return $this->avatar;
    }

    /**
     * @param string $avatar
     * @return Staff
     */
    public function setAvatar(string $avatar): Staff
    {
        $this->avatar = $avatar;
        return $this;
    }

    /**
     * @return string
     */
    public function getPosition(): string
    {
        return $this->position;
    }

    /**
     * @param string $position
     * @return Staff
     */
    public function setPosition(string $position): Staff
    {
        $this->position = $position;
        return $this;
    }

    /**
     * 用戶歸屬的部門
     */
    public function departments()
    {
        return $this->belongsToMany(Department::class);
    }

    /**
     * 用戶歸屬的角色
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}

這個(gè)是我的測(cè)試腳本。

$initAccount = new Staff();
$initAccount->setName("demo");
$initAccount->save();

查看數(shù)據(jù)結(jié)果:
截圖

上圖顯示的其他各字段的值異常,起碼應(yīng)當(dāng)有一個(gè) name 的值是 demo。

staff sql:

CREATE TABLE `space_staff` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `position` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `avatar` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

不是很熟悉Eloquent,但是翻閱Eloquent文檔并未發(fā)現(xiàn)解決辦法。請(qǐng)問(wèn)我是哪里配置沒(méi)寫對(duì)?

1219 2 0
2個(gè)回答

mosquito
  • bear 2022-12-12

    非常感謝。確實(shí)沒(méi)用過(guò) eloquent.我再仔細(xì)翻下文檔。

  • bear 2022-12-12

    是的呢,之前還帶著之前 Doctrine 的思想在寫。

xiuwang

不用這么麻煩

1、執(zhí)行命令 composer require webman/console 安裝webman命令行

2、假設(shè)你數(shù)據(jù)庫(kù)里有一個(gè)user表,里面有id nickname password三個(gè)字段,使用命令

./webman make:model user

它會(huì)根user據(jù)表自動(dòng)生成一個(gè) app/model/User.php 文件,并且生成表里id nickname password字段相關(guān)的注釋,內(nèi)容大概這樣

<?php
namespace app\model;
use support\Model;

/**
 * @property integer $id (主鍵)      // 這里都是自動(dòng)生成的 
 * @property string $nickname 昵稱  // 這里都是自動(dòng)生成的
 * @property string $password 密碼   // 這里都是自動(dòng)生成的
 */
class User extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'uid';

}

然后你就可以用類似下面代碼向數(shù)據(jù)庫(kù)里插入數(shù)據(jù)了

use app\model\User;
$user = new User;
$user->nickname = 'xx';
$user->password = 'xx';
$user->save();

因?yàn)橛凶詣?dòng)生成的注釋,phpstom等ide都會(huì)自動(dòng)提示User類里的屬性,很方便。
不用手動(dòng)寫將類屬性寫到類里,也不用寫方法。

  • bear 2022-12-12

    非常感謝,幫大忙了。因?yàn)槲沂窍榷x了模型再創(chuàng)建表的,所以console生成的代碼 沒(méi)有那些字段。但由于一直以來(lái)習(xí)慣 get set 的代碼風(fēng)格,可能還是得改寫一下 model 里面的代碼。

年代過(guò)于久遠(yuǎn),無(wú)法發(fā)表回答
??