php 如何从symfony 2.6中的实体创建数据库表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29799510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:35:59  来源:igfitidea点击:

How to create database table from entity in symfony 2.6

phpmysqlsymfonydoctrineentity

提问by Umair Malik

what i've done so far is -> i have created a bundle and entity class in it and created a database table named "news" from that entity class using following command

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

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

php 应用程序/控制台学说:模式:更新 --force

everything went well.

一切顺利。

now i created a new bundle and an other entity class in it from which i want to create an other table in database named "user" but gives an error that "The table with name 'symfony.news' already exists' ".

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

class user { 
   private $id; 
   private $userEmail; 

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

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

回答by V. Kovpak

Your entity doesn't contain annotations, and doctrine have no idea what to do with this entity. But if you add to you entity something like:

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

<?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; 
    }
}

OR

或者

if you add file: User.orm.xmllike:

如果添加文件: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>

to your Resources/config/doctrine/directory, you'll be able to run command:

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

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

and as result you'll receive:

结果你会收到:

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

Truly believe that it will solve your problem...

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

回答by pdchaudhary

For symfony 4:

对于 symfony 4:

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

For symfony 3:

对于 symfony 3:

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

回答by Stev

Why don't you use doctrine command?

你为什么不使用学说命令?

php app/console doctrine:generate:entity