简体   繁体   中英

Doctrine QueryBuilder in Symfony2

I have a problem with Symfony and DQL. I got 2 tables.

TableA: id: int, status: string, referenceNumber: int tableB: []tableB

TableB: id:int, type: int, tableA_id: int

Tables are in one(tableA) to many(tableB) relation.

And I need to find every TableA where

TableA.status = Complete
AND (TableA.referenceNumber IS NULL OR COUNT(TableB.type = 22) = 0)

    $queryBuilder = $this->getEntityManager()
            ->createQueryBuilder()
            ->select('u')
            ->from('TestBundle:TableA', 'a');
    $queryBuilder->select('a','b');
    $queryBuilder->leftjoin('a.tableB', 'b');
    $queryBuilder->andWHERE('a.status = 'Complete')
    $query = $queryBuilder->getQuery();

I don't know how to write rest of query.

I think you just can do:

$query->where("a.status = 'Complete'")
      ->andWhere("a.referenceNumber IS NULL OR COUNT(b.type = 22) = 0");

If you use variable, use prepared query with:

$query->where("a.status = :status")
      ->setParameter("status", $status);

Hope it's what you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM