原则2:错误:类“.. \ ..”没有名为“...”的字段或关联

时间:2012-05-22 22:49:02

标签: php orm doctrine-orm dql

在搜索时,我想出了许多人遇到类似问题的结果,但他们总是与关联错误有关。我正在尝试将一个简单的文本字段添加到数据库中的表中,对于我的生活,我无法弄清楚这一次有什么不同 - 当我以前没有遇到任何问题的时候。

我在4个不同的实体中添加了'record_checksum'字段,但我只使用一个,这里是为了简化示例。 (同样的错误发生在所有4)。

以下是我的Entity \ Cloud.php文件示例,底部添加了“ record_checksum ”字段:

use Doctrine\ORM\Mapping as ORM;

namespace Entity;

/**
 * Entity\Cloud
 *
 * @orm:Table(name="cloud")
 * @orm:Entity
 * @orm:HasLifecycleCallbacks
 */
class Cloud
{
    /**
     * @var integer $id
     *
     * @orm:Column(name="id", type="integer", length="13")
     * @orm:Id
     * @orm:GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var float $position_x
     *
     * @orm:Column(name="position_x", type="float", length=9)
     */
    private $position_x;

    /**
     * @var float $position_y
     *
     * @orm:Column(name="position_y", type="float", length=9)
     */
    private $position_y;

    /**
     * @var string $commit_group
     *
     * @orm:Column(name="commit_group", type="string", length=32, nullable=true)
     */
    private $commit_group;

    /**
     * @var string $commit_key
     *
     * @orm:Column(name="commit_key", type="string", length=13, nullable=true)
     */
    private $commit_key;

    /**
     * @var string $record_checksum
     *
     * @orm:Column(name="record_checksum", type="string", length=32, nullable=true)
     */
    private $record_checksum;

该类的其余部分是getter / setter方法,所以我将其遗漏。要查看整个Entity文件,我将其放在pastebin(http://pastebin.com/9LheZ6A1)上。几周前我刚刚添加了'commit_key',没有任何问题。

现在我更新架构:

$ doctrine orm:schema-tool:update --dump-sql
ALTER TABLE cloud ADD record_checksum VARCHAR(32) DEFAULT NULL;
$ doctrine orm:schema-tool:update --force
Updating database schema...
Database schema updated successfully!

我验证了此字段现在存在于DB表中。

但是,当我运行这样一个简单的DQL查询时:

$dql =  "SELECT c.id AS id, c.commit_key AS key, c.record_checksum AS checksum ".
                "FROM Entity\\Cloud c WHERE c.commit_group = :commit_group";

我收到错误:

 [Semantical Error] line 0, col 42 near 'record_checksum': Error: Class Entity\Cloud has no field or association named record_checksum, 

我已经把头撞在墙上一段时间了。我敢肯定我忽视了一些非常愚蠢的事情。任何帮助是极大的赞赏! -Nick

2 个答案:

答案 0 :(得分:4)

尝试:

  1. 清除任何可能包含配置或PHP代码的缓存。
  2. 重命名字段,以防第一个解决方案无效。

答案 1 :(得分:1)

转到相关的Entity并检查变量名称。如果它类似于TESTVar,并且您尝试在查询中使用testVar,那么它将为您提供Entity中不存在该变量的错误。

将查询更改为使用TESTVar而不是testVar,它会起作用。