transform SQL to createQueryBuilder - sql

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.

Related

Laravel Query Builder is not working when i put 'where' and 'orWhereNull'

SQL:
$datas = DB::select("SELECT a.id,a.parent_id, d.action_id, d.role_id,d.department_id, d.is_created,
d.is_edit,d.is_delete,d.is_view,d.is_audit,d.is_verify,d.is_approved
FROM application_security_actions a
LEFT JOIN application_default_permissions d on d.action_id = a.id where d.role_id = " . $id . " or d.role_id is null");
Query Builder:
$datas = DB::select('application_security_actions as A')
->leftJoin('application_default_permissions as B','A.id','=','B.action_id')
->where('B.role_id','=',$id)
->orWhereNull('B.role_id')
->select('A.id','A.parent_id','B.is_created','B.is_edit','B.is_delete','B.is_view','B.is_audit','B.is_verify','B.is_approved')
->get();
sql is working perfectly but when is convert in to query builder it is not working. can any on check where i am wrong
Change on the line 1
DB::select to DB::table
Full code:
$datas = DB::table('application_security_actions as A')
->leftJoin('application_default_permissions as B','A.id','=','B.action_id')
->where('B.role_id','=',$id)
->orWhereNull('B.role_id')
->select('A.id','A.parent_id','B.is_created','B.is_edit','B.is_delete','B.is_view','B.is_audit','B.is_verify','B.is_approved')
->get();

Join with conditional IN with Doctrine2

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);

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');

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');

createQueryBuilder IN clause

I´m working with Symfony2 and I need to execute this SQL for example:
select detformacion.* from detformacion
left join formacion
on detformacion.formacion_id = formacion.id
left join detcurso
on formacion.id = detcurso.formacion_id
where detcurso.id IN ('143','144');
For that, I have this in my Repository:
public function getSeleccion() {
$em = $this->getEntityManager();
$query = $em->createQueryBuilder()
->select('d')
->from('GitekUdaBundle:Detformacion', 'd')
->leftJoin('d.formacion', 'f')
->leftJoin('f.detcursos', 'det')
->where('det.id = :miarray')
->setParameter('miarray',array('143','144'))
->getQuery()
;
return $query->getResult();
}
I tried with ->where('det.id IN :miarray') but I´m getting errors all the time.
Any help or clue?
thanks in advance.
UPDATE: The problem is setting the parameters.
Missing parentheses after the IN operator :
->where('det.id IN (:miarray)')
->setParameter('miarray', array('143','144'))