Symfony2 Doctrine ORM Composite Primary Keys

I am developing an application in Symfony 2.3 with Doctrine 2.4 as an ORM. I am using the PostgreSQL database engine. I am having problems comparing objects with compound primary keys in other tables. These keys are foreign keys in the corresponding key.

The tables in my database have the following structure

CREATE TABLE public.establecimiento ( id_establecimiento integer NOT NULL, establecimiento character varying(100) NOT NULL, CONSTRAINT pk_establecimiento PRIMARY KEY (id_establecimiento ) ) WITH ( OIDS=FALSE ); CREATE TABLE public.establecimiento_sec ( id_establecimiento_sec integer NOT NULL, id_establecimiento integer NOT NULL, det_seccion character varying(40) NOT NULL, plano character varying(100), sector_ingreso character varying(254), sponsor_imagen_sec character varying(96000), CONSTRAINT pk_establecimientos_sec PRIMARY KEY (id_establecimiento_sec , id_establecimiento ), CONSTRAINT fk_establec_reference_establec FOREIGN KEY (id_establecimiento) REFERENCES public.establecimiento (id_establecimiento) MATCH SIMPLE ON UPDATE RESTRICT ON DELETE RESTRICT ) WITH ( OIDS=TRUE ); CREATE TABLE public.establecimiento_sec_plano ( id_establecimiento_sec_plano integer NOT NULL, id_establecimiento_sec integer NOT NULL, id_establecimiento integer NOT NULL, det_plano character varying(512), cantidad integer NOT NULL, precio double precision, insert_charge double precision DEFAULT 0, descr character varying(254), CONSTRAINT pk_establecimiento_sec_plano PRIMARY KEY (id_establecimiento_sec_plano , id_establecimiento_sec , id_establecimiento ), CONSTRAINT fk_establecimiento_sec FOREIGN KEY (id_establecimiento, id_establecimiento_sec) REFERENCES public.establecimiento_sec (id_establecimiento, id_establecimiento_sec) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE ) WITH ( OIDS=FALSE ); 

Definition of the establecimientoSecPlano object, the $ establecimientoSec ​​variable, containing the keys $ establecimiento and $ id_establecimiento_sec

// Entity / EstablecimientosSecPlano

 /** * @ORM\Id * @ORM\ManyToOne(targetEntity="Ticketway\PruebaBundle\Entity\EstablecimientosSec") * @ORM\JoinColumns( * @ORM\JoinColumn(name="id_establecimiento_sec", referencedColumnName="id_establecimiento_sec"), * @ORM\JoinColumn(name="id_establecimiento", referencedColumnName="id_establecimiento")) */ private $establecimientoSec; 

// Entity / EstablecimientosSec

  /** * @ORM\Id * @ORM\ManyToOne(targetEntity="Ticketway\PruebaBundle\Entity\Establecimientos") * @ORM\JoinColumn(name="id_establecimiento", referencedColumnName="id_establecimiento") */ private $establecimiento; 

When doing doctrine: mapping: import command, I get the following error

[Teaching \ ORM \ Mapping \ MappingException] Cannot map the object "EstablecimientoSec" to the composite primary key as part of the primary key of another object "EstablecimientoSecPlano # idEstablecimiento".

I wonder if there is a way to define entities in symfony, and I cannot do this with a doctrine.

Is it possible to map functions differently so that the application works correctly?

I hope that my question will be understood. thanks

+7
source share
1 answer

You encountered this problem because your composite foreign key is another primary key of the table. This is not a good development practice, so it is simply not supported by the Doctrine, and I strongly doubt that it will ever be.

Solution 1 (preferred):

Add a single auto increment primary key to EstablecimientosSec . You can then reference EstablecimientosSec.id .

Solution 2:

If changing the database structure is absolutely impossible, do not map it. Instead, you can get the associated EstablecimientosSec objects in a separate request using a composite primary key. This is not a prefect decision, but it works under these restrictions. Tip. Do not query related objects as part of a loop.

+1
source

All Articles