gpt4 book ai didi

php - Propel - 使用多个外键连接同一个表

转载 作者:行者123 更新时间:2023-11-30 22:34:07 25 4
gpt4 key购买 nike

我在使用 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);
}

默认值:$key 应该是 ToSite,而不是 SiteFromSite 也类似。结果,Site 表只为 Site 加入,而不为 FromSiteToSite 加入,随后不允许我使用 FromSiteToSite 调用对象。

表的连接方式如下:

$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();

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

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

关于php - Propel - 使用多个外键连接同一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33034954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com