简体   繁体   English

Symfony2 - 在实体构造函数中设置默认值

[英]Symfony2 - Set default value in entity constructor

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. 我可以设置一个简单的默认值,如字符串或布尔值,但我找不到如何为实体设置defualt。

In my User.php Entity: 在我的User.php实体中:

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

In the constructor I need to set a default for $foo: 在构造函数中,我需要为$ foo设置一个默认值:

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

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

A Foo object is expected and this passes an integer. 期望一个Foo对象,它传递一个整数。

What is the proper way to set a default entity id? 设置默认实体ID的正确方法是什么?

I think you're better to set it inside a PrePersist event. 我认为你最好把它放在PrePersist活动中。

In User.php : 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 . 但是设置关系值不是通过设置整数id ,而是通过添加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). 这可以在事件监听器内完成,而不是实体的LifecycleCallback事件(因为你必须调用Foo实体的存储库)。

First, Register the event in your bundle services.yml file: 首先,在bundle services.yml文件中注册事件:

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

And the FooSetter class: FooSetter类:

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);
        }
    }
}

I would stay well away from listeners in this simple example, and also passing the EntityManager into an entity. 在这个简单的例子中,我会远离监听器,并且还将EntityManager传递给实体。

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 这将是我的首选方式

You can't just pass the id of the relationship with 'Foo'. 你不能只用'Foo'传递关系的id。 You need to retrieve the Foo entity first, and then set the foo property. 您需要首先检索Foo实体,然后设置foo属性。 For this to work, you will need an instance of the Doctrine Entity Manager. 为此,您需要一个Doctrine Entity Manager实例。 But then, you make your entity rely on the EntityManager, this is something you don't want. 但是,你让你的实体依赖于EntityManager,这是你不想要的。

Example: 例:

// .../User.php

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

    $this->foo = $this->em->getRepository('Foo')->find(1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM