I am using doctrine2 with symfony2 and I am trying to perform a simple select query:
I want to run:
SELECT * FROM table WHERE status in (1, -1)
This PHP code:
$queryBuilder = $this->_em->createQueryBuilder();
$queryBuilder
->select('n')
->from('MyBundle:Table', 'n')
->where('n.status IN (1, -1)');
return $queryBuilder->getQuery()->getResult();
Gives the following exception:
[Syntax Error] line 0, col 96: Error: Expected Literal, got '-'
This is the attribute definition within the entity:
/**
* #var integer
*
* #ORM\Column(name="status", type="integer", nullable=true)
*/
private $status;
If I use only positive numbers within the in argument, it will work. The exception only happens with negative numbers.
What causes this exception?
Should do the trick :
$queryBuilder = $this->_em->createQueryBuilder();
$queryBuilder
->select('n')
->from('MyBundle:Table', 'n')
->where('n.status IN (:status)')
->setParameter('status', array(1, -1));
Related
I use the query builder of Symfony and I have this query:
->select('partial detail.{id}')
->from('Shopware\Models\Article\Detail', 'detail')
->innerJoin('detail.attribute', 'attr')
->leftJoin('detail.article', 'article')
Now I have a custom model outside of this scope with the following relation:
class Supplier {
/**
* #var \Shopware\Models\Article\Supplier
* #ORM\ManyToMany(targetEntity="\Shopware\Models\Article\Supplier")
* #ORM\JoinTable(name="fp_demand_planning_supplier_manufacturers", joinColumns={#ORM\JoinColumn(name="supplier_id", referencedColumnName="id")}, inverseJoinColumns={#ORM\JoinColumn(name="manufacturer_id", referencedColumnName="id", unique=true)})
*/
private $manufacturers;
On the article side the model \Shopware\Models\Article\Supplier is stored in article.supplier.
So now I want to join my custom model with the query above as follows:
->select('partial detail.{id}')
->from('Shopware\Models\Article\Detail', 'detail')
->innerJoin('detail.attribute', 'attr')
->leftJoin('detail.article', 'article')
->innerJoin('FpDemandPlanning\Models\Supplier', 'fps', 'WITH', 'article.supplier = fps.manufacturers');
Here I get this error:
206 near 'manufacturers': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.
I also tried it this way:
->innerJoin('FpDemandPlanning\Models\Supplier', 'fps', 'WITH', 'article.supplier IN (fps.manufacturers)');
But then I get this error:
[Syntax Error] line 0, col 216: Error: Expected Literal, got 'fpSupplier' in
Does anybody know how I can join a custom model that has a many to many relation here?
Thanks!
I wrote this query.
$query = $this->getEntityManager()->createQueryBuilder();
$query->select(['cart','subscriptions', 'member'])
->from('ComitiUserBundle:Cart', 'cart')
->leftJoin('cart.subscriptions', 'subscriptions')
->leftJoin('subscriptions.member', 'member')
->where('cart.club = :club_id')
->andWhere('subscriptions.clubSeason = :season_id')
->setParameter('club_id', $club)
->setParameter('season_id', $season);
if($section != null && !is_array($section)){
$query->andWhere('subscriptions.section = :section_id')
->setParameter('section_id', $section);
} elseif($section != null && is_array($section)){
$query->andWhere('subscriptions.section IN :section_ids')
->setParameter('section_ids', $section);
}
$query->orderBy('cart.transaction_date','DESC');
return $query->getQuery()->getResult();
I've got this error in return :
[Syntax Error] line 0, col 28: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got 'member'
In fact I try to hydrate my result collection with members in it. Member is manyToOne property in my Subscription entity. It is defined like this :
/**
* #ORM\ManyToOne(targetEntity="Comiti\UserBundle\Entity\UserComiti", inversedBy="subscriptions", cascade={"persist"})
* #ORM\JoinColumn(name="member_id", referencedColumnName="id")
*/
protected $member;
So I ran into the exact same error when trying to join a category group for a category entity. See the code below:
$qb = $this->entityManager->createQueryBuilder()
->select('c')
->from(Category::class, 'c');
if (isset($fields['categoryGroup'])) {
$qb = $qb->join('c.categoryGroup', 'group')
->addSelect('group');
}
$qb = $qb->getQuery()
->getArrayResult();
This resulted in the exact same error as the OP, only with 'group'. My guess is that you cannot use database reserved words in the query, like group and member.
For me, changing the alias to something like cg worked just fine.
I have this entity:
class TruckOrderPud {
/**
* #ORM\Id
* #ORM\OneToOne(targetEntity="Pud")
* #ORM\JoinColumn(name="pud_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $pudId;
/**
* #ORM\Id
* #ORM\OneToOne(targetEntity="TruckOrder")
* #ORM\JoinColumn(name="truck_order_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $truckOrderId;
/**
* #ORM\Column(name="package_count", type="integer", options={"unsigned"=true}, nullable=true)
*/
protected $packageCount;
/**
* #ORM\Column(name="package_weight", type="integer", options={"unsigned"=true}, nullable=true)
*/
protected $packageWeight;
}
I am needing get only single column pudId from this entity by truckOrderId parameter.
I am try:
$result = $this->_em->createQuery('SELECT top.pudId FROM AppTruckingBundle:TruckOrderPud top WHERE top.truckOrderId = ?1')
->setParameter(1, $truckOrderId)
->getSQL();`
Then I have error:
"[Semantical Error] line 0, col 11 near 'pudId FROM AppTruckingBundle:TruckOrderPud': Error: Invalid PathExpression. Must be a StateFieldPathExpression."
If i change query from 'SELECT top.pudId...' on 'SELECT top...' is it all OK.
I am also try:
$qb = $this->_em->createQueryBuilder();
$qb->select('top.pudId')
->from('AppTruckingBundle:TruckOrderPud', 'top')
->where($qb->expr()->eq('top.truckOrderId', $truckOrderId));`
In this case I am getting identical error. If i change $qb->select('top.pudId') on $qb->select('top') is it all OK.
Thanks for help!
PudId is a foreign key that refers to Pud, so you have to refer to Pud. Try this
$qb->select('IDENTITY(top.pud) PudId')
->from('AppTruckingBundle:TruckOrderPud', 'top')
->where($qb->expr()->eq('top.truckOrderId', $truckOrderId));`
Try to remove the 1 from the ?1 and leave only ?:
$result = $this->_em->createQuery('SELECT top.pudId FROM AppTruckingBundle:TruckOrderPud top WHERE top.truckOrderId = ?')
->setParameter(1, $truckOrderId)
->getSQL();
Doctrine does the parameter positioning by itself (first param on the first question mark spot). Read here for details.
I got an error [Syntax Error] line 0, col 81: Error: Expected Literal, got 'NULL' when I try to execute query via query builder
$qb = $this->createQueryBuilder('r')
->select('r')
->where('r.query = :query')
->setParameter('query', $query)
->andWhere('r.lang = NULL')
;
return $qb->getQuery()->getOneOrNullResult();
r.lang field is defined as:
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="\BW\LocalizationBundle\Entity\Lang")
* #ORM\JoinColumn(name="lang_id", referencedColumnName="id")
*/
private $lang;
Help to solve error, please
When you check for NULL value the expression should be
->andWhere('r.lang IS NULL')
I have query with two MtM relations:
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb
->select('o')
->from('UserBundle:User', 'o')
;
$qb->join('o.organisations', 'org')
->where('org.id = :organisation')
->setParameter('organisation', $filterData['organisation'])
;
$qb
->join('o.scientificDirections', 'd')
->where('d.id IN (:directionIds)')
->setParameter('directionIds', $directionIds)
->orderBy('o.surname')
;
return $qb->getQuery();
But it gives me error: Invalid parameter number: number of bound variables does not match number of tokens.
Can anybody explain me what is wrong?
Relation in User model:
/**
* #ORM\ManyToMany(targetEntity="\StrangeBundle\Entity\ScientificDirection")
*
*/
protected $scientificDirections;
/**
* #ORM\ManyToMany(targetEntity="\StrangeBundle\Entity\Organisation", mappedBy="workers")
*/
protected $organisations;
I think the reason is because you used where twice. That overwrites the first where, which is why it is giving you the parameters number error.
Use andWhere