1

like in symfony's query builder doesn't work and show me error!

this is my query:

    $apiToken = $this->createQueryBuilder('ud')
        ->select('ud.apiToken')
        ->where('ud.user LIKE :userPhone')
        ->setParameter('userPhone','%'.$phone)
        ->getQuery()
        ->getResult();

    return $apiToken;

and this is the error:

[Semantical Error] line 0, col 64 near 'user LIKE :u': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

cant find any extension stuff for like in query builder or useful description!

this is my UserDevice Entity:

class UserDevice implements UserInterface, \Serializable
{
    /**
     * @var int
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(name="notification_token",type="string", nullable=true)
     */
    private $notificationToken;

    /**
     * @ORM\Column(name="api_token" , type="string", unique=true, nullable=true)
     */
    private $apiToken;

    /**
     * @ORM\OneToOne(targetEntity="User", inversedBy="device", fetch="EAGER")
     * @ORM\JoinColumn(name="user_phone", referencedColumnName="phone")
     */
    private $user;

}
Imanali Mamadiev
  • 2,604
  • 2
  • 15
  • 23
sina
  • 111
  • 1
  • 9
  • 3
    Is ud.user really a phone number? I would think user would be an object with a phone property. In which case you would left join user then do your like clause on user.phone. Just speculating. – Cerad Oct 17 '18 at 12:49
  • @Cerad no its ok problem is (like expression) in qb – sina Oct 17 '18 at 12:51
  • @sina, Can You show your `Entity` pls? – Imanali Mamadiev Oct 17 '18 at 12:53
  • @sina Okay. Interesting that your error message does not match your posted code. – Cerad Oct 17 '18 at 12:55
  • is ud.user a string or an entity? I.e. as a wild guess with this limited information, you might be missing the user table join from your query (and the where clause should be accessing user.phonenumber field)... As asked above, including your entity code would clarify the situation. – ejuhjav Oct 18 '18 at 07:51
  • @ImanaliMamadiev i added entity to my post – sina Oct 20 '18 at 06:37

1 Answers1

1

You should use join query between table UserDevice and User.

$apiToken = $this->createQueryBuilder('ud')
        ->select('ud.apiToken')
        ->where('u.phone LIKE :userPhone') // here Users phone field
        ->innerJoin('ud.user', 'u')
        ->setParameter('userPhone','%'.$phone)
        ->getQuery()
        ->getResult();

return $apiToken;
Imanali Mamadiev
  • 2,604
  • 2
  • 15
  • 23