How to create database table from object in symfony 2.6

what i have done so far -> i have created a package and entity class in it and created a database table named "news" from this entity class using the following command

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

everything went well.

now I have created a new package and another entity class from which I want to create another table in the database named "user" but gives an error that "a table named" 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; 
   } 
}

      

+5


source to share


3 answers


Your entity has no annotations and doctrine has no idea what to do with this entity. But if you add an entity to yourself with 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 the directory Resources/config/doctrine/

, you should be able to run the command:

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

      

and as a result you get:

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

      

Truly believe that it will solve your problem ...

+9


source


Why aren't you using doctrine command?



php app/console doctrine:generate:entity 

      

0


source


For Symfony 4:

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

      

For Symfony 3:

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

      

0


source







All Articles