0

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

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;
}

ProductI18n保存 in ProductImageobjects 表时(保存 a 时) ,我需要访问对象的属性Product。问题是对象是在对象之后ProductI18n保存的。这意味着当它是新的时属性为空(因为在保存基于其他一些属性的对象时填充了该属性)。有什么办法可以改变 Propel 生成相关对象的保存顺序吗?有没有其他方法可以在不重写方法的情况下完成这项工作?ProductImageProductProductI18ndoSave

4

1 回答 1

0

虽然通过重新排序foreign-key架构文件中的条目可能有一个技巧来使其工作,但这可能是脆弱的(如果有人再次重新排序,您的代码会中断)。postInsert在类上使用钩子ProductImage并访问它的相关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();
    }
  }
  ...
}
于 2013-01-09T16:14:24.040 回答