Doctrine 2.5 key composite error mapping

1

Good afternoon I'm doing a project in Symfony 3 in which I use Doctrine 2.5 .

In this project I have the following tables:

CREATE TABLE companies (
    id_company INT NOT NULL AUTO_INCREMENT,
    id_user INT NOT NULL UNIQUE,
    name VARCHAR(200) NOT NULL,
    address VARCHAR(200) NULL,
    phone INT NULL UNIQUE,
    email VARCHAR(200) NULL UNIQUE, 
    createat TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updateat DATETIME NULL,
    PRIMARY KEY (id_company),
    FOREIGN KEY (id_user) REFERENCES users(id_user)
);

CREATE TABLE faces (
    id_face INT NOT NULL AUTO_INCREMENT,
    image VARCHAR(200) NOT NULL,
    PRIMARY KEY (id_face)
);

CREATE TABLE points (
    id_point INT NOT NULL AUTO_INCREMENT,
    point_value INT NOT NULL,
    PRIMARY KEY (id_point)
);

CREATE TABLE companies_faces_points (   
    id_company INT NOT NULL,
    id_face INT NOT NULL,
    id_point INT NOT NULL,
    PRIMARY KEY (id_company,id_face,id_point),
    FOREIGN KEY (id_company) REFERENCES companies(id_company),
    FOREIGN KEY (id_face) REFERENCES faces(id_face),
    FOREIGN KEY (id_point) REFERENCES points(id_point)
);

I generate the bundle with the following command in cmd:

php bin/console doctrine:gernerate:bundle ---namespace=BackendBundle --format=yml

And it generates me the bundle without problems. This is where my problem comes from.

Now I create the mapping with the following command:

php bin/console doctrine:mapping:import BackendBundle

It generates the map of the tables:

  • companies
  • points
  • faces

But does not generate the map of the table:

  • companies_faces_points

The terminal does not give me any kind of error.

  

Information sought

I have been searching without any results, and what I have found is that it allows the user of composite keys but I do not know why the table mapping does not work for me.

  

Type of solution

I am looking for a solution which allows me to keep the composite key.

If while you read this post, you came up with the idea of creating a generic id and leaving the other two fields as foregin key. I will not consider it a valid solution.

  

News

After getting it to create the entity of the table with composite key, it returns the following error:

No identifier/primary key specified for Entity "BackendBundle\Entity\CompaniesFacesPoints". Every Entity must have an identifier/primary key.

Code of the entity:

<?php

namespace BackendBundle\Entity;

/**
* CompaniesFacesPoints
*/
class CompaniesFacesPoints
{
/**
 * @var \BackendBundle\Entity\Companies
 */
private $idCompany;

/**
 * @var \BackendBundle\Entity\Faces
 */
private $idFace;

/**
 * @var \BackendBundle\Entity\Points
 */
private $idPoint;


/**
 * Set idCompany
 *
 * @param \BackendBundle\Entity\Companies $idCompany
 *
 * @return CompaniesFacesPoints
 */
public function setIdCompany(\BackendBundle\Entity\Companies $idCompany = null)
{
    $this->idCompany = $idCompany;

    return $this;
}

/**
 * Get idCompany
 *
 * @return \BackendBundle\Entity\Companies
 */
public function getIdCompany()
{
    return $this->idCompany;
}

/**
 * Set idFace
 *
 * @param \BackendBundle\Entity\Faces $idFace
 *
 * @return CompaniesFacesPoints
 */
public function setIdFace(\BackendBundle\Entity\Faces $idFace = null)
{
    $this->idFace = $idFace;

    return $this;
}

/**
 * Get idFace
 *
 * @return \BackendBundle\Entity\Faces
 */
public function getIdFace()
{
    return $this->idFace;
}

/**
 * Set idPoint
 *
 * @param \BackendBundle\Entity\Points $idPoint
 *
 * @return CompaniesFacesPoints
 */
public function setIdPoint(\BackendBundle\Entity\Points $idPoint = null)
{
    $this->idPoint = $idPoint;

    return $this;
}

/**
 * Get idPoint
 *
 * @return \BackendBundle\Entity\Points
 */
public function getIdPoint()
{
    return $this->idPoint;
}
}

Translate the code here

Novedades sobre las novedades

The error No identifier / primary key specified comes because when using the automatic generator I do not generate ids in the following yml:

CompaniesFacesPoints

BackendBundle\Entity\CompaniesFacesPoints:
type: entity
table: companies_faces_points
indexes:
    id_face:
        columns:
            - id_face
    id_point:
        columns:
            - id_point
    IDX_11231BC89122A03F:
        columns:
            - id_company
id: {}  
oneToOne:
    idCompany:
        targetEntity: Companies
        cascade: {  }
        fetch: LAZY
        mappedBy: null
        inversedBy: null
        joinColumns:
            id_company:
                referencedColumnName: id_company
        orphanRemoval: false
    idFace:
        targetEntity: Faces
        cascade: {  }
        fetch: LAZY
        mappedBy: null
        inversedBy: null
        joinColumns:
            id_face:
                referencedColumnName: id_face
        orphanRemoval: false
    idPoint:
        targetEntity: Points
        cascade: {  }
        fetch: LAZY
        mappedBy: null
        inversedBy: null
        joinColumns:
            id_point:
                referencedColumnName: id_point
        orphanRemoval: false
lifecycleCallbacks: {  }

How do I fill in the ids in this case? I tried to put them in the code of another yml and changing the references but I skipped the following error when doing that:

Property "idCompany" in "BackendBundle\Entity\CompaniesFacesPoints" was already declared, but it must be declared only once
    
asked by mohamet monte 09.11.2016 в 12:09
source

2 answers

0

As @Muriano comments, Doctrine supports composite keys:

link

But, from experience, it has always been a somewhat experimental feature.

From what I see, it is the same case as that presented in the documentation:

link

What I would do is create those three tables of the documentation, see what SQL code they generate, and adjust my tables to that code.

Likewise, instead of doing reverse engineering, what you can do is create the models, check the SQL they would generate to create the tables, see that they do not change anything, DO NOT update the model of the database, and work against the same database; It should work.

Try and tell us:)

Greetings.

    
answered by 10.11.2016 в 12:46
0

Have you checked that you create the composite key well? Try doing the following:

CREATE TABLE companies_faces_points (   
    id_company INT NOT NULL,
    id_face INT NOT NULL,
    id_point INT NOT NULL,
    FOREIGN KEY (id_company) REFERENCES companies(id_company),
    FOREIGN KEY (id_face) REFERENCES faces(id_face),
    FOREIGN KEY (id_point) REFERENCES points(id_point)
);

ALTER TABLE companies_faces_points 
 ADD PRIMARY KEY ('id_company', 'id_face', 'id_point'),
 ADD KEY 'IDX_9F267A24CCD7E912' ('id_company'),
 ADD KEY 'IDX_9A267A754CEE9132' ('id_face'),
 ADD KEY 'IDX_9F267A24D60322AC' ('id_point');
    
answered by 11.11.2016 в 14:16