I am trying to use "join" in a query while developing a widget that is within a module for Social Engine 4. I'm working in Dreamweaver. I am trying to replace this working code:
//creates query to get user's region
$select = $db->select();
$select->from("engine4_user_fields_search");
$select->where("item_id = ?", $user_id['user_id']);
$stmt = $db->query($select);
$result = $stmt->fetchAll();
I then have to use a long if-else chain, adding and subtracting from indexes using "magic numbers" to get the result.
Using join, I wouldn't need to do all that. I could have a simple query and display the result. The widget I'm working on is in a tab container so when there is a problem (every time i use join) the whole tab disappears and makes debugging a problem.
Here is what I have:
<?php
class Widget_RegionalBreakingNewsController extends Engine_Content_Widget_Abstract
{
public function indexAction()
{
//connect to DB
$file = APPLICATION_PATH . '/application/settings/database.php';
$options = include $file;
$db = Zend_Db::factory($options['adapter'], $options['params']);
$select = new Zend_Db_Select($db);
$db->getConnection();
//end DB setup
$user_id = Engine_Api::_()->user()->getViewer();
//SELECT * FROM engine4_user_fields_search as s
//join engine4_user_fields_options as o
//WHERE item_id = <current user's ID> AND s.field_7+22 = o.option_id
$select = $db->select();
$select->from(array('s' => 'engine4_user_fields_search'),
array('s.field_7', 's.user_id'))
->joinInner(array('o' => 'engine4_user_fields_options'),
's.field_7+22 = o.option_id');
$select->where("s.item_id = ?", $user_id['user_id']);
$stmt = $db->query($select);
$result = $stmt->fetchAll();
//print_r($result);
As this is my first week developing with Zend, don't dismiss even the simplest of errors. I am prone to not knowing simple conventions that may make a difference. If anyone can offer help it would be appreciated.
PS Is there anyway to not use the Zend select functions and do a regular SQL statement?
After having another look at your code, I think the problem is this line:
$select->from(array('s' => 'engine4_user_fields_search'),
array('s.field_7', 's.user_id'))
You're telling it to only select two columns (equivalent to SELECT s.field_7, s.user_id FROM ...
) which I don't think is what you intended? Your first example that you say works is selecting all columns from engine4_user_fields_search. You can just leave the second array off there to get all columns from that table.
Also, this:
$stmt = $db->query($select);
$result = $stmt->fetchAll();
Could be written as just:
$result = $db->fetchAll($select);
You can debug Zend_Db_Select
issues by simply echoing the select object, which will give you the generated query as a string:
echo $select;
And if you prefer to write raw SQL you can just pass that to the query function as a string:
$db->query("
SELECT * FROM engine4_user_fields_search as s
join engine4_user_fields_options as o
WHERE item_id = ? AND s.field_7+22 = o.option_id
", array($user_id['user_id']));