2

I'm working on a project in Symfony2 with a legacy database, that uses a string to have a 6 digit 'franchisee_number' with floating 0's on the front - eg. 000123. When I try to flush the entity, I get an error that shows the SQL statement it tried to do, and it doesn't even try to insert to insert my primary key field. Up until the point of flushing the entity, the 'franchisee_id' is showing up in the entity, so I'm assuming there's a problem with Doctrine not wanting to set the Primary Key, but have it generated.

My problem is very similar to this question: when flush() primary key does not get inserted, but I've tried the answers listed, and they don't work.

Here is the code from my Entity for the 'franchisee_number' field:

/**
 * @var string
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="NONE")
 * @ORM\Column(name="franchisee_number", type="string", length=255, nullable=false)
 *
 */
private $franchiseeNumber;

I assumed that @ORM\GeneratedValue(strategy="NONE") would tell doctrine I'm inserting my own value into the field, and not trying to generate it.

And my controller code

    public function createAction(Request $request)
{
    $this->denyAccessUnlessGranted('ROLE_ADMIN');
    $entity = new Accounts();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager('inertia');
        $em->persist($entity);
        $em->flush();
        return $this->redirect($this->generateUrl('accounts_show', array('id' => $entity->getId())));
    }

    return $this->render('InertiaBundle:Accounts:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

I also made sure there were no xml config files in the Bundle/Resources/config/doctrine directory.

Thanks in advance for the help!

Community
  • 1
  • 1
chrislebaron
  • 269
  • 3
  • 14

2 Answers2

0

I wasted a lot of time trying to figure this out, and couldn't really find a great answer. I just decided to do it the hard way and wrote a custom query with Doctrine DBAL, inspired by this question/answer: https://stackoverflow.com/a/15650290/1647183

It works like a charm for me!

Community
  • 1
  • 1
chrislebaron
  • 269
  • 3
  • 14
0

If you use strategy="NONE", you must create a identifier in __construct or another systems before persist/flush entity.

Example:

class MyEntity
{
    /**
     * @var string
     *
     * @ORM\Id
     * @ORM\Column(type="string", name="id")
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $id;

    public function __construct()
    {
        $this->id = 123; // Inject you custom ID here!
    }
}
ZhukV
  • 2,892
  • 6
  • 25
  • 36
  • Forgive me if I'm naive, but using this method is it even possible to set the id to the data from my form ? I'm pretty new to Symfony so I could be totally wrong here, but that looks pretty static to me. I looked through my code step by step by dumping the $entity and looking through the data, and it's all intact up through the persist. It's just when flushing that it had issues. Does that make sense? – chrislebaron May 12 '16 at 13:58