Symfony2 orm自定义查询问题

时间:2013-10-03 15:21:07

标签: symfony orm doctrine

我坚持使用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;
如果需要,请询问更多详细信息,任何帮助都会更加苛刻! THX。

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

$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  +

我正在努力,如果解决了,我会更新。之前我遇到过这个错误,我在array('id_chapter' => 'idc')中添加了addJoinedEntityFromClassMetadata,但这似乎不是解决方案。

我在doc中找不到解决方案。我尊重大多数惯例,但我仍然有一个id问题(似乎是这样)。

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

修改

仍然坚持这一点,这是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上。对我来说,类,别名和renamedColumns都很好,因为我使用的是renamedColumns属性(array('id_book' => 'idb')),我不应该有id冲突......

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

1 个答案:

答案 0 :(得分:0)

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

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