Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create database table from entity in symfony 2.6

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

php app/console doctrine:schema:update --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' ".

class user { 
   private $id; 
   private $userEmail; 

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

   public function setUserEmail($userEmail) { 
       $this->userEmail = $userEmail; 
       return $this; 
   } 
}
like image 412
Umair Malik Avatar asked Apr 22 '15 13:04

Umair Malik


2 Answers

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.xml like:

<?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:

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...

like image 140
cn007b Avatar answered Nov 14 '22 23:11

cn007b


For symfony 4 and 4++:

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

For symfony 3:

php app/console doctrine:schema:update --force
like image 44
pdchaudhary Avatar answered Nov 14 '22 23:11

pdchaudhary