DDC-1824: Native Query fail #2298

Closed
opened 2026-01-22 13:47:40 +01:00 by admin · 6 comments
Owner

Originally created by @doctrinebot on GitHub (May 17, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user mowee:

I keep getting an error while I'm trying to perfom an native query.
If there is no result then everything is fine and I don't get any errors.

Notice: Undefined index: id in C:\xampp\htdocs\Symfony\vendor\doctrine\lib\Doctrine\ORM\UnitOfWork.php line 1949

$sql = "SELECT q.question FROM question q WHERE MATCH (q.question) AGAINST (?)";

$rsm = new \Doctrine\ORM\Query\ResultSetMapping;
$rsm->addEntityResult('WWMGameBundle:Question', 'q');
$rsm->addFieldResult('q', 'question', 'question');

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createNativeQuery($sql, $rsm);
$query->setParameter(1, $request->request->get('q'));

$search = $query->getResult();
/****
* WWM\Entity\Question
*
* @ORM\Entity(repositoryClass="WWM\Entity\QuestionsRepository")
* @ORM\Table(name="question")
*/
class Question
{
    /****
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /****
     * @var integer $level
     *
     * @ORM\Column(name="level", type="integer", nullable=false)
     */
    private $level;

    /****
     * @var string $question
     *
     * @ORM\Column(name="question", type="string", length=255, nullable=false)
     */
    private $question;



    /****
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /****
     * Set level
     *
     * @param integer $level
     */
    public function setLevel($level)
    {
        $this->level = $level;
    }

    /****
     * Get level
     *
     * @return integer
     */
    public function getLevel()
    {
        return $this->level;
    }

    /****
     * Set question
     *
     * @param string $question
     */
    public function setQuestion($question)
    {
        $this->question = $question;
    }

    /****
     * Get question
     *
     * @return string
     */
    public function getQuestion()
    {
        return $this->question;
    }
}
Originally created by @doctrinebot on GitHub (May 17, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user mowee: I keep getting an error while I'm trying to perfom an native query. If there is no result then everything is fine and I don't get any errors. **Notice: Undefined index: id in C:\xampp\htdocs\Symfony\vendor\doctrine\lib\Doctrine\ORM\UnitOfWork.php line 1949** ``` none $sql = "SELECT q.question FROM question q WHERE MATCH (q.question) AGAINST (?)"; $rsm = new \Doctrine\ORM\Query\ResultSetMapping; $rsm->addEntityResult('WWMGameBundle:Question', 'q'); $rsm->addFieldResult('q', 'question', 'question'); $em = $this->getDoctrine()->getEntityManager(); $query = $em->createNativeQuery($sql, $rsm); $query->setParameter(1, $request->request->get('q')); $search = $query->getResult(); ``` ``` none /**** * WWM\Entity\Question * * @ORM\Entity(repositoryClass="WWM\Entity\QuestionsRepository") * @ORM\Table(name="question") */ class Question { /**** * @var integer $id * * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /**** * @var integer $level * * @ORM\Column(name="level", type="integer", nullable=false) */ private $level; /**** * @var string $question * * @ORM\Column(name="question", type="string", length=255, nullable=false) */ private $question; /**** * Get id * * @return integer */ public function getId() { return $this->id; } /**** * Set level * * @param integer $level */ public function setLevel($level) { $this->level = $level; } /**** * Get level * * @return integer */ public function getLevel() { return $this->level; } /**** * Set question * * @param string $question */ public function setQuestion($question) { $this->question = $question; } /**** * Get question * * @return string */ public function getQuestion() { return $this->question; } } ```
admin added the Bug label 2026-01-22 13:47:40 +01:00
admin closed this issue 2026-01-22 13:47:41 +01:00
Author
Owner

@doctrinebot commented on GitHub (May 17, 2012):

Comment created by @beberlei:

You have to add the id as field aswell using $rsm->addFieldResult()

@doctrinebot commented on GitHub (May 17, 2012): Comment created by @beberlei: You have to add the id as field aswell using $rsm->addFieldResult()
Author
Owner

@doctrinebot commented on GitHub (May 17, 2012):

Comment created by mowee:

It doesn't matter if I use the id as well. I keep getting the same error message:

$sql = "SELECT q.question FROM question q WHERE MATCH (q.question) AGAINST (?)";

$rsm = new \Doctrine\ORM\Query\ResultSetMapping;
$rsm->addEntityResult('WWMGameBundle:Question', 'q');
$rsm->addFieldResult('q', 'question', 'question');
$rsm->addFieldResult('q', 'id', 'id');

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createNativeQuery($sql, $rsm);
$query->setParameter(1, $request->request->get('q'));

$search = $query->getResult();
@doctrinebot commented on GitHub (May 17, 2012): Comment created by mowee: It doesn't matter if I use the id as well. I keep getting the same error message: ``` $sql = "SELECT q.question FROM question q WHERE MATCH (q.question) AGAINST (?)"; $rsm = new \Doctrine\ORM\Query\ResultSetMapping; $rsm->addEntityResult('WWMGameBundle:Question', 'q'); $rsm->addFieldResult('q', 'question', 'question'); $rsm->addFieldResult('q', 'id', 'id'); $em = $this->getDoctrine()->getEntityManager(); $query = $em->createNativeQuery($sql, $rsm); $query->setParameter(1, $request->request->get('q')); $search = $query->getResult(); ```
Author
Owner

@doctrinebot commented on GitHub (May 20, 2012):

Comment created by mowee:

It doesn't matter if I use the id as well. I keep getting the same error message.

@doctrinebot commented on GitHub (May 20, 2012): Comment created by mowee: It doesn't matter if I use the id as well. I keep getting the same error message.
Author
Owner

@doctrinebot commented on GitHub (May 27, 2012):

Comment created by @beberlei:

Do you want to fetch the whole Question Entity? Or just the question as string? Since you only do q.question i suppose using a scalar result instead of the entity works.

$sql = "SELECT q.question FROM question q WHERE MATCH (q.question) AGAINST (?)";

$rsm = new \Doctrine\ORM\Query\ResultSetMapping;
$rsm->addScalarResult('question', 'question');

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createNativeQuery($sql, $rsm);
$query->setParameter(1, $request->request->get('q'));

$search = $query->getResult();

However if you want the whole entity, then you have to fetch the ID from sql as well:

$sql = "SELECT q.id, q.question FROM question q WHERE MATCH (q.question) AGAINST (?)";

$rsm = new \Doctrine\ORM\Query\ResultSetMapping;
$rsm->addEntityResult('WWMGameBundle:Question', 'q');
$rsm->addFieldResult('q', 'question', 'question');
$rsm->addFieldResult('q', 'id', 'id');

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createNativeQuery($sql, $rsm);
$query->setParameter(1, $request->request->get('q'));

$search = $query->getResult();
@doctrinebot commented on GitHub (May 27, 2012): Comment created by @beberlei: Do you want to fetch the whole Question Entity? Or just the question as string? Since you only do q.question i suppose using a scalar result instead of the entity works. ``` $sql = "SELECT q.question FROM question q WHERE MATCH (q.question) AGAINST (?)"; $rsm = new \Doctrine\ORM\Query\ResultSetMapping; $rsm->addScalarResult('question', 'question'); $em = $this->getDoctrine()->getEntityManager(); $query = $em->createNativeQuery($sql, $rsm); $query->setParameter(1, $request->request->get('q')); $search = $query->getResult(); ``` However if you want the whole entity, then you have to fetch the ID from sql as well: ``` $sql = "SELECT q.id, q.question FROM question q WHERE MATCH (q.question) AGAINST (?)"; $rsm = new \Doctrine\ORM\Query\ResultSetMapping; $rsm->addEntityResult('WWMGameBundle:Question', 'q'); $rsm->addFieldResult('q', 'question', 'question'); $rsm->addFieldResult('q', 'id', 'id'); $em = $this->getDoctrine()->getEntityManager(); $query = $em->createNativeQuery($sql, $rsm); $query->setParameter(1, $request->request->get('q')); $search = $query->getResult(); ```
Author
Owner

@doctrinebot commented on GitHub (May 27, 2012):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (May 27, 2012): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (May 27, 2012):

Comment created by mowee:

Now it works perfectly fine. Thank you :)

@doctrinebot commented on GitHub (May 27, 2012): Comment created by mowee: Now it works perfectly fine. Thank you :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2298