更改相关对象的保存顺序



我有一个 product表,该表链接到一对多关系中的product_image表。在同一张桌子上,我也有I18N行为。这意味着另一个表,product_i18n具有相同类型的关系,一对多。我正在使用propelormplugin(Propel 1.6)。默认情况下,它在我的BaseProduct.php文件中生成了forowing doSave方法。

protected function doSave(PropelPDO $con)
{
    $affectedRows = 0; // initialize var to track total num of affected rows
    if (!$this->alreadyInSave) {
        $this->alreadyInSave = true;
        // We call the save method on the following object(s) if they
        // were passed to this object by their coresponding set
        // method.  This object relates to these object(s) by a
        // foreign key reference.
        if ($this->aCategory !== null) {
            if ($this->aCategory->isModified() || $this->aCategory->isNew()) {
                $affectedRows += $this->aCategory->save($con);
            }
            $this->setCategory($this->aCategory);
        }
        if ($this->isNew() || $this->isModified()) {
            // persist changes
            if ($this->isNew()) {
                $this->doInsert($con);
            } else {
                $this->doUpdate($con);
            }
            $affectedRows += 1;
            $this->resetModified();
        }
        if ($this->productImagesScheduledForDeletion !== null) {
            if (!$this->productImagesScheduledForDeletion->isEmpty()) {
                ProductImageQuery::create()
                    ->filterByPrimaryKeys($this->productImagesScheduledForDeletion->getPrimaryKeys(false))
                    ->delete($con);
                $this->productImagesScheduledForDeletion = null;
            }
        }
        if ($this->collProductImages !== null) {
            foreach ($this->collProductImages as $referrerFK) {
                if (!$referrerFK->isDeleted()) {
                    $affectedRows += $referrerFK->save($con);
                }
            }
        }
        if ($this->productI18nsScheduledForDeletion !== null) {
            if (!$this->productI18nsScheduledForDeletion->isEmpty()) {
                ProductI18nQuery::create()
                    ->filterByPrimaryKeys($this->productI18nsScheduledForDeletion->getPrimaryKeys(false))
                    ->delete($con);
                $this->productI18nsScheduledForDeletion = null;
            }
        }
        if ($this->collProductI18ns !== null) {
            foreach ($this->collProductI18ns as $referrerFK) {
                if (!$referrerFK->isDeleted()) {
                    $affectedRows += $referrerFK->save($con);
                }
            }
        }
        $this->alreadyInSave = false;
    }
    return $affectedRows;
}

保存在ProductImage对象表中(保存Product)时,我需要访问ProductI18n对象的属性。问题在于 ProductImage对象后,ProductI18n对象保存。这意味着当Product是新的时,该属性为空(因为根据某些其他属性保存ProductI18n对象时填充该属性)。有什么方法可以改变推进的方式生成相关对象的保存顺序?是否有其他方法可以在不重写doSave方法的情况下进行此工作?

虽然可以通过重新排序架构文件中的 foreign-key条目来使其工作有一个窍门,但这可能会很脆弱(如果有人再次重新定位它,则您的代码断开了)。在ProductImage类上使用postInsert挂钩并访问其相关的ProductI18n输入以获取SLUG(如果还没有),然后再次保存ProductImage

class ProductImage extends BaseProductImage {
  ...
  public function postInsert(PropelPDO $con = null) {
    if (!$this->getSlug()) {
      // get the slug from ProductI18n and update $this, then call ->save();
    }
  }
  ...
}

最新更新