向 Propel 中的多个基类添加功能

发布于 2024-11-30 22:24:46 字数 1449 浏览 3 评论 0原文

我有一组模型对等类,它们都应该具有相同的功能。我宁愿将函数添加到模型对等类扩展的类中,而不是每次向组中添加新函数时都进行复制和粘贴。不幸的是,由于模型对等类是从 BaseModelPeer 等基类扩展而来的,因此这是不可能的。

Propel 1.5 添加了 basePeer 属性,它可以允许您设置 < code>BaseModelPeer 从给定的类扩展。但是,默认情况下,BaseModelPeer 类不会从任何内容扩展。相反,它只是对 BasePeer 类进行所有调用,该类的函数具有不同的签名。通过更改 basePeer 属性,BaseModelPeer 从您的新类扩展,我们将其称为 NewBasePeer,并将调用更改为 BasePeer< /code> 到 NewBasePeer。然而,由于签名不同,这只会导致它崩溃!

我实际上正在尝试遵循 Symfony 的 Zend Lucene 教程,移动一些使项目可索引到此 NewBasePeer 类的函数。这些函数之一是doDeleteAll。该函数的 BaseModelPeer 签名如下所示:

public static function doDeleteAll($con = null)

但在该函数内部,它会进行如下调用:

$affectedRows += BasePeer::doDeleteAll(ModelPeer::TABLE_NAME, $con, ModelPeer::DATABASE_NAME);

由于 BaseModelPeer 不扩展 BasePeer,所以没问题。但是,通过更改架构中的 basePeer 属性,函数调用将更改为:

$affectedRows += NewBasePeer::doDeleteAll(ModelPeer::TABLE_NAME, $con, ModelPeer::DATABASE_NAME);

BaseModelPeer 现在扩展了 NewBasePeer,这意味着它会覆盖对doDeleteAll(),它们需要相同的签名。显然,BaseModelPeer 类现在是自相矛盾的!

这是 Propel 的错误吗?如果不是,如何使用 basePeer 属性?

I have a group of model peer classes that should all have the same functions. Rather than copying and pasting to each every time I add a new one to the group, I would rather add the functions to a class the model peer classes extend. Unfortunately, as the model peer classes extend from Base classes such as BaseModelPeer, it's not possible.

Propel 1.5 has added a basePeer attribute, which can allow you to set BaseModelPeer to extend from a given class. However, by default, a BaseModelPeer class does not extend from anything. Instead, it just makes all its calls to the BasePeer class, which has different signatures for its functions. By changing the basePeer attribute, the BaseModelPeer extends from your new class, let's call it NewBasePeer, and changes the calls to BasePeer to NewBasePeer. As the signatures differ, though, this just causes it to fall apart!

I am actually trying to follow on from Symfony's Zend Lucene tutorial, moving some of the functions which make items indexable to this NewBasePeer class. One of these functions is doDeleteAll. BaseModelPeer's signature of this function looks like this:

public static function doDeleteAll($con = null)

But inside this function it makes a call like this:

$affectedRows += BasePeer::doDeleteAll(ModelPeer::TABLE_NAME, $con, ModelPeer::DATABASE_NAME);

As BaseModelPeer doesn't extend BasePeer, it's fine. But by changing the basePeer attribute in the schema, the function call changes to:

$affectedRows += NewBasePeer::doDeleteAll(ModelPeer::TABLE_NAME, $con, ModelPeer::DATABASE_NAME);

BaseModelPeer now extends NewBasePeer, meaning for it to override the call to doDeleteAll(), they need identical signatures. Clearly the BaseModelPeer class is now contradicting itself!

Is this a bug with Propel? If not, how is one meant to use the basePeer attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2024-12-07 22:24:46

我宁愿将函数添加到模型对等类扩展的类中,而不是每次向组中添加新函数时都进行复制和粘贴。

正确的方法是定义一个 Propel Behaviors

Rather than copying and pasting to each every time I add a new one to the group, I would rather add the functions to a class the model peer classes extend.

The correct way is to define a Propel Behavior

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文