Symfony2 Query Builder - symfony

I am trying to get records from a table where patentgroup_id is null. I tried this
$repository = $this->getDoctrine()->getRepository('MunichInnovationGroupPatentBundle:SvPatents');
$qb1 = $repository->createQueryBuilder('sv')
->select('sv')
->where('sv.patentgroup = :patentgroup')
->setParameter('patentgroup', null)
->getQuery();
$nogroup_patents = $qb1->getArrayResult();
var_dump($nogroup_patents);
There is one record in the table whose patentgroup_id is null but I am getting empty array.
Any ideas? What I am doing wrong
thanks

same way you would do it in SQL
->where('sv.patentgroup IS NULL')

Related

Select AVG of rows using Query Builder

I want to select the average of columns 'note' from a table
'noteparagraphe' where
the id_paragraphe is a parameter ($ref), I tried this query but I'm getting nothing back !
$query=$this->get('doctrine.orm.entity_manager')
->createQuery('Select avg(n.note) from ParagrapheBundle:NoteParagraphe n
WHERE n.id_paragraphe = :idModele');
$query->setParameter('idModele', $ref);
$query->execute();
$avgNote = $query->getResult();
the SQL is : SELECT AVG(note) from note_paragraphe WHERE id_paragraphe=?
Use Query builder:
$query = $this->getDoctrine()->getRepository('ParagrapheBundle:NoteParagraphe');
$avgNote = $query->createQueryBuilder('n')
->select('avg(n.note)')
->where('n.id_paragraphe = :idModele')
->setParameter('idModele', $ref);
->getQuery();
See if that works - I haven't tried it, but I think it should work.

Symfony doctrine using column value as array key when using joins

I've tried to find how to use column value as array key when getting results using queryBuilder. I found this question Using column value as array index in doctrine but unfortunately it doesn't work in my case as I'm using left join.
Currently I have this query:
$qb = $entityManager->createQueryBuilder('translation');
$qb->select('translation')
->from('MainBundle:PageTranslation', 'translation', 'translation.id')
->leftJoin('MainBundle:Page', 'page', 'WITH', 'IDENTITY(translation.page) = page.id')
->where('translation.locale = :locale')
->andWhere('translation.enabled = :enabled')
->andWhere('page.category = :category')
->setParameter('category', $category)
->setParameter('locale', $locale)
->setParameter('enabled', true);
$result = $qb->getQuery()->getResult();
Results I get has array keys starting with 0 and incrementing by one. I need array keys to be Page IDs (page.id) or IDENTITY of translation.page
If you look at the QueryBuilder API you'll see that the innerJoin() method has $indexBy as it's last argument. Pass 'translation.id' or ( the pk of translation instead of 'id') as the last argument, to get the desired result.

Doctrine query building select MAX

I would like to select everything + MAX value and receive only rows having max values.
$query = $this->createQueryBuilder('s');
$query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);
$query->groupBy('s.score');
$query->getQuery();
return $query->select('s.*, MAX(s.score) AS max_score')->getQuery()->getResult();
How could I achieve this in doctrine? I am getting an error that * property is not found. I have tried to select them all one by one but no luck either.
Goal is to achieve something like this
SELECT user, challenge, whateverelse, MAX(score) FROM users_scores_table GROUP BY user_id
Please help ;)
It's too late, but I write this for the records.
You can use "as HIDDEN" in SELECT statements to remove a field of the final result, this way you can use it for ordering or grouping without modifying result fields.
In your example:
$query = $this->createQueryBuilder('s');
$query->select('s, MAX(s.score) AS HIDDEN max_score');
$query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);
$query->groupBy('s.user');
$query->setMaxResults($limit);
$query->orderBy('max_score', 'DESC');
Here is a final working query
$query = $this->createQueryBuilder('s');
$query->select('s, MAX(s.score) AS max_score');
$query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);
$query->groupBy('s.user');
$query->setMaxResults($limit);
$query->orderBy('max_score', 'DESC');
return $query->getQuery()->getResult();

Propel multiple-parameter binding in where() clause

I'm trying to run this query on Propel 1.6 with symfony 1.4.20.
I want to bind 2 parameters onto this subquery but its not working.
$paginas = PaginaQuery::create()
->where("pagina.id not in (select id from cliente_artista where cliente_artista.cliente_id = ? and cliente_artista.culture = ?)"
,array('XXX', 'en')
)
->limit(5)
->find();
This gives me the error:
Cannot determine the column to bind to the parameter in clause
I also found this post but there is no answer (https://groups.google.com/forum/?fromgroups=#!topic/propel-users/2Ge8EsTgoBg)
Instead of using placeholders. You may use $id and $culture:
//first, get an array of the id's
//define your vars
$id = $your_id_param;
$culture = 'en';
$cliente_artistas = ClienteArtistaQuery::create()
->select('id')
->distinct()
->filterByClienteId($id)
->filterByCulture($culture)
->find();
$paginas = PaginaQuery::create()
->where("pagina.id NOT IN ?", $cliente_artistas)
->limit(5)
->find();
If this has to be done in one query, recommend using raw sql and binding the parameters into the PDO statement (but then you lose the convenience of PropelObjectCollections):
public function getResultSet($id, $culture) {
$id = $id_param;
$culture = $culture_param;
$sql = <<<ENDSQL
SELECT * from pagina
WHERE id NOT IN (SELECT distinct id
FROM cliente_artista
WHERE cliente_id = ?
AND culture = ?
)
LIMIT 5
ENDSQL;
$connection = Propel::getConnection();
$statement = $connection->prepare($sql);
$statement->bindValue(1, $id);
$statement->bindValue(2, $culture);
$statement->execute();
$resultset = $statement->fetchAll(PDO::FETCH_ASSOC); // or whatever you need
if (! count($resultset) >= 1) {
// Handle empty resultset
}
return $resultset;
}
You could also write some query methods to use propel orm query methods. Ofcourse, the propel api is beneficial reference. There are several ways to do this. I have indicated one method here which should work for you.
EDIT:
Here's an idea on doing this as one query [since useSelectQuery() requires 'relation' name], this idea assumes tables are not related but that id's are:
$paginas = PaginaQuery::create()
->addJoin(PaginaPeer::ID, ClienteArtistaPeer::CLIENTE_ID, Criteria::LEFT_JOIN)
->where("ClienteArtista.Id <> ?", $id)
->where("ClienteArtista.Culture <> ?", $culture)
->select(array('your','pagina','column','array'))
->limit(5)
->find();

Query in symfony not working

I have a query in the model of my table:
$q = $this->createQuery('a')
->where('a.img1 = null')
->orderBy('a.created_at DESC')
->limit(4);
The thing is that it returns nothing, but in DB there is an entry with no image (img1 field is null). What am I doing wrong? thank you
$q = $this->createQuery('a')
->where('a.img1 IS NULL') // It needs to be IS NULL instead of = null
->orderBy('a.created_at DESC')
->limit(4);
Don't forget ->execute()!

Resources