Propel - 使用多个外键加入同一个表

时间:2015-10-09 09:43:40

标签: php mysql orm schema propel

我遇到了Propel2(版本2.0.0-dev)的问题。

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

<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,但基类会错误地生成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,而不是Site。同样适用于FromSite。因此,Site表仅加入Site,但不加入FromSiteToSite,后者不允许我使用{调用对象{1}}和FromSite

表格按以下方式连接:

ToSite

有解决方法吗?

1 个答案:

答案 0 :(得分:0)

我们成功解决了上述问题。在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();

现在我可以将多个带有PHPNames的FK引用到同一个表中而没有任何问题。

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