更新数据库架构时不允许使用空值

Null value not allowed while updating Database schema

我有一个名为 "FosUser" 的实体:

<?php

namespace techeventBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * FosUser
 *
 * @ORM\Table(name="fos_user", uniqueConstraints={@ORM\UniqueConstraint(name="UNIQ_957A6479A0D96FBF", columns={"email_canonical"}), @ORM\UniqueConstraint(name="UNIQ_957A647992FC23A8", columns={"username_canonical"}), @ORM\UniqueConstraint(name="UNIQ_957A6479C05FB297", columns={"confirmation_token"})}, indexes={@ORM\Index(name="IDX_957A647914013572", columns={"copon_id"})})
 * @ORM\Entity
 */
class FosUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=180, nullable=true)
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(name="username_canonical", type="string", length=180, nullable=true)
     */
    private $usernameCanonical;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=180, nullable=false)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="email_canonical", type="string", length=180, nullable=true)
     */
    private $emailCanonical;

    /**
     * @var boolean
     *
     * @ORM\Column(name="enabled", type="boolean", nullable=true)
     */
    private $enabled;

    /**
     * @var string
     *
     * @ORM\Column(name="salt", type="string", length=255, nullable=true)
     */
    private $salt;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=255, nullable=false)
     */
    private $password;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="last_login", type="datetime", nullable=true)
     */
    private $lastLogin;

    /**
     * @var string
     *
     * @ORM\Column(name="confirmation_token", type="string", length=180, nullable=true)
     */
    private $confirmationToken;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="password_requested_at", type="datetime", nullable=true)
     */
    private $passwordRequestedAt;

    /**
     * @var array
     *
     * @ORM\Column(name="roles", type="array", nullable=true)
     */
    private $roles;

        /**
         * @var string
         *
         * @ORM\Column(name="nom", type="string", length=255, nullable=false)
         */
        private $nom;

        /**
         * @var string
         *
         * @ORM\Column(name="prenom", type="string", length=255, nullable=false)
         */
        private $prenom;

        /**
         * @var string
         *
         * @ORM\Column(name="tel", type="string", length=30, nullable=true)
         */
        private $tel;

        /**
         * @var integer
         *
         * @ORM\Column(name="age", type="integer", nullable=false)
         */
        private $age;

        /**
         * @var string
         *
         * @ORM\Column(name="sexe", type="string", length=255, nullable=true)
         */
        private $sexe;

        /**
         * @var string
         *
         * @ORM\Column(name="fonction", type="string", length=255, nullable=true)
         */
        private $fonction;

        /**
         * @var string
         *
         * @ORM\Column(name="address", type="string", length=255, nullable=true)
         */
        private $address;

        /**
         * @var string
         *
         * @ORM\Column(name="categorie", type="string", length=255, nullable=true)
         */
        private $categorie;

        /**
         * @var string
         *
         * @ORM\Column(name="type", type="string", length=255, nullable=true)
         */
        private $type;

        /**
         * @var string
         *
         * @ORM\Column(name="description", type="string", length=255, nullable=true)
         */
        private $description;

        /**
         * @var string
         *
         * @ORM\Column(name="role", type="string", length=255, nullable=true)
         */
        private $role;

        /**
         * @var \Coupon
         *
         * @ORM\ManyToOne(targetEntity="Coupon")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="copon_id", referencedColumnName="id")
         * })
         */
        private $copon;


    }

我已经删除了这个实体中的字段并将字段的默认值更改为 null ('nullable = true'),如下所示:

<?php

namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * FosUser
 *
 * @ORM\Table(name="fos_user", uniqueConstraints={@ORM\UniqueConstraint(name="UNIQ_957A6479A0D96FBF", columns={"email_canonical"}), @ORM\UniqueConstraint(name="UNIQ_957A647992FC23A8", columns={"username_canonical"}), @ORM\UniqueConstraint(name="UNIQ_957A6479C05FB297", columns={"confirmation_token"})}, indexes={@ORM\Index(name="IDX_957A647914013572", columns={"copon_id"})})
 * @ORM\Entity
 */
class FosUser extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255, nullable=true)
     */
    protected $nom;

    /**
     * @var string
     *
     * @ORM\Column(name="prenom", type="string", length=255, nullable=true)
     */
    protected $prenom;

    /**
     * @var string
     *
     * @ORM\Column(name="tel", type="string", length=30, nullable=true)
     */
    protected $tel;

    /**
     * @var integer
     *
     * @ORM\Column(name="age", type="integer", nullable=false)
     */
    protected $age;

    /**
     * @var string
     *
     * @ORM\Column(name="sexe", type="string", length=255, nullable=true)
     */
    protected $sexe;

    /**
     * @var string
     *
     * @ORM\Column(name="fonction", type="string", length=255, nullable=true)
     */
    protected $fonction;

    /**
     * @var string
     *
     * @ORM\Column(name="address", type="string", length=255, nullable=true)
     */
    protected $address;

    /**
     * @var string
     *
     * @ORM\Column(name="categorie", type="string", length=255, nullable=true)
     */
    protected $categorie;

    /**
     * @var string
     *
     * @ORM\Column(name="type", type="string", length=255, nullable=true)
     */
    protected $type;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=255, nullable=true)
     */
    protected $description;

    /**
     * @var string
     *
     * @ORM\Column(name="role", type="string", length=255, nullable=true)
     */
    protected $role;

    /**
     * @var \Coupon
     *
     * @ORM\ManyToOne(targetEntity="Coupon")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="copon_id", referencedColumnName="id")
     * })
     */
    protected $copon;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

我在更新数据库模式后遇到了这些错误:

In AbstractMySQLDriver.php line 112:

An exception occurred while executing 'ALTER TABLE fos_user CHANGE username username VARCHAR(180) NOT NULL, CHANGE username_canonical username_canonic al VARCHAR(180) NOT NULL, CHANGE email_canonical email_canonical VARCHAR(18 0) NOT NULL, CHANGE enabled enabled TINYINT(1) NOT NULL, CHANGE roles roles LONGTEXT NOT NULL COMMENT '(DC2Type:array)', CHANGE nom nom VARCHAR(255) D EFAULT NULL, CHANGE prenom prenom VARCHAR(255) DEFAULT NULL':

SQLSTATE[22004]: Null value not allowed: 1138 Utilisation incorrecte de la valeur NULL

In PDOConnection.php line 106:

SQLSTATE[22004]: Null value not allowed: 1138 Utilisation incorrecte de la valeur NULL

In PDOConnection.php line 104:

SQLSTATE[22004]: Null value not allowed: 1138 Utilisation incorrecte de la valeur NULL

你的代码是正确的! 之前你没有使用过 FOUser class,你的 fos_user table 已经有用户名,emailCanonical 等等... 然后,当您扩展 FOSUser 并更新数据库模式时,您当前的实体不接受用户名等的空值。(这是 FOSUser 逻辑)

解决方案,您必须在更新数据库模式之前删除 fos_user table