symfony2 : Sql Syntax Error - php

I want to display the list of papers with more That a reporter , but I think I have a problem in my sql query syntax .. here is the whole function in my controller :
`public function nonattribuéAction()
{
$em=$this->getDoctrine()->getManager();
$repository = $em->getRepository('administrationadministrationBundle:Papier');
$repository = $em->getRepository('administrationadministrationBundle:CorrectionPap');
$query = $em->createQuery('SELECT * FROM administaration\administrationBundle\Entity\Papier WHERE pNum IN (SELECT DISTINCT pNum FROM administaration\administrationBundle\Entity\CorrectionPapier GROUP BY pNum HAVING COUNT(rNum)=>0)');
$papier = $query->getResult();
return $this->render('administrationadministrationBundle:Default:papier.html.twig', array('papier' => $papier, 'name' => 'Papiers Non Attribués'));
}`

This is the answer :D
public function nonattribuéAction()
{
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery('SELECT u FROM administrationadministrationBundle:Papier u WHERE u.pNum IN (SELECT DISTINCT v.pNum FROM administrationadministrationBundle:CorrectionPap v GROUP BY v.pNum HAVING COUNT(v.rNum)= 2)');
$results = $query->getResult();
return $this->render('administrationadministrationBundle:Default:papier.html.twig', array('papier' => $results, 'name' => 'Papiers Non Attribués'));
}

Related

How to join multiple tables with aliases

I have 3 tables in database:
teams
id
name
matches
id (int)
team_home_id
team_away_id
goals
id
match_id
team_id
time
I need display names of teams in view where I get goals in controller.
I know that I should do join tables.
I have this code:
public function get_goals() {
$this->db->select('goals.*');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$q = $this->db->get();
return $q->result();
}
and I don't know what next.
I need in view display names by:
$goals->team_home_name and $goals->team_away_name
To get $goals->team_home_name and $goals->team_away_name result, use aliases like this :
public function get_goals() {
$this->db->select('goals.*, home_team.name team_home_name, away_team.name team_away_name');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$this->db->join('teams home_team', 'home_team.id = matches.team_home_id');
$this->db->join('teams away_team', 'away_team.id = matches.team_away_id');
$q = $this->db->get();
return $q->result();
}
You can try with this:-
public function get_goals() {
$this->db->select('g.* , t.name as team_home_name , t.name as team_away_name);
$this->db->from('goals as g');
$this->db->join('matches as m', 'm.id = g.match_id');
$this->db->join('teams as t', 't.id = g.team_id');
$q = $this->db->get();
return $q->result();
}
U can use an alias by
guss the team_away_name and team_home_name from teams table
public function get_goals() {
$this->db->select('goals.*, team.team_home_name, team.team_away_name');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$this->db->join('teams as team', 'team.id = goals.team_id');
$q = $this->db->get();
return $q->result();
}

How can i resolve this PDOException for this update query? [duplicate]

Using Doctrine 2 I want to get some users that are contacts of another user. The table user contains the mapping between those users. The query in the function will return the following error:
Invalid parameter number: number of bound variables does not match number of tokens.
However to my best understanding $stris set to "b" and $ownerId is set to "2" and both are assigned by the setParameters function.
protected function getContactBySubstring($str, $ownerId) {
echo $str;
echo $ownerId;
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->add('select', 'u')
->add('from', '\Paston\VerBundle\Entity\User u, \Paston\VerBundle\Entity\Contact c')
->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE '?2'")
->add('orderBy', 'u.firstname ASC, u.lastname ASC')
->setParameters(array (1=> $ownerId, 2=> '%'.$str.'%'));
echo $qb->getDql();
$query = $qb->getQuery();
$users = $query->getResult();
foreach($users as $user)
echo $user->getUsername();
exit;
//return $contacts;
}
Don't surround any of the parameters in your query text with quotes!
->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE '?2'")
should be
->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE ?2")

Symfony - how to use an array as an parameter by using a querybuilder?

I'm about to output a list that includes several documents(called waiver). However not every user should be allowed to see all documents and therefore I've implemented an filter to check if the user has the same "airline" and "market" assigned. So every user should only see the documents that are assigned to his "airline" and "market".
This is f.e. the getter for the airline of the user entity:
/**
* Get airlines
*
* #return array
*/
public function getAirlines()
{
if($this->airlines != null)
{
$airlines = explode(",", $this->airlines);
return $airlines;
}
return Array();
}
This is the controller logic:
//Get User:
$user = $this->container->get('security.context')->getToken()->getUser();
// Gets an Array of User markets
$user_markets = $user->getMarkets();
// Gets an Array of User carriers
$user_airlines = $user->getAirlines();
if(!$this->ROLE_IS(Array( 'ROLE_XY'))){
$query = $em->createQuery(
'SELECT w
FROM WaiverBundle:Waiver w
WHERE w.carrier = :carrier
AND w.market = :market
ORDER BY w.id DESC'
)
->setFirstResult($page*10-10)
->setMaxResults(10)
// I wan't to get the whole array and not just one position here:
->setParameters(array(':carrier'=>$user_airlines[0],
':market'=>$user_markets[0],
));
}else{
$query = $em->createQuery(
'SELECT u
FROM WaiverBundle:Waiver u
ORDER BY u.id DESC'
)
->setFirstResult($page*10-10)
->setMaxResults(10);
}
Question: How do I manage to compare the DQL attributes with an array and not just a string as a parameter?
I think you want to use "IN" syntax, not "=" syntax:
'SELECT w
FROM WaiverBundle:Waiver w
WHERE w.carrier IN (:carrier)
AND w.market = :market
ORDER BY w.id DESC'
Your query is not complicated. I think you should consider QueryBuilder instead of DQL in this case. Something like this would do the trick:
$qb = $em->createQueryBuilder();
$qb->select('w')
->from('WaiverBundle:Waiver', 'w')
->where($qb->expr()->in('w.carrier', ':carrier'))
->andWhere($qb->expr()->eq('w.market', ':market'))
->orderBy('w.id', 'DESC')
->setParameters(
array(
'carrier'=>$user_airlines[0],
'market'=>$user_markets[0)
);

symfony doctrine complex querybuilder

Maybe something simple but my query builder doesn't return what I want. I would something like ... where ... AND (.... OR ....)
I try to read the doctrine doc but, I'm not fluent english so I don't really understand the doctrine doc (maybe a translation in french is available ?).
<?php
// $qb instanceof QueryBuilder
$qb->select(array('u')) // string 'u' is converted to array internally
->from('User', 'u')
->where($qb->expr()->orX(
$qb->expr()->eq('u.id', '?1'),
$qb->expr()->like('u.nickname', '?2')
))
->orderBy('u.surname', 'ASC'));
Here is my MySQL test code that work
select count(distinct(u.username)) from fos_user u join users_accounts ua ON u.id=ua.user_id join account acc ON acc.id = ua.account_id where u.id=48 and (acc.id=2 OR acc.id=5)
Here is the query builder :
I use a service because I had to use this functionality more than once.
/**
* Check if User exists in one of the connected user (Admin or SupAdmin) accounts
* argument : userID
*
*/
public function userExists($userID)
{
// Return accounts (array)
$accounts = $this->accountManager->listAccountsByConnectedUser();
$repository = $this->em->getRepository('CMiNewsBundle:User');
$qb = $repository->createQueryBuilder('u');
$query = $qb
->select('count(u)')
->join ('u.accounts', 'acc')
->where('u.id = :userID')
->andwhere('acc.id = :accountID')
->setParameters(array(
'userID' => $userID,
'accountID' => $accounts[0]->getId(),
));
if (count($accounts) > 1) {
$accountMax = count($accounts);
for($acc=1; $acc<$accountMax; $acc++)
{
$query->orWhere('acc.id = :accountID_'.$acc.'')->setParameter('accountID_'.$acc.'', $accounts[$acc]->getId());
}
};
$query = $query->getQuery();
$result = $query->getResult();
return $result;
}
Thank you for your advices
You can rewrite your query as using IN() in mysql like below
SELECT
COUNT(DISTINCT (u.username))
FROM
fos_user u
JOIN users_accounts ua
ON u.id = ua.user_id
JOIN account acc
ON acc.id = ua.account_id
WHERE u.id = 48
AND acc.id IN(2,5) /* this is equivalent to acc.id = 2 or acc.id = 5 */
The current query for doctrine you are running is ignoring your and condition like if you have 2 accounts ids query will be
WHERE u.id = 48 AND acc.id = 2 OR acc.id = 5
So it will give records for acc.id = 5 and u.id can be any other id
For doctrine query you can rewrite your code as below,You have to build an array of account ids and pass this array in your IN() clause
public function userExists($userID)
{
// Return accounts (array)
$accounts = $this->accountManager->listAccountsByConnectedUser();
$repository = $this->em->getRepository('CMiNewsBundle:User');
$qb = $repository->createQueryBuilder('u');
$accounts=array();
$accounts[]=$accounts[0]->getId();
if (count($accounts) > 1) {
$accountMax = count($accounts);
for ($acc = 1; $acc < $accountMax; $acc++) {
$accounts[]=$accounts[$acc]->getId();
}
}
$query = $qb
->select('count(u) as your_count')
->join('u.accounts', 'acc')
->where('u.id = :userID')
->andwhere('acc.id IN(:accountID)')
->setParameters(array(
'userID' => $userID,
'accountID' => $accounts,
));
$query = $query->getQuery();
$result = $query->getResult();
return $result;
}

SQL to function in Codeigniter returns row

I'm trying to get an entire row values from a new SQL query so.
How can I make a function
Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id
to something like this :
In model page
public function getID_researcher($lastname){
$query = $this->db->get_where('researcher', array('lastname' => $lastname));
return $query->row_array();
}
I need to return one row result base on the lastname which is from table1
To clarify here, 'researcher' is the table where it gets data but what I want is the new SQL for me to get the data.
I tried this one but still error.
public function getID_researcher($lastname){
$sql = = $this->db->query('Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id');
$query = $this->db->get_where($sql, array('lastname' => $lastname));
return $query->row_array();
}
to use this query
Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id
you can try to use active record db 'join' like this:
$this->db->from('table1 t1');
$this->db->join('table2 t2', 't1.t1_id = t2.t1_id', 'left');
$this->db->join('table3 t3', 't3.id = t2.t3_id', 'left');
$this->db->where('lastname', $lastname);
$query = $this->db->get();
$c = 0;
foreach($query->result() as $q){
$result[$c]['name'] = $q->lastname;
// put move variable here
$c++;
}
return $result;
hope this help

Categories