Doctrine2 is trying to insert aggregate field

284 views Asked by At

I'm working on a Symfony2 application with Doctrine2 as ORM and FOSRestBundle as REST API.

Many of my entites got composed PK and their fields can't be auto-generated (users have to type them). Because I'm working with JMSSerializerBundle, I had to create aggregate fields to fill the PK fields. Indeed, if I put PK fields in the sent JSON, JMS try to make an update (that makes sens). To sum up, I fill the PK fields when JMS deserialize the aggregates ones into my object.

Here the concerned doctrine variables:

 /**
 * @var string
 *
 * @Accessor(getter="getPostCPRO", setter="setPostCPRO")
 * @ORM\Column(type="string")
 */
private $post_cpro;

public function getPostCPRO()
{
    return $this->post_cpro;
}

public function setPostCPRO($post_cpro)
{
    $this->cpro = $post_cpro;
}

/**
 * @var string
 *
 * @Accessor(getter="getPostNPROVER", setter="setPostNPROVER")
 * @ORM\Column(type="string")
 */
private $post_nprover;

public function getPostNPROVER()
{
    return $this->post_nprover;
}

public function setPostNPROVER($post_nprover)
{
    $this->nprover = $post_nprover;
}

/**
 * @var string
 *
 * @ORM\Column(name="CPRO", type="string", length=17)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="NONE")
 */
private $cpro;

/**
 * @var string
 *
 * @ORM\Column(name="NPROVER", type="decimal")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="NONE")
 */
private $nprover;

CPRO and NPROVER are the PK, POST_CPRO and POST_NPROVER are the aggregate fields.

But, when I try to persist, I've an SQL error because Doctrine want to insert both PK and aggregate fields. How can I prevent Doctrine to INSERT POST_CPRO and POST_NPROVER ?

Here my postAction():

public function postProAction(Request $request)
{
    $detachedEntity = $this->deserialize($request, 'ProductBundle\Entity\Pro');
    $em = $this->getDoctrine()->getManager();
    $em->persist($detachedEntity);
    $em->flush();

    return $detachedEntity;
}

and SQL error:

"message": "An exception occurred while executing 'INSERT INTO PRO (post_cpro, post_nprover, CPRO, NPROVER, ...) with params [null, null, \"ASSOC000000000009\", \"2\", ...] SQLSTATE[HY000]: General error: 207 General SQL Server error: Check messages from the SQL Server [207] (severity 16) [(null)]

Thanks

0

There are 0 answers