PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 11, 2022

[FIXED] Symfony Doctrine not working when uses "SEQUENCE" as GeneratedValue in Oracle

 January 11, 2022     doctrine, doctrine-orm, oracle, symfony     No comments   

Issue

I have the following Entity in Symfony 2 with an ID generated by an Oracle Sequence:

namespace PSBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="CONTRATO")
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="PSBundle\Entity\ContratoRepository")
 */
class Contrato
{
    /**
     * @var integer
     *
     * @ORM\Column(name="ID_CONTRATO", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="SID_CONTRATO", allocationSize=1, initialValue=1)
     */
    private $idContrato;

    // Other fields...
}

But when I do:

$entityContrato = new Contrato();
// Set some fields
$em->persist($entityContrato);

I get:

Entity of type PSBundle\Entity\Contrato is missing an assigned ID for field 'idContrato'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.

I know that SID_CONTRATO is well defined as if I select the NEXTVAL it works:

SID_CONTRATO works

What I'm missing? Any kind of light in the subject would be more than appreciated, my related dependencies are:

"php": "7.2",
"symfony/symfony": "2.3.42",
"doctrine/orm": "2.3.x-dev",
"doctrine/doctrine-bundle": "1.2.0",

Doctrine config (from config/config.yml, I don't have a doctrine.yaml file):

# Doctrine Configuration
doctrine:
    dbal:
        driver:   oci8
        port:     1521
        dbname:   ORCL
        host:     %dbal_host%
        user:     %dbal_user%
        password: %dbal_pass%
        charset:  UTF8
        memory: true

        logging: true

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

# IMPORTANT: this fixes bugs with the date format that is brought from Oracle ('d/m/y')
# The problem is described here: https://stackoverflow.com/questions/37723240/symfony-doctrine-oracle-datetime-format-issue 
services:
    oracle.listener:
        class: Doctrine\DBAL\Event\Listeners\OracleSessionInit
        tags:
            - { name: doctrine.event_listener, event: postConnect }

Solution

Ok, I found the problem:

If I do a var_dump() in doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php of $class->idGenerator it outputs:

object(Doctrine\ORM\Id\AssignedGenerator)

While if I do the same with entities that currently work with sequences they output:

object(Doctrine\ORM\Id\SequenceGenerator)...

So Symfony wasn't getting the correct type of Generator. I check the entity file and found out that there was some fields with @ORM\GeneratedValue(strategy="NONE"), eg.:

/**
 * @var integer
 *
 * @ORM\Column(name="ID_CONTRATO", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="SEQUENCE")
 * @ORM\SequenceGenerator(sequenceName="SID_CONTRATO", allocationSize=1, initialValue=1)
 */
private $idContrato;

/**
 * @var string
 *
 * @ORM\Column(name="TIPO", type="string", length=1, nullable=false)
 * @ORM\GeneratedValue(strategy="NONE")  // This was the conflicting line
 */
private $tipo;

Apparently, Symfony gets the last @ORM\GeneratedValue definition as the class identifier generator, which in this case had the strategy="NONE" so It couldn't get the next value from the sequence.



Answered By - Genarito
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing