0

This is my function:

  public function findByIdJoinedToCategory($id)
     {
         $query = $this->getEntityManager()
             ->createQuery(
                 'SELECT a, b
                 FROM App\Entity\Products a
                 JOIN a.productgroup b
                 WHERE a.id = 2');
     }

I simply try to replace 2 with the variable $id. But I do not succeed:

  public function findByIdJoinedToCategory($id)
     {
         $query = $this->getEntityManager()
             ->createQuery(
                 'SELECT a, b
                 FROM App\Entity\Products a
                 JOIN a.productgroup b
                 WHERE a.id = $id');
     }

I get the error message:

[Syntax Error] line 0, col 125: Error: Expected Literal, got '$'

0

2 Answers 2

5

In short, when placing special characters such as a new line or vars in a string you need to use double quotes.

To place a var in a string you need to concatenate it into the string as follows:

'SELECT a, b
FROM App\Entity\Products a
JOIN a.productgroup b
WHERE a.id = '.$id

You are able to able to place the var inside of the string as follows:

"SELECT a, b
FROM App\Entity\Products a
JOIN a.productgroup b
WHERE a.id = $id"

However, you may find the following more readable:

"SELECT a, b
FROM App\Entity\Products a
JOIN a.productgroup b
WHERE a.id = {$id}"
Sign up to request clarification or add additional context in comments.

2 Comments

I think it is working. I get the error now Notice: Array to string conversion but I think this is another issue
Yes that means $id is an array.
1

it has to concat the string i think. try this :

public function findByIdJoinedToCategory($id)
     {
         $query = $this->getEntityManager()
             ->createQuery(
                 "SELECT a, b
                 FROM App\Entity\Products a
                 JOIN a.productgroup b
                 WHERE a.id ='$id'");
     }

2 Comments

try this, i hope it works
stackoverflow.com/questions/42188422/… try read this thread, hope it can solve

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.