Symfony2时间戳包括相关实体的更新


Symfony2 timestampable include update of related entity

我正在尝试使用Symfony2创建一个应用程序,并且我遇到了一些我无法弄清楚的东西。我有一个名为Company的实体和另一个名为Address的实体,它们具有多对多关系(应该是一对多,但我认为这是Symfony/Doctrine处理它的方式)。即每个公司可以有一个或多个地址。我已经建立了Gedmo的学说扩展,我在两个实体上都使用了时间戳。我已经建立了一个表单,更新两个链接的实体,但我真正想要的是能够时间戳公司,如果地址得到更新,因为它是两个的所有者。

有没有人知道如果时间戳可以做到这一点,或者我可能做错了什么?

你可以在这里看到我的Company类:

<?php
// src/Amber/AtsBundle/Entity/Company.php
namespace Amber'AtsBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Gedmo'Mapping'Annotation as Gedmo;
//...
use Doctrine'Common'Collections'ArrayCollection;
/**
* Company
*
* @ORM'Table()
* @ORM'Entity
*/
class Company
{
/**
 * @var integer
 *
 * @ORM'Column(name="id", type="integer")
 * @ORM'Id
 * @ORM'GeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @ORM'ManyToMany(targetEntity="Address", cascade={"persist"})
 * @ORM'JoinTable(name="company_addresses",
 *      joinColumns={@ORM'JoinColumn(name="company_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM'JoinColumn(name="address_id", referencedColumnName="id", unique=true)}
 *      )
 **/
private $addresses;
public function __construct()
{
    $this->addresses = new 'Doctrine'Common'Collections'ArrayCollection();
}
/**
 * @var datetime $created
 *
 * @Gedmo'Timestampable(on="create")
 * @ORM'Column(type="datetime")
 */
private $created;
/**
 * @var datetime $updated
 *
 * @Gedmo'Timestampable(on="update")
 * @ORM'Column(type="datetime")
 */
private $updated;

Address类,没什么可看的

<?php
// src/Amber/AtsBundle/Entity/Address.php
namespace Amber'AtsBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Gedmo'Mapping'Annotation as Gedmo;
/**
 * Address
 *
 * @ORM'Table()
 * @ORM'Entity
 */
class Address
{
/**
 * @var integer
 *
 * @ORM'Column(name="id", type="integer")
 * @ORM'Id
 * @ORM'GeneratedValue(strategy="AUTO")
 */
 private $id;
/**
 * @var datetime $created
 *
 * @Gedmo'Timestampable(on="create")
 * @ORM'Column(type="datetime")
 */
private $created;
/**
 * @var datetime $updated
 *
 * @Gedmo'Timestampable(on="update")
 * @ORM'Column(type="datetime")
 */
private $updated;

我认为你不需要看setter和getter但是告诉我

控制器

use Symfony'Bundle'FrameworkBundle'Controller'Controller;
class CompaniesController extends Controller
{
    public function editAction(Request $request, $companyID)
    {
    $em = $this->getDoctrine()->getManager();
    $company = $em->getRepository('AmberAtsBundle:Company')->find($companyID);
    $passExist = 'Yes';
   $form = $this->createForm(new CompanyType(), $company, array("pass_exists" => $passExist));
    if ($request->isMethod('POST')) {
        $form->bind($request);
        if ($form->isValid()) {
            $em->flush(); 
            return $this->redirect($this->generateUrl('amber_ats_companies_edit', array('companyID' => $company->getId())));
        }
    }    
}

我想时间戳不会检查关联实体上的更改。

解决方案是在Company和Address之间创建一个双向的一对多关系,Company是拥有方。

公司

/** 
 * @ORM'OneToMany(targetEntity="Address", inversedBy="company", cascade={"persist"})
 */
protected $addresses;
<<p> 地址/strong>
/**
 * @ORM'ManyToOne(targetEntity="Company", mappedBy="adresses")
 */
protected $company;
public function getCompany()
{
   return $this->company;
}
public function setCompany($company)
{
   $this->company = $company;
    return $this;
}
// now the the tricky part
public function setUpdated($updated)
{
    $this->updated = $updated;
    if  ( $updated > $this->company->getUpdated() )
    {
       $this->company->setUpdated($updated);
    }
    return $this;
}

如果多个公司可以有相同的地址,则可以使用多对多,并循环遍历所有相关公司,如:

public function setUpdated($updated)
{
    $this->updated = $updated;
    foreach ($this->companies as $company) {
       if  ( $updated > $this->company->getUpdated() )
       {
           $this->company->setUpdated($updated);
        }
    }
    return $this;
}

调整你的映射注释,但我猜你得到的想法…