Zend framework - how to write group by query - zend-framework

I use zend framework and can't write in model next sql query:
SELECT `id_user`,`system_role`,count(`system_role`)
FROM `favorites`
group by `id_user`,`system_role`
How to do this?

$db = new Zend_Db_Table('favorites');
$select = $db->select()
->from('favorites', array('id_user', 'system_role', 'COUNT(system_role)'))
->group('id_user', 'system_role');

Related

joinLeft Zend Framework, the same field names in different tables

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

convert sql query to Zend DB query

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

Problem with sql in zend framework

I wanted to get the sql query like SELECT numbers FROM table ORDER BY numbers+0; in zend framework.
I am new to zend. Can anyone please help me on this.
Thank you.
What about this:
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select()->from('table','numbers')->order(new Zend_Db_Expr('numbers+0'));
var_dump($select->assemble());
//outputs:
//string 'SELECT `table`.`numbers` FROM `table` ORDER BY numbers+0' (length=56)
new Zend_Db_Expr is needed because without it, ZF will add 'ASC' to your query:
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select()->from('table','numbers')->order('numbers+0');
var_dump($select->assemble());
//outputs:
//string 'SELECT `table`.`numbers` FROM `table` ORDER BY `numbers+0` ASC' (length=62)
Hope this helps.

Zend Framework Query with Joins

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.

Zend DB Selecting constants - columns that do not exist in table

I'm trying to do this query using Zend DB select but I'm not able to do so
This is the sql query
select shopping_id,shopping_details,"friend" as type
from shopping
Notice here how I'm specifying "friend" as type and friend is not a column in the shopping table.
Now how do I do this in Zend. I have tried this but it gives me an error saying "sh.friend Column does not exist"
$select->from(array('sh'=>'shopping'),array('shopping_id','shopping_details','"friend" as type');
Any help will be appreciated
thanks
Try with Zend_Db_Expr, maybe something like:
$select->from(array('sh'=>'shopping'),
array('shopping_id','shopping_details',
new Zend_Db_Expr('"friend" as type'));
$select->from(
array('sh'=>'shopping'),
array('shopping_id','shopping_details','friend'=>'type', 'alias'=>'column or expression')
);
For Zend Framework 2/3 or Laminas you have to use Laminas\Db\Sql\Expression. Make sure to quote your constant with a double-quote "".
$select->from(['e' => 'experience'])
->columns([
'id' => 'id',
'value' => 'title',
'name' => new Laminas\Db\Sql\Expression('"skill"')
]);
*for Zend Framework the name of the expression class is Zend\Db\Sql\Expression.