如何使用Alice和Doctrine Fixtures为多个字段设置相同的值?

时间:2016-09-01 13:29:52

标签: doctrine-orm symfony alice-fixtures

AppBundle\Entity\UserAccount:
    UserAccount_{1..5}:
        emailConfirmed: '80%? 1 : 0'
        enabled: ????

如何设置 enabled 字段以保存与 emailConfirmed 字段相同的值。换句话说 - 如何在同一个实体中引用其他字段值?

1 个答案:

答案 0 :(得分:0)

按文档中所述创建自定义处理器:

  

alice处理器可用于在持久化之前和之后操纵对象。要注册自己的处理器,请创建服务并对其进行标记。

随时创建处理器(例如AppBundle\DataFixtures\MyProcessor

<?php
namespace AppBundle\DataFixtures;
use Nelmio\Alice\ProcessorInterface;
class MyProcessor implements ProcessorInterface
{
    /**
     * @param object $object instance to process
     */
    public function preProcess($object)
    {
        if (!$object instanceof AppBundle\Entity\UserAccount) {
            return;
        }

        $object->setEnabled($object->getConfirmed());
    }

    /**
     * @param object $object instance to process
     */
    public function postProcess($object)
    {

    }
}

添加服务:

services:
    my.alice.processor:
        class: AppBundle\DataFixtures\MyProcessor
        tags:
            -  { name: h4cc_alice_fixtures.processor }