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

标签 php mysql symfony doctrine entity

到目前为止我所做的是 -> 我在其中创建了一个包和实体类,并使用以下命令从该实体类创建了一个名为“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/

相关文章:

php - 镜像 API PHP 示例脚本 : how to trigger timeline inserts

php - 使用 PDO 将 polymer 数据绑定(bind)到 MySQL 表数据

mysql - 按 RAND() LIMIT 100 更新订单

PHP 正则表达式 : zero or more whitespace not working

php - MySQL 保存到数据库 Symfony2 时出错

php - 阻止web 'User A'通过查询查看MySQL中的 'User B'记录?

php - 使用php从mysql表中排序数据

php - Android 应用程序未获得 JSON 响应

mysql - 我应该如何优化mysql设置以避免查询慢的问题?

ajax - 用于调用 ajax 的按钮的 Symfony Crawler