【问题标题】:Symfony2 orm custom query issueSymfony2 orm 自定义查询问题
【发布时间】:2013-10-15 20:16:30
【问题描述】:

我遇到了一个 Doctrine ORM 查询错误,我确信它是一个非常简单的修复,但我还不了解 symfony2 中的所有内容:) 我进行了数小时的搜索,但我自己找不到解决方案。

Notice: Undefined index: id_chapter in C:\xampp\htdocs\vendor\doctrine\orm\lib
\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php on line 93

Notice: Undefined index: id_book in C:\xampp\htdocs\vendor\doctrine\orm\lib
\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php on line 93

Notice: Undefined index: id_testament in C:\xampp\htdocs\vendor\doctrine\orm\lib
\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php on line 93

Notice: Undefined index: id in C:\xampp\htdocs\vendor\doctrine\orm\lib\
Doctrine\ORM\UnitOfWork.php on line 2433

Notice: Undefined index: id_chapter in C:\xampp\htdocs\vendor\doctrine\orm\lib\
Doctrine\ORM\Internal\Hydration\ObjectHydrator.php on line 366

Notice: Undefined index: id_chapter in C:\xampp\htdocs\vendor\doctrine\orm\lib
\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php on line 367

Fatal error: Call to a member function getValue() on a non-object in C:\xampp\
htdocs\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator
.php on line 371

这些通知似乎是我的映射错误,这里是我的实体构建的一部分

/**
 * @ORM\ManyToOne(targetEntity="testament", inversedBy="books")
 * @ORM\JoinColumn(name="id_testament", referencedColumnName="id_testament")
 */
 protected $testament;

如果需要,请询问更多详细信息,任何帮助将不胜感激!谢谢。

编辑:添加这段代码可能很有用

$em = $this->getDoctrine()->getManager();
        $rsm = new ResultSetMappingBuilder($em);
        $rsm->addRootEntityFromClassMetadata('dieuenligne\websiteBundle\Entity\line', 'l');
        $rsm->addJoinedEntityFromClassMetadata('dieuenligne\websiteBundle\Entity\livreChapitre', 'c', 'l', 'id_chapter', array('id_chapter' => 'idc'));
        $rsm->addJoinedEntityFromClassMetadata('dieuenligne\websiteBundle\Entity\book', 'b', 'c', 'id_book', array('id_book' => 'idb'));
        $rsm->addJoinedEntityFromClassMetadata('dieuenligne\websiteBundle\Entity\testament', 't', 'b', 'id_testament', array('id_testament' => 'idt'));

        $sql = 'SELECT t.id_testament, b.book_number, c.chapter_wording, l.line_number, l.line_content FROM line l INNER JOIN chapter c ON l.id_chapter = c.id_chapter 
                    INNER JOIN book b ON c.id_book = b.id_book 
                    INNER JOIN testament t ON b.id_testament = t.id_testament 
                    WHERE MATCH (line_content) AGAINST (:request) LIMIT 0,75';

        $query = $em->createNativeQuery($sql, $rsm);
        $query->setParameter('request', $request);

        $lines = $query->getResult();

这是我的新代码,正在处理 id 冲突问题:

$em = $this->getDoctrine()->getManager();
        $rsm = new ResultSetMappingBuilder($em);
        $rsm->addRootEntityFromClassMetadata('dieuenligne\websiteBundle\Entity\line', 'l', array('id' => 'lid'));
        $rsm->addJoinedEntityFromClassMetadata('dieuenligne\websiteBundle\Entity\livreChapitre', 'c', 'l', 'id_chapter', array('id_chapter' => 'idc'));
        $rsm->addJoinedEntityFromClassMetadata('dieuenligne\websiteBundle\Entity\book', 'b', 'c', 'id_book', array('id_book' => 'idb'));
        $rsm->addJoinedEntityFromClassMetadata('dieuenligne\websiteBundle\Entity\testament', 't', 'b', 'id_testament', array('id_testament' => 'idt'));

$sql = 'SELECT t.id, b.book_number, c.chapter_wording, l.line_number, l.line_content FROM line l INNER JOIN chapter c ON l.id_chapter = c.id 
                    INNER JOIN book b ON c.id_book = b.id 
                    INNER JOIN testament t ON b.id_testament = t.id 
                    WHERE MATCH (line_content) AGAINST (:request) LIMIT 0,75';

这是我遗嘱实体的一部分:

class testament
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var integer
 *
 * @ORM\Column(name="id_bible", type="integer").
 */
private $idBible;

/**
 * @var string
 *
 * @ORM\Column(name="testament_wording", type="string", length=20)
 */
private $testamentWording;

/**
 * @ORM\ManyToOne(targetEntity="bible", inversedBy="testaments")
 * @ORM\JoinColumn(name="id_bible", referencedColumnName="id")
 */
protected $bible;

/**
 * @ORM\OneToMany(targetEntity="book", mappedBy="testament")
 * @ORM\OrderBy({"bookNumber" = "ASC"})
 */
protected $books;

public function __construct()
{
    $this->books = new ArrayCollection();
}

这是我的错误:

The column 'id' conflicts with another column in the mapper. 

at ResultSetMappingBuilder ->addAllClassFields ('dieuenligne\websiteBundle\Entity\book', 'b', array('id_book' => 'idb'))
in C:\xampp\htdocs\vendor\doctrine\orm\lib\Doctrine\ORM\Query\ResultSetMappingBuilder.php at line 71  +
at ResultSetMappingBuilder ->addJoinedEntityFromClassMetadata ('dieuenligne\websiteBundle\Entity\book', 'b', 'c', 'id_book', array('id_book' => 'idb'))
in C:\xampp\htdocs\src\dieuenligne\websiteBundle\Controller\WebsiteController.php at line 112  +

我正在努力,如果解决了我会更新。之前遇到过这个错误,我在addJoinedEntityFromClassMetadata里面加了array('id_chapter' => 'idc'),但是这里好像不是解决办法。

我没有在文档中找到解决方案。我尊重大多数约定,但我仍然有一个身份问题(似乎是这样)。

我应该显示我的代码的更大部分吗?

编辑

仍然坚持这个,这是ResultSetMappingBuilder.php at line 71中的代码

public function addJoinedEntityFromClassMetadata($class, $alias, $parentAlias, $relation, $renamedColumns = array())
{
    $this->addJoinedEntityResult($class, $alias, $parentAlias, $relation);
    $this->addAllClassFields($class, $alias, $renamedColumns);
}

第 71 行在 addAllClassFields 上。对我来说,类、别名和重命名的列都很好,因为我使用的是重命名的列属性 (array('id_book' => 'idb')) 我不应该有 id 冲突......

提前感谢您对此的任何建议,这是我在 symfony 上的第一个项目,同时也是网络开发的初学者,所以我真的需要帮助! :)

【问题讨论】:

    标签: symfony orm doctrine


    【解决方案1】:

    不确定,因为我没有看到您的代码的其他部分,但如果您的实体名为 Testament 和 referencedColumnName 是 id 比:

     /**
     * @ORM\ManyToOne(targetEntity="Testament", inversedBy="books")
     * @ORM\JoinColumn(name="id_testament", referencedColumnName="id")
     */
     protected $testament;
    

    【讨论】:

    • 我修改了我的数据库,所以我的表遗嘱没有“id_testament”,我现在有“id”。实体也已更改,因此所有引用的ColumnName 现在都是 id。我遇到 500 错误:“id”列与映射器中的另一列冲突。
    • @user2841678 为什么在一个实体中有 2 个id_bible
    • $idBible 是我的外键,我可能在这里弄错了?我跟着一个法语教程,他用另一个参数(这里是 $bible)加入表格。
    • @user2841678 尝试使用doctrine doc。并尝试使用一些代码约定(例如用大写首字母命名类)。比你阅读你的代码没有任何困难。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 2013-01-18
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2015-04-19
    相关资源
    最近更新 更多