Doctrine 2: cascade persist Oracle “IDENTITY” is r

2019-03-04 06:20发布

I am using doctrine 2 with oracle, the tables in the database has some triggers that generate the IDs, and my ID mapping of my tables is like the following:

/**
 * @orm\Id
 * @orm\Column(type="integer");
 * @orm\GeneratedValue(strategy="IDENTITY")
 */
protected $id;

and I have a OneToMany relation, with cascade={"persist"} but it is not working, I tried the same code with MySQL and it is working fine, but in oracle the last insert Id seems to always return 0 instead of the real id of the inserted row... and so the cascade persist is not working... is this a bug in doctrine or am I doing something wrong? any help?

After following the code it seems that the method

Doctrine\ORM\Id\IdentityGenerator::generate

is returning 0, I don't know why it is being invoked since the sequenceName is null (there is no sequence in the deffinition!

EDIT: Here are the entities: The Client Entity:

/** 
 * @ORM\Entity 
 * @ORM\Table(name="clients")
 **/
class Client {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /** @ORM\Column(name="name",type="string",length=255,unique=true) */
    protected $name;

    /**
    * @ORM\OneToMany(targetEntity="ContactInformation", mappedBy="client", cascade={"persist"})
    **/
    protected $contactInformations;

    public function __construct() {
        $this->contactInformations = new ArrayCollection();
    }

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
        return $this;
    }

    public function getContactInformations() {
        return $this->contactInformations;
    }

    public function addContactInformations(Collection $contactInformations)
    {
        foreach ($contactInformations as $contactInformation) {
            $contactInformation->setClient($this);
            $this->contactInformations->add($contactInformation);
        }
    }

    /**
     * @param Collection $tags
     */
    public function removeContactInformations(Collection $contactInformations)
    {
        foreach ($contactInformations as $contactInformation) {
            $contactInformation->setClient(null);
            $this->contactInformations->removeElement($contactInformation);
        }
    }

    public function setContactInformations($contactInformations) {
        $this->contactInformations = $contactInformations;
        return $this;
    }
}

The Contact Information Entity:

/** 
 * @ORM\Entity 
 * @ORM\Table(name="contact_informations")
 **/
class ContactInformation {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="ContactInformationType")
     * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
     **/
    protected $type;

    /** @ORM\Column(type="text") */
    protected $value;

    /**
     * @ORM\ManyToOne(targetEntity="Client", inversedBy="contact_informations")
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id")
     **/
    private $client;

    public function getId() {
        return $this->id;
    }

    public function getType() {
        return $this->type;
    }

    public function setType($type) {
        $this->type = $type;
        return $this;
    }

    public function getValue() {
        return $this->value;
    }

    public function setValue($value) {
        $this->value = $value;
        return $this;
    }

    public function getClient() {
        return $this->client;
    }

    public function setClient($client = null) {
        $this->client = $client;
        return $this;
    }
}

1条回答
Luminary・发光体
2楼-- · 2019-03-04 06:33

Oracle doesn't support auto incrementing, so you cannot use the "IDENTITY" strategy in Doctrine. You'll have to use the "SEQUENCE" (or "AUTO") strategy.

When specifying "AUTO", Doctrine will use "IDENTITY" for MySql and "SEQUENCE" for Oracle.

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifier-generation-strategies

查看更多
登录 后发表回答