kamikaze_pilot
kamikaze_pilot

Reputation: 14844

zend db select join help

so I have this code:

$sql = new Zend_Db_Select($db);
$sql->from("j");
$sql->join("k","k.id = j.id",array());
echo $sql;

which results in the query:

SELECT `j`.* FROM `j` INNER JOIN `k` ON k.id = j.id

but I don't just want j.*

I also want k.*

how do I specify zend_db_select for this?

Upvotes: 1

Views: 1312

Answers (5)

luca
luca

Reputation: 37146

$sql=new Zend_Db_Select($db);
$sql->from('j',array(
//here fields you want to get from 'j' Ex. 'j.id'
));
$sql->join('k',
// here your condition
'k.id = j.id',
//here the fields you need from 'k'
array(
//now the array is empty!!You need to specify needed fields.
//Or just remove it.Defaults is "k.*"!

));

Upvotes: 0

inquam
inquam

Reputation: 12942

You explicitly tells the join that you want no columns returned for k when you pass an empty array to it. Instead use:

$sql = new Zend_Db_Select($db);
$sql->from('j');
$sql->joinLeft('k','k.id = j.id');
echo $sql;

That should produce what you want I think.

Upvotes: 0

tXK
tXK

Reputation: 712

if you also want k.*, I think you should have
$sql->join("k","k.id = j.id");, without that array() as the third parameter.
From what I know, that third parameter specifies the columns to be selected from k and since you passed an empty array I'm guessing you override that default k.*

Upvotes: 0

Gabriel Spiteri
Gabriel Spiteri

Reputation: 4978

You can specify the columns in the from method.

$sql->from(array("j" => "j", array("j.*" , "k.*");

Honestly I am not entirely sure but the Zend Documentation says so :)

Upvotes: 1

Alex Pliutau
Alex Pliutau

Reputation: 21937

The third parameter for join() is columns for select. You have passed an empty array.

$sql = new Zend_Db_Select($db);
$sql->from('j');
$sql->join('k','k.id = j.id',array());
echo $sql;

Note: for this condition k.id = j.id you should use LEFT JOIN(->joinLeft(...))

Upvotes: 2

Related Questions