Symfony在保存之前检查实体不变性



我有一个课程安排应用程序。

"一个人在被分配之前必须有本学期的申请"。

分配有很多学期周。学期周有一个学期。分配有一个人。申请有一个人。

我的"代码想法"是将其添加到分配实体中。它显然不起作用,因为在实体中使用实体管理器是错误的。

/**
 * Invariant is that a person must have a valid application for the specified semester.
 *
 * @ORMPrePersist()
 * @ORMPreUpdate()
 */
 public function validatePersonApplication() {
    $semester = $this->getSemesterWeek()->getSemester();
    $existing_application = $em->getRepository("CamsBundle:Application")->findOneBy(
  array(
        'person' => $this->person,
        'semester' => $semester
    )
  );
  if (!is_object($existing_application))  {
      throw new Exception("Can't allocate a session to a person without an application.");
   }
}

在保存之前如何测试此条件?

我遵循了瓦迪姆的建议并创建了一个服务。但请注意,这对于扩展域来说并不是很好,因为"symfony确保有一个实例",因此作为业务逻辑一部分的任何瞬态数据都不能真正进入服务。

我仍然需要进入内核才能让容器调用服务。我的更新后数据库事件侦听器。

运行调试器时,似乎只处理一个 PrePersist 事件。所有操作都由一个函数触发。

实体方法

/**
 * Automatically update the updated time.
 *
 * @ORMPrePersist()
 * @ORMPreUpdate()
 */
public function updateUpdated() {
  $this->setUpdated(new DateTime());
  // Validate class
  $this->_validateClassSession();
  global $kernel;
  if ('AppCache' == get_class($kernel)) {
    $kernel = $kernel->getKernel();
  }
  $allocationValidator = $kernel->getContainer()->get('allocation_invariants');
  $allocationValidator->validatePersonApplication($this->semesterWeek, $this->person);
}

在服务中设置.yml

allocation_invariants:
  class: InterlatedCamsBundleServicesAllocationInvariants
  arguments: ['@logger', '@doctrine.orm.entity_manager']

"服务"实现

namespace InterlatedCamsBundleServices;
use DoctrineORMEntityManager;
use Exception;
use SymfonyComponentHttpKernelLogLoggerInterface;
use InterlatedCamsBundleEntitySemesterWeek;
use InterlatedCamsBundleEntityPerson;
class AllocationInvariants {
public function __construct(LoggerInterface $logger, EntityManager $entityManger) {
  $this->logger = $logger;
  $this->entityManager = $entityManger;
}
 /**
 * Invariant is that a person must have a valid application for the specified semester.
 *
 */
 public function validatePersonApplication(SemesterWeek $semesterWeek, Person $person) {
   $semester = $semesterWeek->getSemester();
   $existing_application = $this->entityManager->getRepository("UnswCamsBundle:Application")->findOneBy(
   array(
    'person' => $person,
    'semester' => $semester
   )
 );
 if (!is_object($existing_application))  {
  throw new Exception("Can't allocate a session to a person without an application.");
 }
 }
}

此不变性仍在域类中,因为它不需要执行查询

/**
 * Invariant is that the semester week must be for a semester that this course is allocated to.
 *
 * It looks as though multiple PrePersist() annotations don't work. Only the first is being fired.
 */
protected function _validateClassSession() {
    $semester = $this->getSemesterWeek()->getSemester();
    $courseSemesters = $this->classSession->getTeachingClass()->getCourse()->getSemesters();
    foreach($courseSemesters as $courseSemester) {
      if ($semester == $courseSemester) {return;}
    }
    // Failed to match the course to the current semester.
    $course_semester_names = "";
    foreach($courseSemesters as $courseSemester) {
      $course_semester_names .= $courseSemester->getName() . " ";
    }
    throw new Exception("Trying to make an allocation to a course that is not running in the specified semester. SemesterWeekNumber "
      . $this->semesterWeek->getNumber() . " with semester " . $semester->getName() . " The course is running in the following semesters : " . $course_semester_names);
 }

相关内容

  • 没有找到相关文章

最新更新