1

当另一个表上的字段被更新时,我试图在一个表的几行中更新相同的值。有人建议我做一个我做过的 PostUpdate Listener,问题是,我不知道谁来检索要更新的几行表的行以更改我想要的字段。有没有更简单的方法来做到这一点?具有要更新的几行的表 table 本身没有 ID 值。它有几个字段作为键值,并且这些值不是唯一的。

有几行要更新的表:

/**
 * @ORM\Entity 
 * @ORM\Table(name="catalog_has_prize")
 * @Gedmo\Loggable
 */
class CatalogHasPrize
{
  /**
   * @ORM\Id
   * @ORM\ManyToOne(targetEntity="Catalog", inversedBy="catalog_has_prize")
   * @ORM\JoinColumn(name="catalog", referencedColumnName="id")
   * @Gedmo\Versioned
   */
  private $catalog;

  /**
   * @ORM\Id
   * @ORM\ManyToOne(targetEntity="Prizes\PrizesBundle\Entity\Prize", inversedBy="catalog_has_prize")
   * @ORM\JoinColumn(name="prize", referencedColumnName="id")
   * @Gedmo\Versioned
   */
  private $prize;

  /**
   * @ORM\Column(type="float", nullable=false)
   * @Gedmo\Versioned
   */
  private $point_value;

  /**
   * @ORM\Column(type="float", nullable=false)
   * @Gedmo\Versioned
   */
  private $fullfillment_fee;

  /**
   * @ORM\Column(type="float", nullable=false)
   * @Gedmo\Versioned
   */
  private $margin;

  /**
   * @ORM\Column(type="integer", nullable=false)
   * @Gedmo\Versioned
   */
  private $points;

  /**
   * @ORM\Column(type="integer", nullable=false)
   * @Gedmo\Versioned
   */
  private $auto_calculated_points;

  /**
   * @ORM\Column(type="integer", nullable=false)
   * @Gedmo\Versioned
   */
  private $treshhold;

  /**
   * @ORM\ManyToOne(targetEntity="Prizes\PrizesBundle\Entity\Company", inversedBy="catalog_has_prize")
   * @ORM\JoinColumn(name="company", referencedColumnName="id")
   * @Gedmo\Versioned
   */
  private $company;

  /**
   * @ORM\ManyToOne(targetEntity="Optime\AppStatusBundle\Entity\Status")
   * @ORM\JoinColumn(name="status", referencedColumnName="id")
   * @Gedmo\Versioned
   */
  private $status;

  /**
   * @Gedmo\Timestampable(on="create")
   * @ORM\Column(type="datetime")
   */
  private $created;

  /**
   * @Gedmo\Timestampable(on="update")
   * @ORM\Column(type="datetime")
   */
  private $modified;

  public function __construct( )
  {
  }

  public function setPrize( $prize )
  {
    $this->prize = $prize;
  }

  public function getPrize( )
  {
    return $this->prize;
  }

  public function setCatalog( $catalog )
  {
    $this->catalog = $catalog;
  }

  public function getCatalog( )
  {
    return $this->catalog;
  }

  public function setCompany( $company )
  {
    $this->company = $company;
  }

  public function getCompany( )
  {
    return $this->company;
  }

  public function setStatus( $status )
  {
    $this->status = $status;
  }

  public function getStatus( )
  {
    return $this->status;
  }

  public function setPointValue( $point_value )
  {
    $this->point_value = $point_value;
  }

  public function getPointValue( )
  {
    return $this->point_value;
  }

  public function setFullfillmentFee( $fullfillment_fee )
  {
    $this->fullfillment_fee = $fullfillment_fee;
  }

  public function getFullfillmentFee( )
  {
    return $this->fullfillment_fee;
  }

  public function setMargin( $margin )
  {
    $this->margin = $margin;
  }

  public function getMargin( )
  {
    return $this->margin;
  }

  public function setPoints( $points )
  {
    $this->points = $points;
  }

  public function getPoints( )
  {
    return $this->points;
  }

  public function setAutoCalculatedPoints( $points )
  {
    $this->auto_calculated_points = $points;
  }

  public function getAutoCalculatedPoints( )
  {
    return $this->auto_calculated_points;
  }

  public function setTreshhold( $treshhold )
  {
    $this->treshhold = $treshhold;
  }

  public function getTreshhold( )
  {
    return $this->treshhold;
  }

  public function getCreated( )
  {
    return $this->created->format( 'd/m/Y' );
  }

  public function getModified( )
  {
    return $this->modified->format( 'd/m/Y' );
  }

保存更新值的表:

/**
 * @ORM\Entity @ORM\Table(name="prize")
 * @Gedmo\Loggable
 */
class Prize
{
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   * @Gedmo\Versioned
   */
  private $id;

  /**
   * @ORM\Column(type="string", length=170, nullable=false)
   * @Gedmo\Translatable
   * @Gedmo\Versioned
   */
  private $name;

  /**
   * @ORM\Column(type="string", length=400, nullable=false)
   * @Gedmo\Translatable
   * @Gedmo\Versioned
   */
  private $description;

  /**
   * @ORM\Column(type="string", length=255, nullable=false)
   * @Gedmo\Versioned
   */
  private $img;

  /**
   * @ORM\Column(type="string", length=255, nullable=false)
   */
  private $thumb;

  /**
   * @ORM\ManyToOne(targetEntity="Optime\AppStatusBundle\Entity\Status", cascade={""})
   * @ORM\JoinColumn(name="status", referencedColumnName="id", onDelete="RESTRICT", onUpdate="CASCADE")
   * @Gedmo\Versioned
   */
  private $status;

  /**
   * @Gedmo\Timestampable(on="create")
   * @ORM\Column(type="datetime")
   */
  private $created;

  /**
   * @Gedmo\Timestampable(on="update")
   * @ORM\Column(type="datetime")
   */
  private $modified;

  /**
   * @ORM\OneToMany(targetEntity="CompanyCountryPrize", mappedBy="prize")
   */
  private $company_country_prizes;

  /**
   * @ORM\ManyToMany(targetEntity="Category")
   * @ORM\JoinTable(name="prize_has_category",
   *      joinColumns={@ORM\JoinColumn(name="prize", referencedColumnName="id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="category", referencedColumnName="id")}
   *      )
   */
  private $categories;

  /**
   * @ORM\ManyToMany(targetEntity="Brand")
   * @ORM\JoinTable(name="prize_has_brand",
   *      joinColumns={@ORM\JoinColumn(name="prize", referencedColumnName="id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="brand", referencedColumnName="id")}
   *      )
   */
  private $brands;

  /**
   * @ORM\OneToMany(targetEntity="Prizes\CatalogBundle\Entity\CatalogHasPrize", mappedBy="catalog")
   */
  private $catalog_has_prize;

  /**
   * @ORM\OneToMany(targetEntity="PrizeMarketInfo", mappedBy="prize")
   */
  private $market_infos;

  /**
   * @ORM\OneToMany(targetEntity="PurchasePrize", mappedBy="prize")
   */
  private $purchase;

  /**
   * @Gedmo\Locale
   * Used locale to override Translation listener`s locale
   * this is not a mapped field of entity metadata, just a simple property
   */
  private $locale;

  public function __construct( )
  {
    $this->company_country_prizes = new \Doctrine\Common\Collections\ArrayCollection( );
    $this->categories = new \Doctrine\Common\Collections\ArrayCollection( );
    $this->brands = new \Doctrine\Common\Collections\ArrayCollection( );
    $this->catalog_has_prize = new \Doctrine\Common\Collections\ArrayCollection( );
    $this->market_infos = new \Doctrine\Common\Collections\ArrayCollection( );
  }

  public function getId( )
  {
    return $this->id;
  }

  public function setImg( $img )
  {
    if ( substr( $img, -4, 1 ) == "." )
      $this->img = $img;
  }

  public function getImg( )
  {
    return $this->img;
  }

  public function setName( $name )
  {
    $this->name = $name;
  }

  public function getName( )
  {
    return $this->name;
  }

  public function setDescription( $description )
  {
    $this->description = $description;
  }

  public function getDescription( )
  {
    return $this->description;
  }

  public function setThumb( $thumb )
  {
    if ( substr( $thumb, -4, 1 ) == "." )
      $this->thumb = $thumb;
  }

  public function getThumb( )
  {
    return $this->thumb;
  }

  public function setStatus( \Optime\AppStatusBundle\Entity\Status $status )
  {
    $this->status = $status;
  }

  public function getStatus( )
  {
    return $this->status;
  }

  public function getCreated( )
  {
    return $this->created;
  }

  public function getModified( )
  {
    return $this->modified;
  }

  public function setCompanyCountryPrize( \Prizes\PrizesBundle\Entity\CompanyCountryPrize $company_country_prizes )
  {
    $this->company_country_prizes[] = $company_country_prizes;
  }

  public function getCompanyCountryPrize( )
  {
    return $this->company_country_prizes;
  }

  public function setCategory( \Prizes\PrizesBundle\Entity\Category $categories )
  {
    $this->categories[] = $categories;
  }

  public function getCategory( )
  {
    return $this->categories;
  }

  public function setBrand( \Prizes\PrizesBundle\Entity\Brand $brands )
  {
    $this->brands[] = $brands;
  }

  public function getBrand( )
  {
    return $this->brands;
  }

  public function setCatalogHasPrize( \Prizes\CatalogBundle\Entity\CatalogHasPrize $catalog_has_prize )
  {
    $this->catalog_has_prize[] = $catalog_has_prize;
  }

  public function getCatalogHasPrize( )
  {
    return $this->catalog_has_prize;
  }

  public function setMarketInfo( \Prizes\PrizesBundle\Entity\PrizeMarketInfo $market_infos )
  {
    $this->market_infos[] = $market_infos;
  }

  public function getMarketInfo( )
  {
    return $this->market_infos;
  }

  public function getPurchase( )
  {
    return $this->purchase;
  }

我尝试做一个事件监听器:

class DeactivatePrizesInCatalog {

    public function postUpdate(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $entityManager = $args->getEntityManager();


        if ($entity instanceof Prize) {
            if ($entity->getStatus()->getName() == "Inactive"){
             $catalogs = $entityManager->getRepository('CatalogBundle:CatalogHasPrize')->findBy(array('prize' => $entity->getId(),'status' => 7));
            }

        }
    }
}

我的问题是,我不知道如何检索行,更不用说如何更新它们了。任何帮助将不胜感激。

4

2 回答 2

3

这就是我所做的,它奏效了

public function postUpdate(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $entityManager = $args->getEntityManager();


        if ($entity instanceof Prize) { 
            if ($entity->getStatus()->getName() == "Inactive"){
         $statusinactive = $entityManager->getRepository('AppStatusBundle:Status')->find(8);
         $q = $entityManager->createQuery("UPDATE CatalogBundle:CatalogHasPrize c SET c.status = :statusid WHERE c.status = 7 AND c.prize = :prizeid")
                 ->setParameters(array('statusid' => $statusinactive ,'prizeid' => $entity->getId()));
         $rs = $q->getResult();
        }else if ($entity->getStatus()->getName() == "Active"){
            $statusinactive = $entityManager->getRepository('AppStatusBundle:Status')->find(7);
            $q = $entityManager->createQuery("UPDATE CatalogBundle:CatalogHasPrize c SET c.status = :statusid WHERE c.status = 8 AND c.prize = :prizeid")
                 ->setParameters(array('statusid' => $statusinactive ,'prizeid' => $entity->getId()));
            $rs = $q->getResult();
        }
    }
于 2013-07-23T18:05:21.720 回答
1

您的 PostUpdate 函数应在实体类中使用注释 @ORM\PostUpdate() 进行声明。在班级奖中,添加:

/**
 * @ORM\PostUpdate()
 */
public function postUpdate(LifecycleEventArgs $args)
{
    // Your postupdate code here
}

不要忘记告诉 Doctrine 您的实体有带有注释 @ORM\HasLifecycleCallbacks 的生命周期回调:

/**
 * @ORM\Entity @ORM\Table(name="prize")
 * @Gedmo\Loggable
 * @ORM\HasLifecycleCallbacks
 */
class Prize {
    // etc

您在生命周期函数中使用的逻辑是可以的:由于实体是从 Doctrine 中获取的,因此您可以更改它,它将在数据库中更新。

但要小心,postUpdate 会出现问题:数据库更新已完成,因此您对 Catalog 实体所做的修改不会发送到数据库,直到您调用另一个 flush() 操作。在 postUpdate 函数中,可能会触发主键冲突异常。请参阅处理问题的这个问题,onFlush 或 postFlush 生命周期事件在这里可能会有所帮助。

于 2013-07-23T07:38:04.743 回答