2015-10-27 35 views
1

我有Customer,,BudgetBudgetItem实体。 Budget相关为@ORM \ ManyToOneCustomer@ORM \ OneToManyBudgetItemBudgetItem被关联为@ORM \ ManyToOneProductBudget学说PrePersist不为自己的实体工作

内部的BudgetItem有用于计算先前选择的,然后持续到budget_itens表的Products总价的方法。更新项目时,代码将被执行并再次成功保存到数据库中。方法如下:

public function calculateTotalPrice() 
{ 
    return $this->getProduct()->getPrice() * $this->getMeters(); 
} 

/** 
* @ORM\PrePersist 
*/ 
public function onPreEvents() 
{ 
    $this->setPrice($this->calculateTotalPrice()); 
} 

现在,我的Budget内:

/** 
* @return \DateTime 
*/ 
public function generateNextPaymentDate() 
{ 
    if ($this->getStartsAt() !== null) { 
     $date = new \DateTime($this->getStartsAt()->format('Y-m-d')); 
     return $date->add(new \DateInterval('P' . $this->getCheckFor() . 'D')); 
    } 
} 

/** 
* @return decimal 
*/ 
public function calculateTotalBudgetPrice() 
{ 
    $totalBudgetPrice = 0; 

    foreach ($this->getItems() as $item) { 
     $totalBudgetPrice += $item->getPrice(); 
    } 

    return $totalBudgetPrice; 
} 

/** 
* @return decimal 
*/ 
public function calculateInstallmentRatePrice() 
{ 
    return $this->calculateTotalBudgetPrice()/$this->getInstallmentRate(); 
} 

/** 
* @ORM\PrePersist 
*/ 
public function onPreEvents() 
{ 
    $this->setNextPaymentDate($this->generateNextPaymentDate()); 
    $this->setInstallmentRatePrice($this->calculateInstallmentRatePrice()); 
    $this->setTotalBudgetPrice($this->calculateTotalBudgetPrice()); 
} 

这些方法都没有他们的研究结果持续到数据库后编辑Budget。如果我理解得很好,似乎Doctrine2不更新其他相关实体,但这些字段属于此实体。我还有什么要做的?

编辑

BudgetController,我在那里坚持和冲洗。

<?php 

namespace CDGBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use CDGBundle\Entity\Budget; 
use CDGBundle\Form\BudgetType; 

class BudgetController extends Controller 
{ 
    /** 
    * Lista todos os orçamentos 
    */ 
    public function indexAction() 
    { 
     return $this->render('budget/index.html.twig', array(
      'budgets' => $this->getDoctrine()->getRepository('CDGBundle:Budget')->findAll(), 
      'title' => 'Lista de Orçamentos' 
     )); 
    } 

    /** 
    * Adicionar um novo orçamento 
    * 
    * @param Request $request 
    * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response 
    */ 
    public function addAction(Request $request) 
    { 
     $budget = new Budget(); 
     $form = $this->createForm(new BudgetType(), $budget); 
     $form->handleRequest($request); 

     if ($form->isValid()) { 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($budget); 
      $em->flush(); 

      return $this->redirectToRoute('budgets'); 
     } 

     return $this->render('budget/add.html.twig', array(
      'form' => $form->createView(), 
      'title' => 'Novo Orçamento' 
     )); 
    } 

    /** 
    * Exibe os detalhes do orçamento selecionado 
    * 
    * @param integer $id 
    * @return Budget 
    */ 
    public function showAction($id) 
    { 
     $budget = $this->getDoctrine()->getRepository('CDGBundle:Budget')->find($id); 

     return $this->render('budget/show.html.twig', array(
      'budget' => $budget, 
      'title' => 'Dados do orçamento #' . $budget->getId() 
     )); 
    } 

    /** 
    * Edita as informações do orçamento selecionado 
    * 
    * @param integer $id 
    * @param Request $request 
    * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response 
    */ 
    public function editAction($id, Request $request) 
    { 
     $budget = $this->getDoctrine()->getRepository('CDGBundle:Budget')->find($id); 
     $form = $this->createForm(new BudgetType(), $budget); 
     $form->handleRequest($request); 

     if ($form->isValid()) { 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($budget); 
      $em->flush(); 

      return $this->redirectToRoute('budgets'); 
     } 

     return $this->render('budget/edit.html.twig', array(
      'form' => $form->createView(), 
      'title' => 'Editar orçamento #' . $budget->getId() 
     )); 
    } 

    /** 
    * Deleção de um orçamento 
    * 
    * @param integer $id 
    * @return Budget 
    */ 
    public function deleteAction($id) 
    { 
     $budget = $this->getDoctrine()->getRepository('CDGBundle:Budget')->find($id); 
     $em = $this->getDoctrine()->getManager(); 
     $em->remove($budget); 
     $em->flush(); 

     return $this->redirectToRoute('budgets'); 
    } 
} 

Budget类:

<?php 

namespace CDGBundle\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 
use CDGBundle\Entity\Customer; 
use CDGBundle\Entity\BudgetItem; 

/** 
* Budget 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="CDGBundle\Entity\Repository\BudgetRepository") 
* @ORM\HasLifecycleCallbacks 
*/ 
class Budget 
{ 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var integer 
    * 
    * @ORM\ManyToOne(targetEntity="Customer", inversedBy="budgets") 
    */ 
    private $customer; 

    /** 
    * @var integer 
    * 
    * @ORM\OneToMany(targetEntity="BudgetItem", mappedBy="budget", cascade={"persist"}) 
    */ 
    private $items; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="address", type="string", length=255) 
    */ 
    private $address; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="installment_rate", type="integer") 
    */ 
    private $installmentRate; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="check_for", type="integer") 
    */ 
    private $checkFor; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="starts_at", type="datetime", nullable=true) 
    */ 
    private $startsAt; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="deadline", type="datetime", nullable=true) 
    */ 
    private $deadline; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="is_approved", type="string", length=1, nullable=true) 
    */ 
    private $isApproved; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="has_started", type="string", length=1, nullable=true) 
    */ 
    private $hasStarted; 

    /** 
    * @var decimal 
    * 
    * @ORM\Column(name="installment_rate_price", type="decimal", scale=2, nullable=true) 
    */ 
    private $installmentRatePrice; 

    /** 
    * @var decimal 
    * 
    * @ORM\Column(name="total_budget_price", type="decimal", scale=2, nullable=true) 
    */ 
    private $totalBudgetPrice; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="next_payment_date", type="datetime", nullable=true) 
    */ 
    private $nextPaymentDate; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="is_paid", type="string", length=1) 
    */ 
    private $isPaid; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="obs", type="text", nullable=true) 
    */ 
    private $obs; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="created_at", type="datetime") 
    */ 
    private $createdAt; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->items = new ArrayCollection(); 
     $this->createdAt = new \DateTime(); 
     $this->isPaid = 'n'; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set address 
    * 
    * @param string $address 
    * 
    * @return Budget 
    */ 
    public function setAddress($address) 
    { 
     $this->address = $address; 

     return $this; 
    } 

    /** 
    * Get address 
    * 
    * @return string 
    */ 
    public function getAddress() 
    { 
     return $this->address; 
    } 

    /** 
    * Set installmentRate 
    * 
    * @param integer $installmentRate 
    * 
    * @return Budget 
    */ 
    public function setInstallmentRate($installmentRate) 
    { 
     $this->installmentRate = $installmentRate; 

     return $this; 
    } 

    /** 
    * Get installmentRate 
    * 
    * @return integer 
    */ 
    public function getInstallmentRate() 
    { 
     return $this->installmentRate; 
    } 

    /** 
    * Set checkFor 
    * 
    * @param integer $checkFor 
    * 
    * @return Budget 
    */ 
    public function setCheckFor($checkFor) 
    { 
     $this->checkFor = $checkFor; 

     return $this; 
    } 

    /** 
    * Get checkFor 
    * 
    * @return integer 
    */ 
    public function getCheckFor() 
    { 
     return $this->checkFor; 
    } 

    /** 
    * Set startsAt 
    * 
    * @param \DateTime $startsAt 
    * 
    * @return Budget 
    */ 
    public function setStartsAt($startsAt) 
    { 
     $this->startsAt = $startsAt; 

     return $this; 
    } 

    /** 
    * Get startsAt 
    * 
    * @return \DateTime 
    */ 
    public function getStartsAt() 
    { 
     return $this->startsAt; 
    } 

    /** 
    * Set deadline 
    * 
    * @param \DateTime $deadline 
    * 
    * @return Budget 
    */ 
    public function setDeadline($deadline) 
    { 
     $this->deadline = $deadline; 

     return $this; 
    } 

    /** 
    * Get deadline 
    * 
    * @return \DateTime 
    */ 
    public function getDeadline() 
    { 
     return $this->deadline; 
    } 

    /** 
    * Set isApproved 
    * 
    * @param string $isApproved 
    * 
    * @return Budget 
    */ 
    public function setIsApproved($isApproved) 
    { 
     $this->isApproved = $isApproved; 

     return $this; 
    } 

    /** 
    * Get isApproved 
    * 
    * @return string 
    */ 
    public function getIsApproved() 
    { 
     return $this->isApproved; 
    } 

    /** 
    * Set hasStarted 
    * 
    * @param string $hasStarted 
    * 
    * @return Budget 
    */ 
    public function setHasStarted($hasStarted) 
    { 
     $this->hasStarted = $hasStarted; 

     return $this; 
    } 

    /** 
    * Get hasStarted 
    * 
    * @return string 
    */ 
    public function getHasStarted() 
    { 
     return $this->hasStarted; 
    } 

    /** 
    * Set installmentRatePrice 
    * 
    * @param string $installmentRatePrice 
    * 
    * @return Budget 
    */ 
    public function setInstallmentRatePrice($installmentRatePrice) 
    { 
     $this->installmentRatePrice = $installmentRatePrice; 

     return $this; 
    } 

    /** 
    * Get installmentRatePrice 
    * 
    * @return string 
    */ 
    public function getInstallmentRatePrice() 
    { 
     return $this->installmentRatePrice; 
    } 

    /** 
    * Set totalBudgetPrice 
    * 
    * @param string $totalBudgetPrice 
    * 
    * @return Budget 
    */ 
    public function setTotalBudgetPrice($totalBudgetPrice) 
    { 
     $this->totalBudgetPrice = $totalBudgetPrice; 

     return $this; 
    } 

    /** 
    * Get totalBudgetPrice 
    * 
    * @return string 
    */ 
    public function getTotalBudgetPrice() 
    { 
     return $this->totalBudgetPrice; 
    } 

    /** 
    * Set nextPaymentDate 
    * 
    * @param \DateTime $nextPaymentDate 
    * 
    * @return Budget 
    */ 
    public function setNextPaymentDate($nextPaymentDate) 
    { 
     $this->nextPaymentDate = $nextPaymentDate; 

     return $this; 
    } 

    /** 
    * Get nextPaymentDate 
    * 
    * @return \DateTime 
    */ 
    public function getNextPaymentDate() 
    { 
     return $this->nextPaymentDate; 
    } 

    /** 
    * Set isPaid 
    * 
    * @param string $isPaid 
    * 
    * @return Budget 
    */ 
    public function setIsPaid($isPaid) 
    { 
     $this->isPaid = $isPaid; 

     return $this; 
    } 

    /** 
    * Get isPaid 
    * 
    * @return string 
    */ 
    public function getIsPaid() 
    { 
     return $this->isPaid; 
    } 

    /** 
    * Set obs 
    * 
    * @param string $obs 
    * 
    * @return Budget 
    */ 
    public function setObs($obs) 
    { 
     $this->obs = $obs; 

     return $this; 
    } 

    /** 
    * Get obs 
    * 
    * @return string 
    */ 
    public function getObs() 
    { 
     return $this->obs; 
    } 

    /** 
    * Set createdAt 
    * 
    * @param \DateTime $createdAt 
    * 
    * @return Budget 
    */ 
    public function setCreatedAt($createdAt) 
    { 
     $this->createdAt = $createdAt; 

     return $this; 
    } 

    /** 
    * Get createdAt 
    * 
    * @return \DateTime 
    */ 
    public function getCreatedAt() 
    { 
     return $this->createdAt; 
    } 

    /** 
    * Set customer 
    * 
    * @param Customer $customer 
    * 
    * @return Budget 
    */ 
    public function setCustomer(Customer $customer = null) 
    { 
     $this->customer = $customer; 

     return $this; 
    } 

    /** 
    * Get customer 
    * 
    * @return Customer 
    */ 
    public function getCustomer() 
    { 
     return $this->customer; 
    } 

    /** 
    * Add item 
    * 
    * @param BudgetItem $item 
    * 
    * @return Budget 
    */ 
    public function addItem(BudgetItem $item) 
    { 
     $item->setBudget($this); 
     $this->items[] = $item; 

     return $this; 
    } 

    /** 
    * Remove item 
    * 
    * @param BudgetItem $item 
    */ 
    public function removeItem(BudgetItem $item) 
    { 
     $this->items->removeElement($item); 
    } 

    /** 
    * Get items 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getItems() 
    { 
     return $this->items; 
    } 

    /** 
    * @return \DateTime 
    */ 
    public function generateNextPaymentDate() 
    { 
     if ($this->getStartsAt() !== null) { 
      $date = new \DateTime($this->getStartsAt()->format('Y-m-d')); 
      return $date->add(new \DateInterval('P' . $this->getCheckFor() . 'D')); 
     } 
    } 

    /** 
    * @return decimal 
    */ 
    public function calculateTotalBudgetPrice() 
    { 
     $totalBudgetPrice = 0; 

     foreach ($this->getItems() as $item) { 
      $totalBudgetPrice += $item->getPrice(); 
     } 

     return $totalBudgetPrice; 
    } 

    /** 
    * @return decimal 
    */ 
    public function calculateInstallmentRatePrice() 
    { 
     return $this->calculateTotalBudgetPrice()/$this->getInstallmentRate(); 
    } 

    /** 
    * @ORM\PrePersist 
    * @ORM\PreUpdate 
    */ 
    public function onPreEvents() 
    { 
     $this->setNextPaymentDate($this->generateNextPaymentDate()); 
     $this->setInstallmentRatePrice($this->calculateInstallmentRatePrice()); 
     $this->setTotalBudgetPrice($this->calculateTotalBudgetPrice()); 
    } 
} 

EDIT2

我刚刚意识到@ORM \更新前的工作,但只为我nextPaymentDate。我在Budget一个collection,并且ProductController内有用于收集产品信息的方法:

<?php 

namespace CDGBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use CDGBundle\Entity\Product; 
use CDGBundle\Form\ProductType; 
use Symfony\Component\HttpFoundation\JsonResponse; 

class ProductController extends Controller 
{ 
    /** 
    * Lista todos os materials 
    */ 
    public function indexAction() 
    { 
     return $this->render('product/index.html.twig', array(
      'products' => $this->getDoctrine()->getRepository('CDGBundle:Product')->findAll(), 
      'title' => 'Lista de Materiais' 
     )); 
    } 

    /** 
    * Adicionar um novo material 
    * 
    * @param Request $request 
    * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response 
    */ 
    public function addAction(Request $request) 
    { 
     $product = new Product(); 
     $form = $this->createForm(new ProductType(), $product); 
     $form->handleRequest($request); 

     if ($form->isValid()) { 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($product); 
      $em->flush(); 

      return $this->redirectToRoute('products'); 
     } 

     return $this->render('product/add.html.twig', array(
      'form' => $form->createView(), 
      'title' => 'Novo Material' 
     )); 
    } 

    /** 
    * Exibe os detalhes do material selecionado 
    * 
    * @param integer $id 
    * @return Product 
    */ 
    public function showAction($id) 
    { 
     $product = $this->getDoctrine()->getRepository('CDGBundle:Product')->find($id); 

     return $this->render('product/show.html.twig', array(
      'product' => $product, 
      'title' => 'Dados do material #' . $product->getId() 
     )); 
    } 

    /** 
    * Edita as informações do material selecionado 
    * 
    * @param integer $id 
    * @param Request $request 
    * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response 
    */ 
    public function editAction($id, Request $request) 
    { 
     $product = $this->getDoctrine()->getRepository('CDGBundle:Product')->find($id); 
     $form = $this->createForm(new ProductType(), $product); 
     $form->handleRequest($request); 

     if ($form->isValid()) { 
      $em = $this->getDoctrine()->getManager(); 
      $em->merge($product); 
      $em->flush(); 

      return $this->redirectToRoute('products'); 
     } 

     return $this->render('product/edit.html.twig', array(
      'form' => $form->createView(), 
      'title' => 'Editar material #' . $product->getId() . ' - ' . $product->getName() 
     )); 
    } 

    /** 
    * Deleção de um material 
    * 
    * @param integer $id 
    * @return Product 
    */ 
    public function deleteAction($id) 
    { 
     $product = $this->getDoctrine()->getRepository('CDGBundle:Product')->find($id); 
     $em = $this->getDoctrine()->getManager(); 
     $em->remove($product); 
     $em->flush(); 

     return $this->redirectToRoute('products'); 
    } 

    /** 
    * Traz as informações do material selecionado na criação do orçamento. 
    * 
    * @param Request $request 
    * @return JsonResponse 
    */ 
    public function getProductInfoAction(Request $request) 
    { 
     $id = $request->query->get('id'); 
     $product = $this->getDoctrine()->getRepository('CDGBundle:Product')->find($id); 

     if ($request->query->get('format') == 'json') { 
      $data = array(
       'id' => $product->getId(), 
       'name' => $product->getName(), 
       'price' => $product->getPrice(), 
       'meters' => $product->getMeters(), 
       'description' => $product->getDescription(), 
       'created_at' => $product->getCreatedAt()->format('d-m-Y H:i'), 
      ); 

      return new JsonResponse($data, 200, array(
       'Content-type' => 'application/json' 
      )); 
     } 
    } 
} 

看来,编辑它的时候,因为我经历了一些错误,好像如果我改变了一些有问题点击编辑后的产品信息,就好像根本没有选定的产品。

回答

0

documentation,具体prePersist

应当指出的是,在最初的 坚持一个实体的该事件时才会触发(即它不会触发对未来更新)。

所以你还需要添加一个preUpdate监听器。

+0

我刚看到它。 'calculateTotalPrice'方法在更新后执行,因为它实际上是一个设置总值的JavaScript函数。该方法实际上是无用的。但是,仍然添加PreUpdate无效。我在PrePersist下添加了注释,或者我需要创建另一个方法? – mfgabriel92

+0

你在你的实体上设置了@ORM \ HasLifecycleCallbacks吗?你可以设置更新/坚持一个单一的方法就好了,这应该不成问题。 – Richard

+0

是的,因为我创建了它。 – mfgabriel92