Doctrine Project

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What’s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
Doctrine 2 - ORM
  • Doctrine 2 - ORM
  • DDC-1824

Native Query fail

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Blocker Blocker
  • Resolution: Invalid
  • Affects Version/s: 2.1.6
  • Fix Version/s: None
  • Component/s: DQL, ORM
  • Security Level: All
  • Labels:
    None
  • Environment:
    XAMPP on Windows 7
    Doctrine in Combination with Symfony

Description

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;
    }
}

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • History
  • Activity
  • Source
Hide
Permalink
Benjamin Eberlei added a comment - 17/May/12 9:39 PM

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

Show
Benjamin Eberlei added a comment - 17/May/12 9:39 PM You have to add the id as field aswell using $rsm->addFieldResult()
Hide
Permalink
Moritz added a comment - 17/May/12 9:44 PM

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();
Show
Moritz added a comment - 17/May/12 9:44 PM 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();
Hide
Permalink
Moritz added a comment - 20/May/12 10:53 PM

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

Show
Moritz added a comment - 20/May/12 10:53 PM It doesn't matter if I use the id as well. I keep getting the same error message.
Hide
Permalink
Benjamin Eberlei added a comment - 27/May/12 7:42 AM

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();
Show
Benjamin Eberlei added a comment - 27/May/12 7:42 AM 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();
Hide
Permalink
Moritz added a comment - 27/May/12 1:31 PM

Now it works perfectly fine. Thank you

Show
Moritz added a comment - 27/May/12 1:31 PM Now it works perfectly fine. Thank you

People

  • Assignee:
    Benjamin Eberlei
    Reporter:
    Moritz
Vote (0)
Watch (1)

Dates

  • Created:
    17/May/12 4:40 PM
    Updated:
    27/May/12 1:31 PM
    Resolved:
    27/May/12 7:42 AM
  • Atlassian JIRA (v5.2.7#850-sha1:b2af0c8)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Doctrine Project. Try JIRA - bug tracking software for your team.