0

I am following this question here Explicitly set Id with Doctrine when using "AUTO" strategy

But I can't seem to make it work on my application.

My entity SuppliersOrdersDeliveries:

class SuppliersOrdersDeliveries
{
/**
 * @var integer
 *
 * @ORM\Column(name="supplier_order_id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="NONE")
 */
private $supplierOrderId;
{...}
}

The main key is

supplier_order_id

YAML file:

    id:
    supplierOrderId:
        type: integer
        nullable: false
        unsigned: true
        comment: 'Supplier order ID'
        id: true
        column: supplier_order_id
        generator:
            strategy: NONE

And this is how I persist the data into DB:

$dateTimeDeliveryTerm = new DateTime($generalDeliveryTerm);
$delivery = new \Doctrine\Entities\SuppliersOrdersDeliveries();
$delivery->setGeneralDeliveryTerm($dateTimeDeliveryTerm);
$delivery->setSupplierOrderId($supplier_order['supplier_order_id']);
$delivery->setCmsUserId($this->cmsusers->get('id'));

$this->eMgr->getClassMetaData(get_class($delivery))->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
$this->eMgr->getClassMetaData(get_class($delivery))->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);

$this->eMgr->persist($delivery);
$this->eMgr->flush();

Problem is that I still get the null on insert at supplier_order_id variable.

Also I tried to set the strategy to NONE on YAML and entity files, but the error stays the same as I would change the type later using setIdGeneratorType function.

I don't want the supplier_order_id to be auto incremented or anything else, I need to set it by myself.

Any ideas?

The50
  • 1,096
  • 2
  • 23
  • 47
  • Why you are setting the generator strategy to AUTO and after you are using a custom id generator? Or why you are stting the `supplier_order_id` value if you want an Auto generation? Can you clarify you goals in this question? – Yulio Aleman Jimenez Nov 28 '18 at 15:55
  • Edited the main post. I get the same null values even if I set the strategy to NONE. And no, I don't want the auto increment, I need to set the value by myself. – The50 Nov 28 '18 at 16:02
  • If you don't want an AUTO generation of the `supplier_order_id` you must to remove all lines related with it `@ORM\GeneratedValue(strategy="NONE")` and `$this->eMgr->getClassMetaData(***)***`. You just need to set the value manually as in this line `$delivery->setSupplierOrderId($supplier_order['supplier_order_id']);` – Yulio Aleman Jimenez Nov 28 '18 at 16:21
  • Doesn't help, I still get null on my supplier_order_id. – The50 Nov 28 '18 at 16:23
  • 1
    Not sure where the error is but you can't mix annotations and yaml files. Pick one or the other but not both. Also Doctrine ORM deals with objects so you should have $delivery->setSupplierOrder($supplierOrder); You seldom deal with the actual id. Might want to review the examples in the docs on how to associate entities. – Cerad Nov 28 '18 at 17:27

1 Answers1

0

Solved this by setting an SupplierOrder object into the delivery entity, instead of just putting plain ID value. Just as @Cerad said on the comments above ^^.

$delivery->setSupplierOrder($supplierOrder); 
The50
  • 1,096
  • 2
  • 23
  • 47