Join with conditional IN with Doctrine2 - sql

Not even sure this is possible, but it looks like it should
In my Video entity, I've got a ManyToOne associate for the Product entity
Here, I am trying to acquire all published products, and acquire the videos that belong to that product with a Join
// ProductRepository.php
public function findPublished()
{
$q = $this->getEntityManager()->createQueryBuilder();
$q->select(['p', 'v'])->from($this->getEntityName(), 'p')
->leftJoin('p.videos', 'v', 'ON', 'p IN v.products')
->where('p.published = :published')
->setParameter('published', true);
$results = $q->getQuery()->getResult();
return $results;
}
The Exception that comes back is this:
[Syntax Error] line 0, col 76: Error: Expected end of string, got 'ON'
[1/2] QueryException: SELECT p, v FROM Company\CoreBundle\Entity\Product p LEFT JOIN p.videos v ON p IN v.products WHERE p.published = :published

If your entities are mapped correctly, you don't need to specify the join condition. Try this
$q->select(['p', 'v'])->from($this->getEntityName(), 'p')
->leftJoin('p.videos', 'v')
->where('p.published = :published')
->setParameter('published', true);

Related

transform SQL to createQueryBuilder

I'm trying to transform this sql
SELECT * FROM user_account
LEFT JOIN brand ON user_account.id = brand.user_id_id
LEFT JOIN influencer ON user_account.id = influencer.user_id_id'
to a createQueryBuilder, I try this but it's not working
$qb = $this->createQueryBuilder('u')
->from(Brand::class, 'brand')
->from(Influencer::class, 'influencer')
->leftJoin('brand', 'b', 'ON', 'u.id = b.user_id_id')
->leftJoin('influencer', 'i', 'ON', 'u.id = i.user_id_id');
$query = $qb->getQuery();
return $query->execute();
And I got this error
[Semantical Error] line 0, col 42 near 'brand b ON u.id': Error: Class
'brand' is not defined.
Someone can help?
Many thanks
I have never used doctrine query builder, but after reading the docs I would suggest the following solution:
$qb = $this->createQueryBuilder()
->from('user_account', 'u')
->leftJoin('u', 'brand', 'b', 'u.id = b.user_id_id')
->leftJoin('u', 'influencer', 'i', 'u.id = i.user_id_id')
$query = $qb->getQuery();
return $query->execute();
NOTE: I left b.user_id_id as it is in your example, even though I think there is a typo. Maybe this should mean b.user_id. The same for influencer.

How to write join query with multiple column - LINQ

I have a situation where two tables should be joined with multiple columns with or condition. Here, I have a sample of sql query but i was not able to convert it into linq query.
select cm.* from Customer cm
inner join #temp tmp
on cm.CustomerCode = tmp.NewNLKNo or cm.OldAcNo = tmp.OldNLKNo
This is how i have write linq query
await (from cm in Context.CustomerMaster
join li in list.PortalCustomerDetailViewModel
on new { OldNLKNo = cm.OldAcNo, NewNLKNo = cm.CustomerCode } equals new { OldNLKNo = li.OldNLKNo, NewNLKNo = li.NewNLKNo }
select new CustomerInfoViewModel
{
CustomerId = cm.Id,
CustomerCode = cm.CustomerCode,
CustomerFullName = cm.CustomerFullName,
OldCustomerCode = cm.OldCustomerCode,
IsCorporateCustomer = cm.IsCorporateCustomer
}).ToListAsync();
But this query doesn't returns as expected. How do I convert this sql query into linq.
Thank you
You didn't tell if list.PortalCustomerDetailViewModel is some information in the database, or in your local process. It seems that this is in your local process, your query will have to transfer it to the database (maybe that is why it is Tmp in your SQL?)
Requirement: give me all properties of a CustomerMaster for all CustomerMasters where exists at least one PortalCustomerDetailViewModel where
customerMaster.CustomerCode == portalCustomerDetailViewModel.NewNLKNo
|| customerMaster.OldAcNo == portalCustomerDetailViewModel.OldNLKNo
You can't use a normal Join, because a Join works with an AND, you want to work with OR
What you could do, is Select all CustomerMasters where there is any PortalCustomerDetailViewModel that fulfills the provided OR:
I only transfer those properties of list.PortalCustomerDetailViewModel to the database that I need to use in the OR expression:
var checkProperties = list.PortalCustomerDetailViewModel
.Select(portalCustomerDetail => new
{
NewNlkNo = portalCustomerDetail.NewNlkNo,
OldNLKNo = portalCustomerDetail.OldNLKNo,
});
var result = dbContext.CustomerMasters.Where(customerMaster =>
checkProperties.Where(checkProperty =>
customerMaster.CustomerCode == checkProperty.NewNLKNo
|| customerMaster.OldAcNo == checkProperty.OldNLKNo)).Any()))
.Select(customerMaster => new CustomerInfoViewModel
{
Id = customerMaster.Id,
Name = customerMaster.Name,
...
});
In words: from each portalCustomerDetail in list.PortalCustomerDetailViewModel, extract the properties NewNKLNo and OldNLKNo.
Then from the table of CustomerMasters, keep only those customerMasters that have at least one portalCustomerDetail with the properties as described in the OR statement.
From every remaining CustomerMasters, create one new CustomerInfoViewModel containing properties ...
select cm.* from Customer cm
inner join #temp tmp
on cm.CustomerCode = tmp.NewNLKNo or cm.OldAcNo = tmp.OldNLKNo
You don't have to use the join syntax. Adding the predicates in a where clause could get the same result. Try to use the following code:
await (from cm in Context.CustomerMaster
from li in list.PortalCustomerDetailViewModel
where cm.CustomerCode == li.NewNLKNo || cm.OldAcNo = li.OldNLKNo
select new CustomerInfoViewModel
{
CustomerId = cm.Id,
CustomerCode = cm.CustomerCode,
CustomerFullName = cm.CustomerFullName,
OldCustomerCode = cm.OldCustomerCode,
IsCorporateCustomer = cm.IsCorporateCustomer
}).ToListAsync();
var result=_db.Customer
.groupjoin(_db.#temp ,jc=>jc.CustomerCode,c=> c.NewNLKNo,(jc,c)=>{jc,c=c.firstordefault()})
.groupjoin(_db.#temp ,jc2=>jc2.OldAcNo,c2=> c2.OldNLKNo,(jc2,c2)=>{jc2,c2=c2.firstordefault()})
.select(x=> new{
//as you want
}).distinct().tolist();

Symfony query on intermediate table

I have a ManyToMany relation between guest and event. The intermediate table is events.
This sql query is working in PHP My Admin :
SELECT *
FROM guest AS G
LEFT JOIN guest_event AS E ON event_id = G.id
I'm trying to do the same query with the querybuilder, and tried this:
$query = $this->createQueryBuilder('g')
->leftJoin('g.events', 'e', 'WITH', 'e.id = :id')
->addSelect('e');
but I get no results! Does anyone have a solution?
Or try this:
$id = 1; // Your event id
$repository = $this->getDoctrine()
->getRepository('AcmeDemoBundle:Entity');
$query = $repository->createQueryBuilder('g')
->select('e')
->leftJoin('g.events', 'e', 'WITH', 'e.id = :id')
->setParameter('id', $id)
->getQuery();
$events = $query->getResult();
Read the docs about Using Doctrine's Query Builder in Symfony
You don't have entity reference
Probably something like this would do it:
$query = $this->createQueryBuilder()
->select('e')
->from('MyBundle:Guest', 'g')
->leftJoin('g.events', 'e', 'WITH', 'e.id = :id');

symfony and DQL

i have :
public function findTodasLasCompras($usuario_id)
{
$em = $this->getEntityManager();
$dql = 'SELECT v, o, t
FROM OfertaBundle:Venta v
JOIN v.oferta o
JOIN o.tienda t
WHERE v.usuario = :id
ORDER BY v.fecha DESC';
$consulta = $em->createQuery($dql);
$consulta->setParameter('id', $usuario_id);
$result = $consulta->getResult();
return $consulta->getResult();
}
and when i execute it fails with it's error:
ContextErrorException: Notice: Undefined index: Cupon\OfertaBundle\Entity\Oferta in C:\wamp\www\sympony\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php line 477
in C:\wamp\www\sympony\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php line 477
at ErrorHandler->handle('8', 'Undefined index: Cupon\OfertaBundle\Entity\Oferta',
help! please! Thanks
Try to tranform your 'JOIN' to 'LEFT JOIN'.
If an usario exists well with id 8, maybe he is not linked to oferta or tienda, so the jointure return nothing, but left join keep usario instance.
First of all, replace this:
$result = $consulta->getResult();
return $consulta->getResult();
with this:
$result = $consulta->getResult();
return $result;
Maybe you should reinstall Symphony, it seems something is broken.

Codeigniter Joining Tables - Passing Parameters

I'm attempting to join two tables while using codeigniter. I've done this same SQL query writing regular SQL. When I attempt to do the same in codeigniter, I keep getting errors. I'm not quite sure what I'm doing wrong. What do you guys think I'm doing wrong?
My function in model_data.php
function getJoinInformation($year,$make,$model)
{
//$this->db->distinct();
// here is where I need to conduct averages but lets just work on extracting info.
// from the database and join tables.
$this->db->select('*');
$this->db->from('tbl_car_description');
$this->db->join('tbl_car_description', 'd.id = p.cardescription_id');
$this->db->where('d.year', $year);
$this->db->where('d.make', $make);
$this->db->where('d.model', $model);
$result = $this->db->get();
/*
$query = $this->db->get_where('tbl_car_description',
array(
'year' => $year,
'make' => $make,
'model' => $model
)
);
if($query->num_rows()) return $query->result();
return null;
*/
}
My error message
A Database Error Occurred
Error Number: 1066
Not unique table/alias: 'tbl_car_description'
SELECT * FROM (`tbl_car_description`) JOIN `tbl_car_description` ON `d`.`id` = `p`.`cardescription_id` WHERE `d`.`year` = '2006' AND `d`.`make` = 'Subaru' AND `d`.`model` = 'Baja'
Filename: C:\wamp\www\_states\system\database\DB_driver.php
Line Number: 330
Here is the code written in SQL and it's working great. I want to do the something in codeigniter but I'm confused as to how. Any help would be much appreciated. Thanks everyone.
$sql_2 = mysql_query("SELECT ROUND(AVG(p.value),1) AS AvgPrice, ROUND(AVG(p.mileage),1) AS AvgMileage
FROM tbl_car_description d, tbl_car_prices p
WHERE (d.id = p.cardescription_id)
AND (d.year = '".$year."')
AND (d.make = '".$make."')
AND (d.model = '".$model."')
AND (p.approve = '1')");
Your CI query references the same table twice, which I assume is a typo. However, you only need include your table alias with the table name in your Active Record call:
//$this->db->distinct();
$this->db->select('*');
$this->db->from('tbl_car_description d');
$this->db->join('tbl_car_prices p', 'd.id = p.cardescription_id');
$this->db->where('d.year', $year);
$this->db->where('d.make', $make);
$this->db->where('d.model', $model);
$result = $this->db->get();
Couple issues: You need to include your table aliases and your join should have the name of the second table ...
$this->db->from('tbl_car_description AS d');
$this->db->join('tbl_car_prices AS p', 'd.id = p.cardescription_id');