9

I can set a simple default value such as a string or boolean, but I can't find how to set the defualt for an entity.

In my User.php Entity:

/**
* @ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\Foo")
*/
protected $foo;

In the constructor I need to set a default for $foo:

public function __construct()
{
    parent::__construct();

    $this->foo = 1; // set id to 1
}

A Foo object is expected and this passes an integer.

What is the proper way to set a default entity id?

3 Answers 3

14

I think you're better to set it inside a PrePersist event.

In User.php:

use Doctrine\ORM\Mapping as ORM;

/**
* ..
* @ORM\HasLifecycleCallbacks
*/
class User 
{
         /**
         * @ORM\PrePersist()
         */
        public function setInitialFoo()
        {
             //Setting initial $foo value   
        }

}

But setting a relation value is not carried out by setting an integer id, rather it's carried out by adding an instance of Foo. And this can be done inside an event listener better than the entity's LifecycleCallback events (Because you'll have to call Foo entity's repository).

First, Register the event in your bundle services.yml file:

services:
    user.listener:
        class: Tsk\TestBundle\EventListener\FooSetter
        tags:
            - { name: doctrine.event_listener, event: prePersist }

And the FooSetter class:

namespace Tsk\TestBundle\EventListener\FooSetter;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Tsk\TestBundle\Entity\User;

class FooSetter
{
    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $entityManager = $args->getEntityManager();

        if ($entity instanceof User) {
            $foo = $entityManager->getRepository("TskTestBundle:Foo")->find(1);
            $entity->addFoo($foo);
        }
    }
}
3
  • 1
    This worked great. Thanks for the detailed answer. I thought it would be much simpler than that, and I couldn't find this documented anywhere.
    – hipnosis
    Commented Nov 30, 2013 at 21:31
  • @hipnosis Here's the official symfony tutorial about doctrine symfony.com/doc/current/book/doctrine.html. And about Events symfony.com/doc/current/cookbook/doctrine/…. Also you can check tutorials out there about building sites with symfony, Something like intelligentbee.com/blog/tag/symfony2-jobeet Commented Dec 1, 2013 at 21:39
  • For posterity, using an event listener also decouples the entity class from any particular ORM (in this case Doctrine). Thus, if there's ever a need to migrate to another ORM/DBAL, the entity won't have to be modified. Something to keep in mind when designing your application, generally. Commented Oct 15, 2014 at 14:41
5

I would stay well away from listeners in this simple example, and also passing the EntityManager into an entity.

A much cleaner approach is to pass the entity you require into the new entity:

class User
{

    public function __construct(YourEntity $entity)
    {
        parent::__construct();

        $this->setFoo($entity);
    }

Then elsewhere when you create a new entity, you will need to find and pass the correct entity in:

$foo = [find from entity manager]
new User($foo);

--Extra--

If you wanted to go further then the creation of the entity could be in a service:

$user = $this->get('UserCreation')->newUser();

which could be:

function newUser()
{
    $foo = [find from entity manager]
    new User($foo);
}

This would be my preferred way

-1

You can't just pass the id of the relationship with 'Foo'. You need to retrieve the Foo entity first, and then set the foo property. For this to work, you will need an instance of the Doctrine Entity Manager. But then, you make your entity rely on the EntityManager, this is something you don't want.

Example:

// .../User.php

public function __construct(EntityManager $em) {
    $this->em = $em;

    $this->foo = $this->em->getRepository('Foo')->find(1);
}
2
  • 6
    Explicitly making the entity dependent of EntityManager is really bad idea. Commented Nov 30, 2013 at 21:28
  • This is what I was thinking needs to happen, but it seemed wrong. Thanks for your help.
    – hipnosis
    Commented Nov 30, 2013 at 21:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.