Unable to left join on multiple tables with PhalconPHP's createBuilder? - phalcon

Maybe I am doing something wrong here, but I am unable to join on multiple tables when using createBuilder(). Here is an example query.
$test = $this->modelsManager->createBuilder()
->from('TABLE1')
->leftJoin('TABLE2', 'TABLE1.id = TABLE2.table_one_id')
->leftJoin'TABLE3', 'TABLE3.id = TABLE2.table_three_id')
->where('TABLE1.id = :id:', array('id' => $id))
->groupBy(array('TABLE1.id'))
->getQuery()
->execute();
There error indicates that the SQL query is probably not being generated correctly by the framework, but I could very well be doing something wrong. It appears that no space is being added before the additional LEFT join.
Unknown column 'TABLE2.table_one_idLEFT' in 'on clause''
Any insight would be greatly appreciated.

You will need to add a space at the end of first join condition:
$test = $this->modelsManager->createBuilder()
->from('TABLE1')
->leftJoin('TABLE2', 'TABLE1.id = TABLE2.table_one_id ')
->leftJoin('TABLE3', 'TABLE3.id = TABLE2.table_three_id')
->where('TABLE1.id = :id:', array('id' => $id))
->groupBy(array('TABLE1.id'))
->getQuery()
->execute();

Related

ZF2: Is there a more efficient way to do this Zend\Db Update query?

Here's some ZF1 code for an update query:
$this->getAdapter()->update(
'users', $data, $this->getAdapter()->quoteInto('node_id = ?', $user->nodeId)
);
Here's the same query with ZF2:
$param = $this->getAdapter()->platform->quoteValue($user->nodeId);
$sqlOj = new Sql($this->getAdapter());
$update = $sqlOj->update('users')->set($data)->where('node_id = ' . $param);
$updateString = $sqlOj->getSqlStringForSqlObject($update);
$this->getAdapter()->query($updateString, Adapter::QUERY_MODE_EXECUTE);
As you can see, one line of ZF1 code has become 5 lines of ZF2 code, (actually without the fluent interface it would be 7 lines...)
Am I missing something? Or is ZF2's DB component just more verbose that ZF1?
BTW, I have found the same scenario with select and insert queries too...
I managed to limit it to 3 lines.
use \Zend\Db\Sql\Sql;
$sql = new Sql ($adapter);
$update = $sql->update ('users')->set ($data)->where (['id = ?' => 1]);
$adapter->query ($sql->getSqlStringForSqlObject ($update), $db::QUERY_MODE_EXECUTE);
The problem is they didn't expect you to run your updates like that. Instead, you are expected to use a table gateway.
This way it becomes one line again:
$this->tableGateway->update($data, array('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'))

How to get particular column in zend using Left join

I am new to zend framework,
Following is the plain mysql query which takes particular column from table,
SELECT jobs_users.id,jobs_users.first_name from jobs_users left join friends on jobs_users.id=friends.friend_id where friends.member_id=29
I tried with zend to implement the above query like below,
public function getFriendsProfileList($id){
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select();
$select->from('jobs_users')
->joinLeft(
'friends',
'jobs_users.id=friends.friend_id',
array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo')
)
->where("friends.member_id = ?", $id);
$result = $db->fetchAll($select);
return $result;
}
Here i got result with all column name , not with exact column name which i have given in query.
Kindly help me on this.
Use this instead:
$select->from('jobs_users', array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo'))
->joinLeft('friends', 'jobs_users.id=friends.friend_id')
->where("friends.member_id = ?", '20');
You may also try this:
$select = $db->select();
$select->setIntegrityCheck(false);
$select->joinLeft('jobs_users','',array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo'));
$select->joinLeft('friends','jobs_users.id=friends.friend_id', array());
$select->where("friends.member_id = ?", $id);
$result = $db->fetchAll($select);
return $result;

Zend Framework Db Select Join table help

I have this query:
SELECT g.title, g.asin, g.platform_id, r.rank
FROM games g
INNER JOIN ranks r ON ( g.id = r.game_id )
ORDER BY r.rank DESC
LIMIT 5`
Now, this is my JOIN using Zend_Db_Select but it gives me array error
$query = $this->select();
$query->from(array('g' => 'games'), array());
$query->join(array('r' => 'ranks'), 'g.id = r.game_id', array('g.title', 'g.asin', 'g.platform_id', 'r.rank'));
$query->order('r.rank DESC');
$query->limit($top);
$resultRows = $this->fetchAll($query);
return $resultRows;
Anyone know what I could be doing wrong? I want to get all the columns in 'games' to show and the 'rank' column in the ranks table.
I am going to assume you've solved this, but it would be nice to leave the answer for others.
Add this below the instantiation of the select object.
$query->setIntegrityCheck(false);
You could also type fewer characters....
$query = $this->select()
->from(array('g' => 'games'), array('title', 'asin', 'platform_id'))
->join(array('r' => 'ranks'), 'g.id = r.game_id', array('rank'))
->order('r.rank DESC')
->limit($top);
return $this->fetchAll($query);
Good luck!
Here's how I'd write it:
$query = $this->select();
$query->from(array('g' => 'games'), array('title', 'asin', 'platform_id'));
$query->join(array('r' => 'ranks'), 'g.id = r.game_id', array('rank'));
$query->order('r.rank DESC');
$query->limit($top);
$resultRows = $this->fetchAll($query);
return $resultRows;
Other example:
select n.content, n.date, u.mail
from notes n, users u
where n.id_us=u.id and reminder=current_date
$query = $this->select()
->from(array('n'=>'notes'),
array('content', 'date'))
->join(array('u'=>'users'), 'n.id_us=u.id and n.reminder=current_date',
array('mail'))
->setIntegrityCheck(false);
return $this->fetchAll($query);
That's work fine :)