Php 更改相关对象的保存顺序
我有一个Php 更改相关对象的保存顺序,php,symfony-1.4,propel,Php,Symfony 1.4,Propel,我有一个product表,它以一对多关系链接到product\u图像表 . 在同一张桌子上,我也有一个i18n行为。这意味着另一个表,product_i18n具有相同类型的关系,一对多。我用的是丙氯普卢金(推进1.6)。默认情况下,它在我的BaseProduct.php文件中生成以下doSave方法 protected function doSave(PropelPDO $con) { $affectedRows = 0; // initialize var to track total
product
表,它以一对多关系链接到product\u图像
表
. 在同一张桌子上,我也有一个i18n行为。这意味着另一个表,product_i18n具有相同类型的关系,一对多。我用的是丙氯普卢金(推进1.6)。默认情况下,它在我的BaseProduct.php
文件中生成以下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
对象表中保存时(保存产品时),我需要访问ProductI18n
对象的属性。问题是ProductI18n
对象保存在ProductImage
对象之后。这意味着当产品
是新的时,该属性为空(因为该属性是在基于某些其他属性保存ProductI18n
对象时填充的)。是否有任何方法可以更改Prope生成相关对象保存顺序的方式?有没有其他方法可以在不重写doSave
方法的情况下实现这一点?虽然可以通过对架构文件中的外键
项重新排序来实现这一点,但这可能是脆弱的(如果有人再次对其重新排序,您的代码就会中断)。在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();
}
}
...
}
为什么需要访问ProductI18n对象?这实际上不在doSave的范围之内。你应该在打电话给doSave之前试着这样做。您能否追溯到几个步骤并尝试从此处操作ProductI18n对象?我需要ProductI18n对象的一个属性,该属性仅在保存时生成。基本上是一个slug,由对象的名称生成。问题在于,slug生成代码是通过BaseProductI18n save方法中的行为添加的。所以我不能改变它。在行为部分完成它的工作之前,我没有鼻涕虫。我试图在postSave方法中访问slug,但遇到了这个问题。为了弄清楚流程是这样的:Product->save->1)save ProductImage,2)save ProductI18n;在ProductImage->save中,我需要ProductI18n的slug(用于特定区域性),该slug仅在ProductI18n保存时生成。在保存期间调用ProductImage
上的postInsert
。那还是在ProductI18n
保存之前……嗯。。。是的,我知道可能是这样的。也许您需要将postInsert
钩子放在产品
对象本身上?或者,您可以在对其他对象调用save之前手动保存ProductI18n记录。