0
votes

Edit: I did what @MichaelHirschler and @MayaShah suggested and it solved the problem and it allow me to create a Doctrine Migration file using the doctrine:migration:diff command, but when I tried to migrate these changes to the database with doctrine:migration:migrate the following errors appeared.

Migration 20170325192546 failed during Execution. Error An exception occurred while executing 'ALTER TABLE business ADD CONSTRAINT FK_8D36E3844FFE0C3 FOREIGN KEY (client_email) REFERENCES client (clientEmail)':

SQLSTATE[HY000]: General error: 1005 Can't create table 'eship_case.#sql-81c_e' (errno: 150)
[Doctrine\DBAL\Exception\DriverException] An exception occurred while executing 'ALTER TABLE business ADD CONSTRAINT FK_8D36E3844FFE0C3 FOREIGN KEY (client_email) REFERENCES client (clientEmail)':

I understand that this is a MySQL error but I thought that Doctrine would take care of the migration, so when it didn't I don't know how to fix it. I could do the schema manually on MySQL to fix this but I dont know how would that affect the ORM schema in Symfony. Sorry for bothering, I'm new to PHP and therefore new to Symfony and Doctrine.

Original Post: I'm doing a web application using Symfony Framework with MySQL as the database. I'm using doctrine to do the ORM to map the database. But when I'm trying to update the schema of the database to include the foreign keys, the following error appears: 1 I don't really understad the error and I will appreciate any help anyone can give me.

Here's the code of both classes. The primary key of Client is clientEmail and I want to make a relation with that key to the business entity. I know there might be some documentation missing but I'm trying to fix this error before moving to that. The classes are longer but I only posted the areas that could be causing the problem.

Business Class:

<?php
/**
 * @ORM\Entity
 * @ORM\Table(name="business")
 */
class Business
{
   /**
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    * @ORM\Column(type="integer")
    */
    private $businessId;

   /**
    * @ORM\Column(type="string")
    */
    private $name;

   /**
    * @ORM\Column(type="string")
    */
    private $stage_Of_Development;


//Relations

    /**
    * @ORM\ManyToOne(targetEntity="Client")
    */
    private $client;


//Getters and Setters

    /**
     * @return mixed
     */
    public function getClient()
    {
        return $this->client;
    }

     /**
     * @param mixed $client
     */
    public function setClient(Client $client)
    {
        $this->client = $client;
    }
}

Client Class:

<?php
/**
 * @ORM\Entity
 * @ORM\Table(name="client")
 */
class Client
{
    /**
     * @ORM\ID
     * @ORM\Column(type="string")
     */
    private $clientEmail;    

    /**
     * @return mixed
     */
    public function getClientEmail()
    {
        return $this->clientEmail;
    }

    /**
     * @param mixed $clientEmail
     */
    public function setClientEmail($clientEmail)
    {
        $this->clientEmail = $clientEmail;
    }   
}
2
please post the full sql-exception, it may be hidden, but I can't tell whats going on.Michael Hirschler
Here's a picture of the command line: i.stack.imgur.com/i4StC.pngClassic Schmosby
Please take a look at this answer: stackoverflow.com/a/4673775/6849366. In general Doctrine does make a good job, but I think there is something wrong with your database setup.Michael Hirschler
I managed to fix the problem directly on MySQL, thanks for the help. Now I will go back to work with doctrine. Does the fact that I didnt fix the error that allowed me to export the schema from doctrine to MySQL, affect me in the implementation and communication with said databasae? Because as far as I know the schema was correct. The problem doctrine had was creating the foreign key, it created the column of said foreign key but it wasn't able to set it as the primary key.Classic Schmosby

2 Answers

1
votes

Please add join column for client field.

/**
* @ORM\ManyToOne(targetEntity="Client")
* @ORM\JoinColumn(name="client_email", referencedColumnName="clientEmail")
*/
private $client;
0
votes

Per default Doctrine maps a @ManyToOne to an id-field on the foreign entity. If you want to override it, you need to define it on your own:

class Business
{
    // ...

    /**
     * @ORM\ManyToOne(targetEntity="Client")
     * @ORM\JoinColumn(name="client_email", referencedColumnName="clientEmail")
     */
    private $client;

    // ..
}

Please take a look about Association Mapping in Doctrine.

Also please note: your current mapping doesn't ensure the uniqueness of a ClientEmail, which (depending on your application) might break your behavior if someone adds the same $clientEmail twice.