到目前为止我所做的是 -> 我在其中创建了一个包和实体类,并使用以下命令从该实体类创建了一个名为“news”的数据库表
php app/console doctrine: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;
}
}
最佳答案
您的实体不包含注释,并且 Doctrine 不知道如何处理该实体。但是,如果您向实体添加类似以下内容:
<?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
坚信它会解决您的问题...
关于php - 如何从 symfony 2.6 中的实体创建数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29799510/