普通网友 2014-01-23 01:57
浏览 6
已采纳

在持久化之前创建机器可读的名称

In a Symfony2/Doctrine project, I have an Entity called Developer including those fields in particular:

 1. firstName
 2. lastName
 3. machineName

The form to create a Developer item only shows firstName and lastName fields. I'd like the machineName to be automatically generated when the user creates a Developer item, based on the two other fields.

For instance, John Doe would give john_doe and Jérémy Arçouille would give jeremy_arcouille.

So my questions are:

  1. Is there a general best practice for this (pretty common) case?
  2. Do I have to code something from my own or is it something pretty standard?
  3. If (2 == true) what would be the best place (services? utils?) to put that trunk of code?
  4. Should I rather call this portion of code from the controller or from the entity?

Note: because it would be quite a portion of code, and I'd like it to be reusable, I don't want to put into a prePersist lifecycle callback

I once developed a full solution for this problem and it worked well, but was really lame and spaghetti-esque, so I'd rather make something more in the proper Symfony2 way by now.

Thanks for any help.

  • 写回答

2条回答 默认 最新

  • duanao6704 2014-01-23 20:22
    关注

    There are at least two approaches you could try in this case.

    1. Create prePresists event listener. This solution is very easy to implement, works transparently from the application point of view and can be used with existing code.

      1. Define your listener service:

        use Doctrine\Common\EventSubscriber;
        use Doctrine\ORM\Events;
        use Doctrine\ORM\Event\LifecycleEventArgs;
        
        class MyEventListener implements EventSubscriber
        {
            public function __construct(/* ... */)
            {
                /* you can use other services here */
            }
        
            public function prePersist(LifecycleEventArgs $event)
            {
                $developer = $event->getEntity();
        
                if (!($developer instanceof Developer) || $developer->hasMachineName()) {
                    return;
                }
        
                ...
                $developer->setMachineName(...);
            }
        
            public function getSubscribedEvents()
            {
                return array(Events::prePersist);
            }
        }
        
      2. Declare it in Symofny's service container

        <service id="..." class="...">
            <!-- <argument ... /> -->
            <tag name="doctrine.event_subscriber" />
        </service>
        
      3. You're done.

    2. Create appropriate domain/bussines layer for your application. This requires more work, forces you to follow some general guidelines but makes your code much more flexible and easy to maintain. In a nutshell, instead of using Doctrine's ObjectManager directly inside your controllers create a whole new layer of servers dedicated to work with your domain objects. This layer sholud contain, among others, a service that's responsible for persisting developers with save(Developer $dev) method. This method should do exactly the same job as MyEventListener from first approach.

    PS. You shouldn't do any complex operations or refer to external classes from within your entities.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测