I have simple tables Post and Comment and I am doing query:
$repository = $this->getDoctrine()
->getRepository('AppBundle:Post');
$query = $repository->createQueryBuilder('p')
->where('p.made = :made')
->setParameter('made', 1)
->leftJoin('p.comments', 'c')
->andWhere('c.isAdmin = :isAdmin')
->setParameter('isAdmin', 1)
->getQuery();
$results = $query->getResult();
foreach($results as $post) {
echo $post->getId(); // this clause where (->where('p.made = :made')) working ok
foreach($post->getComments() as $comments) {
echo $comment->getId(); //this clause where (->andWhere('c.isAdmin = :isAdmin')) not working. This return all results
}
}
So how can I use clause where in query with relations?
SQL:
SELECT
i0_.id AS id0,
i0_.made AS made1,
i0_.name AS name2,
FROM
post i0_
LEFT JOIN comment i1_ ON i0_.id = i1_.comment_id
WHERE
i0_.made = ?
AND i1_.isAdmin = ?
Let's add a select method in your request:
$query = $repository->createQueryBuilder('p')
->select(["c", "p"])
->where('p.made = :made')
->setParameter('made', 1)
->leftJoin('p.comments', 'c')
->andWhere('c.isAdmin = :isAdmin')
->setParameter('isAdmin', 1)
->getQuery();
Is it better ?
This works better because you specify to hydrate the whole objects rather than just Ids.
Related
I have a raw sql query which i run using yii2 Yii::$app->db->createCommand() but then i wonder i how i can use yii's method of writing queries to actualize the same thing.
$m = "SELECT p.basic_plan_amt AS basic,p.premium_plan_amt AS premium,p.daju_plan_amt AS daju,c.name
AS country FROM price p LEFT JOIN country c ON p.country_id = c.id WHERE p.country_id = " .$country_id;
though the above query works well and provide expected result, but then how can i write the same query using the below format
$model = \backend\models\Price::find()->select([p.basic_plan_amt AS basic,p.premium_plan_amt AS
premium,p.daju_plan_amt AS daju,c.name
AS country])->where(['country_id' => $country_id])->all();
Below are the examples of query in yii hope it will help you More reference
Relation Model
$model = User::find()
->with('comments')
->all();
foreach ($model as $user) {
// get data from relation model
$comments = $user->comments;
......
foreach($comments as $comment){
........
}
}
joinWith()
Sample 1:
$model = User::find()
->joinWith('comments')
->all();
Sample 2:
$model = User::find()
->joinWith('comments')
->orderBy('tbl_comments_id.id, tbl_user.id')
->all();
innerJoinWith()
$model = User::find()
->innerJoinWith('comments', false)
->all();
// equivalent to the above
$model = User::find()
->joinWith('comments', false, 'INNER JOIN')
->all();
Join()
JOIN_TYPE = INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN etc
Syntax
$query = new Query;
$query ->select(['SELECT COLUMNS'])
->from('TABLE_NAME_1')
->join( 'JOIN_TYPE',
'TABLE_NAME_2',
'TABLE_NAME_2.COLUMN =TABLE_NAME_1.COLUMN'
);
$command = $query->createCommand();
$data = $command->queryAll();
Sample 1:
$query = new Query;
$query ->select([
'tbl_user.username AS name',
'tbl_category.categoryname as Category',
'tbl_document.documentname']
)
->from('tbl_user')
->join('LEFT OUTER JOIN', 'tbl_category',
'tbl_category.createdby =tbl_user.userid')
->join('LEFT OUTER JOIN', 'tbl_document',
'tbl_category.cid =tbl_document.did')
->LIMIT(5) ;
$command = $query->createCommand();
$data = $command->queryAll();
Output Query
SELECT `tbl_user`.`username` AS `name`, `tbl_category`.`categoryname` AS `Category`
FROM `tbl_user`
LEFT OUTER JOIN `tbl_category` ON tbl_category.createdby =tbl_user.userid
LEFT OUTER JOIN `tbl_document` ON tbl_category.cid =tbl_document.did
LIMIT 5
leftJoin()
Sample 1:
$query = new Query;
$query ->select(['tbl_user.username AS name', 'tbl_category.type as Category'])
->from('tbl_user')
->leftJoin('tbl_category', 'tbl_category.createdby = tbl_user.userid')
->limit(2);
$command = $query->createCommand();
$data = $command->queryAll();
Output Query
SELECT `tbl_user`.`username` AS `name`, `tbl_category`.`type` AS `Category`
FROM `tbl_user`
LEFT JOIN `tbl_category` ON tbl_category.createdby = tbl_user.useridd
LIMIT 2
use createcommand() method:
use yii\db\Query();
$connection = \Yii::$app->db;
$query = new Query;
$insql = $connection->createCommand("SELECT* FROM inventory );
$result=$insql->queryAll();
the above method list all data from the inventory table.
I have a query which looks like below:
SELECT *
FROM mydb.users
left join mydb.job on
users.id = job.userid;
Now I am using doctrine orm in querying database but I am newbie here.
What I have done so far is below but it doesn't work as expected.
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->select(array('a', 'c'))
->from('Admin\Entity\User', 'a')
->leftJoin('a.id', 'c');
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
you do not need the from clause and array from clause select
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder('u');
$qb->select('u', 'j')
->leftJoin('u.jobs', 'j')
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
it is mandatory that your users entity contains a "one to many" jobs for the attribute.
trick to display the SQL output :
var_dump($qb->getQuery()->getSQL());
I'm struggling with my own social network where i'm trying to sort the comments of a post by the amount of likes on them.
So i made three tables : a Post with many Comments and a Comment with many Likes. Of course, there's a User table to handle all of this.
public function loadProfilePost($user, $first = 0, $limit = 0)
{
$q = $this->_em->createQueryBuilder()
->select('post, comment')
->from('EvoSocialBundle:Post', 'post')
->leftJoin('post.comments', 'comment')
->where('post.user = :user')
->setParameter(':user', $user)
->addOrderBy('post.created', 'DESC');
$q->setFirstResult($first);
if($limit > 0)
$q->setMaxResults($limit);
return $q->getQuery()->getResult();
}
To sort comments by its count of likes you can add one more join in your query builder with likes entity then set group by criteria like group by post.id,comments.id, i have added addSelect() with count function to count the likes for comment and setting the result of count AS HIDDEN so that they will not be returned with the result set and will affect only the query builder part
public function loadProfilePost($user, $first = 0, $limit = 0)
{
$q = $this->_em->createQueryBuilder()
->select('p, c')
->addSelect('COUNT(DISTINCT c.id) AS HIDDEN total_likes')
->from('EvoSocialBundle:Post', 'p')
->leftJoin('p.comments', 'c')
->leftJoin('c.likes', 'l')
->where('p.user = :user')
->setParameter(':user', $user)
->groupBy('p.id')
->addGroupBy('c.id')
->orderBy('total_likes','DESC');
$q->setFirstResult($first);
if($limit > 0)
$q->setMaxResults($limit);
return $q->getQuery()->getResult();
}
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;
}
I want to get the last user profile . But i am not able to do that in DQL.
I have this code
$em = $this->getEntityManager();
$dql = "SELECT p FROM AcmeBundle:UserProfile p
WHERE p.user_id = :user_id
ORDER BY p.createdAt DESC ";
$allProfiles = $em->createQuery($dql)
->setParameter('user_id', $user_id)
->setMaxResults(5)
->getResult();
return $allProfiles;
It returns all the profiles.
If i use getSingleResult() then it says result not unique
The right method is:
$singleProfile = $em->createQuery($dql)
->setParameter('user_id',$user_id)
->getSingleResult();
To prevent error then no results try this:
$singleProfile = $em->createQuery($dql)
->setParameter('user_id',$user_id)
->getOneOrNullResult();
$allProfiles = $em->createQuery($dql)
->setParameter('user_id',$user_id)
->setMaxResults(1)
->getResult();
return $allProfiles[0];