我有一个product
表,该表链接到一对多关系中的product_image
表
。在同一张桌子上,我也有一个i18n行为。这意味着另一个表,product_i18n具有相同类型的关系,一对多。我正在使用PropelORMPlugin(Propel 1.6)。默认情况下,它会在doSave
文件中生成以下BaseProduct.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
对象表时(保存ProductImage
时)访问Product
对象的属性。问题是ProductI18n
个对象在<{strong> ProductImage
个对象之后保存。意味着当Product
是新的时属性为空(因为在基于某些其他属性保存ProductI18n
对象时填充该属性)。有没有办法改变Propel如何生成保存相关对象的顺序?有没有其他方法可以在不重写doSave
方法的情况下完成这项工作?
答案 0 :(得分: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();
}
}
...
}