我需要在我的某个实体中使用服务,但我不知道如何获取容器。我的属性$numHeure
和$numSem
是$dateDebut
的转换。
<?php
namespace Agnez\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* EdtHeure
*
* @ORM\Table(name="agnez_edt_heure")
* @ORM\Entity(repositoryClass="Agnez\CoreBundle\Repository\EdtHeureRepository")
*/
class EdtHeure
{
/**
*@var datetime
*@ORM\Column(type="datetime", name="dateDebut")
*/
private $dateDebut;
/**
*@var int
*@ORM\Column(type="int", name="numHeure")
*/
private $numHeure;
/**
*@var int
*@ORM\Column(type="int", name="numSem")
*/
private $numSem;
/**
* Set dateDebut
*
* @param \DateTime $dateDebut
*
* @return EdtHeure
*/
public function setDateDebut($dateDebut)
{
$this->dateDebut = $dateDebut;
$servicedate = $this->container->get('agnez_core.servicedate');
$this->numSem=$servicedate->numSem($date);
$this->numHeure=$servicedate->numHeure($date);
return $this;
}
}
我收到了错误:
注意:未定义的属性: Agnez \ CoreBundle \实体\ EdtHeure :: $容器
答案 0 :(得分:4)
我认为你的实体不需要服务,你应该避免使用它。
1)您可以使用学说事件 [documentation]
public function __construct(ServiceDate servicedate)
{
$this->servicedate = $servicedate
}
public function postUpdate(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if (!$entity instanceof EdtHeure) {
return;
}
$entityManager = $args->getEntityManager();
// Call your service here
}
2)另一种方法是在您的实体之外调用服务
public function setDateDebut($dateDebut, $numSem, $numHeure)
并在服务EdtHeureUpdater中调用它。它的责任是调用各种所需的服务并对您的实体进行更改。
public function __construct(ServiceDate servicedate)
{
$this->servicedate = $servicedate
}
public function updateHeure(EdtHeure $edt, \DateTime $date)
{
$numSem = $this->servicedate->numSem($date);
$numHeure = $this->servicedate->numHeure($date)
$edt->setDateDebut($dateDebut, $numSem, $numHeure)
}