Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 从数据库数据计算的实体属性_Symfony_Architecture_Entity - Fatal编程技术网

Symfony 从数据库数据计算的实体属性

Symfony 从数据库数据计算的实体属性,symfony,architecture,entity,Symfony,Architecture,Entity,这个问题可能更多地涉及到架构和应用程序设计,而不是开发本身。下面是: 我有一个用日期和会话(AM或PM)定义的实体,我需要一个get/set属性来知道isReservedAllDay()是否存在,这意味着该实体在同一天但在不同会话中保留了一个补充实体 <?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; class HallReservation { /** * @var integer

这个问题可能更多地涉及到架构和应用程序设计,而不是开发本身。下面是:

我有一个用日期和会话(AM或PM)定义的实体,我需要一个get/set属性来知道isReservedAllDay()是否存在,这意味着该实体在同一天但在不同会话中保留了一个补充实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

class HallReservation
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var \Date
     *
     * @ORM\Column(name="date", type="date")
     */
    private $date;

    /**
     * @var string: "AM" | "PM"
     *
     * @ORM\Column(name="session", type="string")
     */
    private $session;
你能批评这种方法还是建议一种更好的方法


谢谢您

例如,通过条令侦听器设置值。 只是把它复制/打印在一起,这样它可能无法完全工作,但你应该明白这一点。 也许这种方法可以满足你的需求

编辑:与ccKep提到的一样,entityManager已经可以通过
postLoad
功能中的
LifecycleEventArgs
直接使用。只有当您需要另一个依赖于entityManager的服务并且因此无法直接注入时,才需要添加自定义事件。
我将保持这里的代码不变,也许它对其他人有用(他们需要的不仅仅是entityManager)

听众:

class HallReservationLoadListener
{
    /** @var  EventDispatcherInterface */
    protected $eventDispatcher;

    public function __construct(EventDispatcherInterface $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }

    /**
     *
     * @param LifecycleEventArgs $args
     */
    public function postLoad(LifecycleEventArgs $args)
    {
        $item = $args->getEntity();
        if($item instanceof HallReservation) {
            $event = new HallReservationLoadedEvent($item);
            $this->eventDispatcher->dispatch(HallReservationLoadedEvent::NAME, $event);
        }

    }
}
自定义事件:

class HallReservationLoadedEvent extends Event
{
    const NAME = 'hallreservation.loaded';

    protected $hallreservation;

    public function __construct(HallReservation $hallreservation)
    {
        $this->hallreservation = $hallreservation;
    }

    public function getHallreservation()
    {
        return $this->hallreservation;
    }
}
class HallReservationReservedListener
{
    protected $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function setReservedProperty(HallReservationLoadedEvent $event)
    {
        $hallreservation = $event->getHallreservation();
        //do your stuff
    }
}
自定义事件的侦听器:

class HallReservationLoadedEvent extends Event
{
    const NAME = 'hallreservation.loaded';

    protected $hallreservation;

    public function __construct(HallReservation $hallreservation)
    {
        $this->hallreservation = $hallreservation;
    }

    public function getHallreservation()
    {
        return $this->hallreservation;
    }
}
class HallReservationReservedListener
{
    protected $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function setReservedProperty(HallReservationLoadedEvent $event)
    {
        $hallreservation = $event->getHallreservation();
        //do your stuff
    }
}
服务定义:

app.listener.hallreservationload:
        class: AppBundle\Listener\HallReservationLoadListener
        arguments: ['@event_dispatcher']
        tags:
            - { name: doctrine.event_listener, event: postLoad }
    app.listener.hallreservationload.reservedproperty:
        class: AppBundle\Listener\HallReservationReservedListener
        arguments: ['@doctrine.orm.entity_manager']
        tags:
            - { name: kernel.event_listener, event: 'hallreservation.loaded', method: 'setReservedProperty'}

您不想将其作为布尔值保存在数据库中有什么原因吗?对我来说,这似乎是一个布尔值。原因是:这是一个计算字段,每次我需要使用它时,我更喜欢“计算”,而不是每次我删除保留时,都需要查询和修改此字段(此方法不太容易出错)只是以一种更自动化的方式做你想做的事情:使用一个postLoad原则侦听器(将事件调度器注入其中),然后如果一个
HallReservation
被加载,则通过调度器分派一个自定义事件(将实体作为有效负载),另一个侦听器为该自定义事件执行逻辑并设置值。这样,您就不需要在控制器中手动调用它。需要自定义事件,因为您无法将实体管理器注入到条令事件侦听器中(循环依赖性问题)。这应该是解决问题的一种方法go@Joe我需要一些时间来消化,但我想这就是我想要的,thanks@K.Weber有几种可能的解决方案:您还可以使用函数(类似于
isReservedAllDay(保留))创建自定义细枝扩展
它对传递的实体进行计算,也可以使用细枝宏。将这些方法放到服务中可能也是一个好主意(类似于
ReservationService
——您还可以在其中收集其他有用的方法)。