我遇到了一个 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 +
我正在努力,如果解决了我会更新。我之前遇到过这个错误,我在 中添加了 array('id_chapter' => 'idc')
,addJoinedEntityFromClassMetadata
但这似乎不是这里的解决方案。
我没有在文档中找到解决方案。我尊重大多数约定,但我仍然有一个身份问题(似乎是这样)。
我应该显示我的代码的更大部分吗?
编辑
仍然坚持这一点,这是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 上的第一个项目,同时也是网络开发的初学者,所以我真的需要帮助!:)