Eloquent 写入数据结果不符合预期

bear

问题描述

使用 Eloquant 定义了一个 Model,根据文档和测试写入一行记录。数据行插入但是不符合预期。

这个是我的 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);
    }
}

这个是我的测试脚本。

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

查看数据结果:
截图

上图显示的其他各字段的值异常,起码应当有一个 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文档并未发现解决办法。请问我是哪里配置没写对?

694 2 0
2个回答

mosquito
  • bear 2022-12-12

    非常感谢。确实没用过 eloquent.我再仔细翻下文档。

  • bear 2022-12-12

    是的呢,之前还带着之前 Doctrine 的思想在写。

xiuwang

不用这么麻烦

1、执行命令 composer require webman/console 安装webman命令行

2、假设你数据库里有一个user表,里面有id nickname password三个字段,使用命令

./webman make:model user

它会根user据表自动生成一个 app/model/User.php 文件,并且生成表里id nickname password字段相关的注释,内容大概这样

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

/**
 * @property integer $id (主键)      // 这里都是自动生成的 
 * @property string $nickname 昵称  // 这里都是自动生成的
 * @property string $password 密码   // 这里都是自动生成的
 */
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';

}

然后你就可以用类似下面代码向数据库里插入数据了

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

因为有自动生成的注释,phpstom等ide都会自动提示User类里的属性,很方便。
不用手动写将类属性写到类里,也不用写方法。

  • bear 2022-12-12

    非常感谢,帮大忙了。因为我是先定义了模型再创建表的,所以console生成的代码 没有那些字段。但由于一直以来习惯 get set 的代码风格,可能还是得改写一下 model 里面的代码。

年代过于久远,无法发表回答
🔝