I have an working MySQL Query now i need it to convert to Typo3 Syntax
SELECT
tt_news_tx_extendnews_subscriber_mm.uid_local,
fe_users.*
FROM fe_users
JOIN tt_news_tx_extendnews_subscriber_mm
ON tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid
WHERE tt_news_tx_extendnews_subscriber_mm.uid_local = 101
TYPO3
$res0 = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
'tt_news_tx_extendnews_subscriber_mm.uid_local,fe_users.*',
'fe_users',
'tt_news_tx_extendnews_subscriber_mm',
'tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid',
'tt_news_tx_extendnews_subscriber_mm.uid_local = 101',
'',
'',
''
);
the result is empty...anybody knows how this work with typo3?
Debug brings this: $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
SELECT tt_news_tx_extendnews_subscriber_mm.uid_local,fe_users.*
FROM fe_users,tt_news_tx_extendnews_subscriber_mm,tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid
WHERE fe_users.uid=tt_news_tx_extendnews_subscriber_mm.uid_local
AND tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid.uid=tt_news_tx_extendnews_subscriber_mm.uid_foreign tt_news_tx_extendnews_subscriber_mm.uid_local = 101
By exec_SELECT_mm_query the 4th parameter is the foreign key table name, not the reference. You need instead of:
tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid
only:
fe_users
See more details in TYPO3 api : exec_SELECT_mm_query.
I think, you can try the following:
$res0 = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
'tt_news_tx_extendnews_subscriber_mm.uid_local,fe_users.*',
'tt_news',
'tt_news_tx_extendnews_subscriber_mm',
'fe_users',
'tt_news_tx_extendnews_subscriber_mm.uid_local = 101',
'',
'',
''
);
Or you can use exactly the SQL what you have with the following little trick, because exe_SELECTquery only concats your string to SELECT..., FROM... (and so on...) parts. So because of this, you can use a JOIN in FROM part.
$res0 = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tt_news_tx_extendnews_subscriber_mm.uid_local,fe_users.*',
'fe_users
JOIN tt_news_tx_extendnews_subscriber_mm
ON tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid',
'tt_news_tx_extendnews_subscriber_mm.uid_local = 101',
'',
'',
'');
Related
I would like to get the mongodb insert returned id for further insert specific operation my code is this:
$collection1 = $collection->insert(['username' => $this->email,
'password' =>sha1($this->input_password), 'full_name' => $this->full_name
'time_created' => '', 'time_updated' => '', 'time_last_visited' => '']);
when i does var_dump($collection1) function it shows me output
object(MongoId)#92 (1) { ["$id"]=> string(24) "54ba2b2c64363d640f000031" }
but when i try to access the id it shows me errors
i tried following options:
$insert_id = $collection1->primaryKey();
$insert_id = $collection1->getLastInsertID();
$id = $collection1->id;
$id = (string)$collection1['id'];
$id = $collection1['_id'];
but no options work. What is the right way?
Well i got my answer i tried another one differently & that works.
$id = (string) $collection1;
This returns me the string formatted mongoId.
$id = $collection->insert($data);
I've got a problem. I'm trying to left join two tables with Zend Framework using $select object. Unfortunatly my tables has common field 'name' and when I'm joining one with the other the results I get is that name field from table overwrites the name field from the other.
My code is something like that:
$select->joinLeft ( array ('users' => 'users' ), $this->_name . '.employee_id = users.user_id', array ('*' ) );
How I can join tables and avoid this issue?
Use table aliases as you would in any normal sql query!
With Zend_Db aliases are written like this:
$select = $db->select()
->from(array('p' => 'products'),
array('product_id', 'product_name'))
->join(array('l' => 'line_items'),
'p.product_id = l.product_id',
array() ); // empty list of columns
The non-zend query would look like this:
SELECT p.product_id, p.product_name
FROM products AS p
JOIN line_items AS l ON p.product_id = l.product_id;
I guess it's bit late but to get all fields from two tables you must alias all the fields
$select = $db->select()
->from(array('u' => 'users'),
array('u.id'=>'u.id','u.employee_id'=>'u.employee_id','u.name'=>'u.name'))
->joinLeft(array('e' => 'employees'),
'e.id = u.employee_id',
array('e.id'=>'e.id','e.name'=>'e.name') );
And your array would look like:
array(
0=>array(
'u.id'=>'1',
'u.employee_id'=>'1',
'u.name'=>'John Doe',
'e.id'=>'1',
'e.name'=>'Worker'
),
1=>array(
...
));
I have tow table :
article,sub_categories
how to convert this query to zend Db query join
$sql="
SELECT article.*, sub_categories.name_arabic AS sub_category_name_arabic, sub_categories.name_english AS sub_category_name_english, article.sub_category_id AS sub_categories_id FROM article
LEFT JOIN sub_categories ON article.sub_category_id = sub_categories.id
WHERE article.active = 1
";
Please need help
Thanks with regard
This should do the trick:
$db->select()
->from('article', array('*', 'sub_categories_id' => 'sub_category_id')
->joinLeft(
'sub_categories',
'article.sub_category_id = sub_categories.id',
array('sub_category_name_arabic' => 'name_arabic', 'sub_category_name_english' => 'name_english'))
->where('article.active = 1');
I am trying to replicate this query using zend framework:
SELECT
activitytype.description,
activity.datecompleted
FROM
clientactivity
INNER JOIN activity
ON activity.activityID = clientactivity.activityid
INNER JOIN activitytype
ON activitytype.activitytypeid = activity.activitytypeid
WHERE
clientactivity.clientid = 100
This is what I have so far:
$select = $dbTable->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->setIntegrityCheck(false);
$select->where('clientactivity.clientid = ?', $clientID);
$select->join('activity', 'activity.activityid = clientactivity.activityid');
$select->join('activitytype', 'activitytype.activitytypeid = activity.activitytypeid');
$select->columns(array('activitytype.description', 'activity.datecompleted'));
I seem to be having problems with the columns option, it doens't seem to be limiting the columns and I am ending up with
clientactivity.* etc in the column list in the query.
What am I doing wrong?
Thanks,
Martin
Try instead of the $select->columns();
$select->from('activitytype.description', 'activity.datecompleted');
Reference - http://framework.zend.com/manual/en/zend.db.select.html
UPDATE:
This example makes us of a generic database handler:
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => '127.0.0.1',
'username' => 'yourusername',
'password' => 'somepassword',
'dbname' => 'yourdbname'
));
$select = $db->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->from('tableName','fieldName')
->join('joinTable', 'joinTable.keyId = tableName.keyId',array())
->where('tableName.userId = ?', $userId);
$resultSet = $db->fetchAll($select);
The key piece is the blank array at the end of the join statements that specifies no records to be returned from the joined table.
I'm trying to do a group by using Zend framework. Here's my code:
$table = new TableClass();
$select = $table->select();
$select->from ("table", array("date", "column1" => "sum(column1)"));
$select->group ( array ("date") );
$results = $table->fetchAll ($select);
$result = $results[0];
$date = $result->date;
$column1 = $result->column1;
TableClass extends 'Zend_Db_Table_Abstract'.
I can see the query by looking at the mysql query log. The query is well formed - column1 is named in the query and the results look correct if I run the query in mysql workbench.
I cannot access the data in 'column1' - I always get this exception:
Uncaught exception 'Zend_Db_Table_Row_Exception' with message 'Specified column "column1" is not in the row'
I can however access the date column without issue.
I tried:
accessing the columns by array index:
$result[0]
but you get an exception (can't access the columns by index).
not using a column alias:
$select->from ("table", array("date", "sum(column1)"));
$column1 = $result["sum(column1)"];
but you get an exception (no such column "sum(column1)").
throwing in a Zend_Db_Expr:
"column1" => new Zend_Db_Expr ( "sum(column1)" )
but this doesn't help.
Some other examples I have seen suggest the use of the column names without aggregate functions, ie. "column1" instead of "sum(column1)" but that doesn't seem to me to be the answer - the query doesn't have any aggregate functions in it so mysql won't know what to do with it.
Any help appreciated.
Firstly, a quick tip for working with Zend_Db_Select (and by extension Zend_Db_Table_Select), you can view the generated SQL by invoking the toString method. It is vital to verify that your code generates the correct query before working with a result set:
$select = $table->select();
$select->from ("table", array("date", "column1" => "sum(column1)"));
$select->group ( array ("date") );
$sql = (string) $select; //Retrieve SQL as a string
Or simply
die($select); //print SQL
I wrote the following test script using your example and have no problems:
class Table extends Zend_Db_Table_Abstract
{
protected $_primary = 'id';
protected $_name = 'table';
}
$db = Zend_Db::factory('Pdo_Mysql', array(
'dbname' => 'test',
'username' => 'root',
'password' => '',
'host' => 'localhost'
));
$table = new Table($db);
$select = $table->select();
$select->from ($table, array("date", "column1" => new Zend_Db_Expr("sum(column1)")));
$select->group ( array ("date") );
$sql = (string) $select;
echo $sql;
$results = $table->fetchAll ($select);
$result = $results[0];
$date = $result->date;
$column1 = $result->column1;
echo '<br>' . $date . ': ' . $column1;
Use Zend_Debug::dump($result); to inspect data inside the Zend_Db_Table_Row if necessary.
In my case the SQL generated is as follows:
SELECT `table`.`date`, sum(column1) AS `column1` FROM `table` GROUP BY `date`