【问题标题】:How to create database table from entity in symfony 2.6如何在 symfony 2.6 中从实体创建数据库表
【发布时间】:2015-04-22 13:44:25
【问题描述】:

到目前为止我所做的是 -> 我在其中创建了一个包和实体类,并使用以下命令从该实体类创建了一个名为“news”的数据库表

php 应用程序/控制台原则:schema:update --force

一切顺利。

现在我在其中创建了一个新包和一个其他实体类,我想从中创建一个名为“user”的数据库中的另一个表,但给出错误“名称为'symfony.news'的表已经存在'” .

class user { 
   private $id; 
   private $userEmail; 

   public function getId() { 
       return $this->id; 
   } 

   public function setUserEmail($userEmail) { 
       $this->userEmail = $userEmail; 
       return $this; 
   } 
}

【问题讨论】:

    标签: php mysql symfony doctrine entity


    【解决方案1】:

    您的实体不包含注释,并且学说不知道如何处理该实体。但是,如果您向您的实体添加如下内容:

    <?php
    
    namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="user")
     */
    class User
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string", length=60, unique=true)
         */
        private $email;
    
        public function getId()
        { 
            return $this->id; 
        } 
    
        public function setUserEmail($userEmail)
        { 
            $this->userEmail = $userEmail; 
            return $this; 
        }
    }
    

    如果添加文件:User.orm.xml 喜欢:

    <?xml version="1.0" encoding="utf-8"?>
    <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
      <entity name="AppBundle\Entity\User" table="user">
        <unique-constraints>
          <unique-constraint name="UNIQ_797E6294E7927C74" columns="email"/>
        </unique-constraints>
        <id name="id" type="integer" column="id">
          <generator strategy="IDENTITY"/>
        </id>
        <field name="email" type="string" column="email" length="60" nullable="false"/>
      </entity>
    </doctrine-mapping>
    

    到您的Resources/config/doctrine/ 目录, 您将能够运行命令:

    php app/console doctrine:schema:update --force
    

    结果你会收到:

    Updating database schema...
    Database schema updated successfully! "1" queries were executed
    

    真正相信它会解决你的问题...

    【讨论】:

      【解决方案2】:

      对于 symfony 4 和 4++:

      php bin/console doctrine:schema:update --force
      

      对于 symfony 3:

      php app/console doctrine:schema:update --force
      

      【讨论】:

      • php bin/console dictionary:schema:update --force Above Line 也适用于 symfony 5.2
      • 是 Symfony 4 和 4++ : .php bin/console 学说:schema:update --force
      【解决方案3】:

      如果 "php app/console dictionary:schema:update --force""php bin/console dictionary:schema:update --force" 都没有(你可能在 windows 上工作..) 正在解决,你可以试试:

      “php 工匠学说:schema:update --force”.

      唯一对我有用的事情。

      【讨论】:

        【解决方案4】:

        为什么不用教条命令?

        php app/console doctrine:generate:entity 
        

        【讨论】:

        • 我就像你说的那样,但它只在数据库中创建实体而不是表。这个命令 app/console 学说:schema:update --force 在数据库中创建表,但问题是我怎么能告诉这个命令读取所有包中的所有实体文件,如果表已经存在则跳过,否则创建该表..这个命令在第一次运行时完美运行
        • doctrine:schema:update 将更新所有表。如果表存在,那么它将检查任何列更改,但不会重新创建表。
        • 此外,如果您更改了元数据中的某些内容(列定义、关系等),您不能不修改旧表。因此,只要您不对旧实体进行修改,而只添加新实体,就可以安全地使用学说:模式:更新。
        猜你喜欢
        • 2014-12-30
        • 1970-01-01
        • 1970-01-01
        • 2016-08-04
        • 2022-11-30
        • 1970-01-01
        • 2017-06-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多