Propel - 使用多个外键加入相同的 table

Propel - Joining the same table using multiple foreign keys

我在使用 Propel2(版本 2.0.0-dev)时遇到问题。

我有三个外键指向同一个table:

<foreign-key foreignTable="sites" phpName="Site" refPhpName="Timesheet">
  <reference local="siteID" foreign="siteID"/>
</foreign-key>
<foreign-key foreignTable="sites" phpName="FromSite">
  <reference local="from_siteID" foreign="siteID"/>
</foreign-key>
<foreign-key foreignTable="sites" phpName="ToSite">
  <reference local="to_siteID" foreign="siteID"/>
</foreign-key>

尽管 PhpNames 不同,但 Base Class 生成的 default: $key 不正确:

if (null !== $this->aToSite) {
        switch ($keyType) {
            case TableMap::TYPE_CAMELNAME:
                $key = 'site';
                break;
            case TableMap::TYPE_FIELDNAME:
                $key = 'sites';
                break;
            default:
                $key = 'Site';
        }
        $result[$key] = $this->aToSite->toArray($keyType, $includeLazyLoadColumns,  $alreadyDumpedObjects, true);
    }

default: $key 应该是 ToSite,而不是 SiteFromSite 同样如此。结果,Site table 只加入了 Site,但没有加入 FromSiteToSite,这随后不允许我调用对象使用 FromSiteToSite.

table 的连接方式如下:

$timesheets = TimesheetQuery::create()
  ->select(Timesheet::getTransferFieldsWithRelations())
  ...
  ->joinWith("Timesheet.FromSite FromSite", Criteria::LEFT_JOIN)
  ->joinWith("Timesheet.ToSite ToSite", Criteria::LEFT_JOIN)
  ->joinWith("Timesheet.Site Site", Criteria::LEFT_JOIN)
  ...

有办法解决这个问题吗?

我们设法解决了上述问题。在 Propel/Generator/Builder/Om/ObjectBuilder.php 中,更改以下内容:

if ($includeForeignObjects) {";
        foreach ($fks as $fk) {
            $script .= "
        if (null !== $this->" . $this->getFKVarName($fk) . ") {
            {$this->addToArrayKeyLookUp($fk->getForeignTable(), false)}
            $result[$key] = $this->" . $this->getFKVarName($fk) . "->toArray($keyType, $includeLazyLoadColumns,  $alreadyDumpedObjects, true);
        }";
        }
        foreach ($referrers as $fk) {
            if ($fk->isLocalPrimaryKey()) {
                $script .= "
        if (null !== $this->" . $this->getPKRefFKVarName($fk) . ") {
            {$this->addToArrayKeyLookUp($fk->getTable(), false)}
            $result[$key] = $this->" . $this->getPKRefFKVarName($fk) . "->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true);
        }";
            } else {
                $script .= "
        if (null !== $this->" . $this->getRefFKCollVarName($fk) . ") {
            {$this->addToArrayKeyLookUp($fk->getTable(), true)}
            $result[$key] = $this->" . $this->getRefFKCollVarName($fk) . "->toArray(null, false, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
        }";
            }
        }
        $script .= "
    }";

if ($includeForeignObjects) {";
        foreach ($fks as $fk) {
            $script .= "
        if (null !== $this->" . $this->getFKVarName($fk) . ") {
            {$this->addToArrayKeyLookUp($fk->getPhpName(), $fk->getForeignTable(), false)}
            $result[$key] = $this->" . $this->getFKVarName($fk) . "->toArray($keyType, $includeLazyLoadColumns,  $alreadyDumpedObjects, true);
        }";
        }
        foreach ($referrers as $fk) {
            if ($fk->isLocalPrimaryKey()) {
                $script .= "
        if (null !== $this->" . $this->getPKRefFKVarName($fk) . ") {
            {$this->addToArrayKeyLookUp($fk->getRefPhpName(), $fk->getTable(), false)}
            $result[$key] = $this->" . $this->getPKRefFKVarName($fk) . "->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true);
        }";
            } else {
                $script .= "
        if (null !== $this->" . $this->getRefFKCollVarName($fk) . ") {
            {$this->addToArrayKeyLookUp($fk->getRefPhpName(), $fk->getTable(), true)}
            $result[$key] = $this->" . $this->getRefFKCollVarName($fk) . "->toArray(null, false, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
        }";
            }
        }
        $script .= "
    }";

还有这个:

protected function addToArrayKeyLookUp(Table $table, $plural)
{
    $phpName = $table->getPhpName();
    $camelCaseName = $table->getCamelCaseName();
    $fieldName = $table->getName();

至:

protected function addToArrayKeyLookUp($phpName, Table $table, $plural)
{
    if($phpName == "") {
        $phpName = $table->getPhpName();  
    }

    $camelCaseName = $table->getCamelCaseName();
    $fieldName = $table->getName();

现在我可以毫无问题地将多个带有 PHPName 的 FK 引用到同一个 table。

请注意,每次更新 Propel 时文件都会更改。