zend-framework - 使用 Doctrine2/Zend Framework 1.11 以及包含下划线的列

标签 zend-framework frameworks doctrine-orm

我正在将网站重构为 Zend Framework 1.11/Doctrine 2,并且有许多旧表,其中包含带下划线的列名称(例如 plant_id)。 (最初很亲密,但我逐渐对教义印象深刻!)

我已经成功设置了一个 Doctrine 实体(遵循 WJ Gilmores 的优秀著作 Easy PHP with the Zend Framework),但是当我们使用 Doctrine 的 findOne magic finder 以及包含下划线的旧列名称时遇到问题

代码

$plant = $this->em->getRepository('Entities\Plant')
->findOneByPlant_id('3571');

返回错误

Message: Entity 'Entities\Plant' has no field 'plantId'. You can therefore not call 'findOneByPlant_id' on the entities' repository

(顺便说一句,Doctrine 看起来不错 - 我们已经创建了一个实体,以此作为列名,并且可以检索该列。)

我们通过使用查询生成器构建查询来临时解决了这个问题。

除了更改整个表格以删除下划线之外,是否还有其他更简单的解决方案不需要太多代码(这并不容易,因为我们必须重新编写大量遗留代码)?

最佳答案

您不使用魔法查找器中的列名称,而是使用实体属性。属性名称根本不需要与实际的列名称匹配。

尝试如下操作...

/**
 * @Entity
 */
class Plant
{
    /**
     * @Id
     * @Column(name="plant_id", type="integer")
     */
    private $id;

然后,您可以简单地使用

$plant = $plantRepository->find(3571);

如果您使用非主键列,则只需使用属性名称,例如

/**
 * @Column(name="some_col_with_underscores")
 */
private $someProperty;

并通过存储库

$repo->findOneBySomeProperty($val)

也可以使用数组方法

$repo->findOneBy(array('someProperty' => $val));

关于zend-framework - 使用 Doctrine2/Zend Framework 1.11 以及包含下划线的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9574694/

相关文章:

ios - 应用程序崩溃时框架代码会暴露 - iOS

doctrine-orm - Doctrine DBAL 查询生成器省略了一些连接

doctrine-orm - Symfony 4 Doctrine 2 自定义 NamingStrategy 如何

php - Zend 框架关系与表选择

php - Zend_Db_Table_Abstract 和 Zend_Db_Expr

php - 链接中的 ZF2 路由无法正常工作

mysql - Symfony2 查询构建器选择

php - 如何使用 dbSelect 将此 Mysql 查询实现到 Zend 框架 1

安卓开发框架

iphone - SystemConfiguration.framework 显示为红色