goldlife
goldlife

Reputation: 1989

Symfony 3 Doctrine Composite Key: single id is not allowed on composite primary key in entity

I have an entity with an

integer column (compositeId)

and a

string column (asin)

I want both columns working as a composite key. So I look here in the documentation:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html

But when I try to load my repository

$myEntity = $em->getRepository(MyEntity::class);

I got this error message:

single id is not allowed on composite primary key in entity AppBundle\Entity\MyEntity

This is my Entity:

<?php

namespace AppBundle\Entity;


use Doctrine\ORM\Mapping as ORM;

/**
 * @Entity(repositoryClass="AppBundle\Repository\MyEntityRepository")
 * @Table(name="my_entity")
 */
class MyEntity {

    /**
     * @Column(type="integer")
     * @GeneratedValue()
     */
    protected $id;

    /**
     * @var integer
     * @Id
     * @Column(type="integer", name="composite_id")
     */
    protected $compositeId;

    /**
     * @var string
     * @Id
     * @Column(type="string", name="asin")
     */
    protected $asin;

    /**
     * @var string
     * @Column(type="string", name="sku")
     */
    protected $sku;

    /**
     * @var string
     * @Column(type="string", name="ean")
     */
    protected $ean;

    /**
     * @codeCoverageIgnore
     * @return mixed
     */
    public function getId() {

        return $this->id;
    }

    /**
     * @param mixed $id
     *
     * @return MyEntity;
     */
    public function setId($id) {

        $this->id = $id;

        return $this;
    }

    /**
     * @codeCoverageIgnore
     * @return int
     */
    public function getCompositeId() {

        return $this->compositeId;
    }

    /**
     * @param int $compositeId
     *
     * @return MyEntity;
     */
    public function setCompositeId($compositeId) {

        $this->compositeId = $compositeId;

        return $this;
    }

    /**
     * @codeCoverageIgnore
     * @return string
     */
    public function getAsin() {

        return $this->asin;
    }

    /**
     * @param string $asin
     *
     * @return MyEntity;
     */
    public function setAsin($asin) {

        $this->asin = $asin;

        return $this;
    }

    /**
     * @codeCoverageIgnore
     * @return string
     */
    public function getSku() {

        return $this->sku;
    }

    /**
     * @param string $sku
     *
     * @return MyEntity;
     */
    public function setSku($sku) {

        $this->sku = $sku;

        return $this;
    }

    /**
     * @codeCoverageIgnore
     * @return string
     */
    public function getEan() {

        return $this->ean;
    }

    /**
     * @param string $ean
     *
     * @return MyEntity;
     */
    public function setEan($ean) {

        $this->ean = $ean;

        return $this;
    }
}

What am I doing wrong? When I remove @generatedValue at my ID table it works. But I need auto generated values at my ID column. I don't have any @Id annotation at my ID column, only @generatedValue, and on my both @Id columns I don't have any annotation with @generatedValue but I got this error ...

Upvotes: 4

Views: 14866

Answers (2)

Jenko
Jenko

Reputation: 789

Doctrine only supports @GeneratedValue() on @Id fields.

This annotation is optional and only has meaning when used in conjunction with @Id.

https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref_generatedvalue

You may be able to use columnDefinition as a workaround, e.g.

@ORM\Column(type="integer", name="id", columnDefinition="INT AUTO_INCREMENT")

as suggested here: https://stackoverflow.com/a/34749619/380054

Upvotes: 1

Wj Zhang
Wj Zhang

Reputation: 11

You need to remove all @Id in properties description except protected $id.

Upvotes: -6

Related Questions