How to add Sql\Expression in front of column Name in ZF2? - zend-framework2

How to get Sql like this :
select * from foo where LOWER(foo_name) = 'test';
what i get is if Sql\Expression in right, not in left.

You can user code snippet like that.
$where = new Where();
$sql = new Sql($adapter);
$select = $sql->select();
$where->addPredicate(new Predicate\Expression('LOWER(foo_name) = ?', 'test' ));
$select->from('foo')->where($where);
However I dont think Sql\Expression on right side is possible on Zend Framework 2.

You can do it like this:
$sql = new SQL($adaptor);
$select = $sql->select()->from(array('f'=>'foo'));
$select = $select->where('foo_name' => new \Zend\Db\Sql\Expression("LOWER('test')"));
Above query would return as:
SELECT `f`.* FROM `foo` AS `f` WHERE `foo_name` = LOWER('test');

For others out looking for similar, there are actually quite a few different ways to achieve this as of ZF 2.2
Chaining (same as the accepted answer)
<?php
$sql = new Sql($adapter);
$select = $sql->select();
$select->from( array( 'f' => 'foo' ) )
->where
->addPredicate( new Predicate\Expression( 'LOWER(f.foo_name) = ?', 'test' ) );
//SELECT `f`.* FROM `foo` AS `f` WHERE LOWER(f.foo_name) = :where1
//:where1 = 'test'
?>
Note the absence of the execution command "()" of Select::$where allowing you to continue the method chaining.
Select::$where has a __get Magic method catch which returns the protected Select::$_where property within the Select object which is an instance of Sql\Where.
Predicate\Literal 1
<?php
$select->where( "LOWER(f.foo_name) = 'test'" );
//SELECT `f`.* FROM `foo` AS `f` WHERE LOWER(f.foo_name) = 'test'
?>
Predicate\Literal 2
<?php
$select->where( array( "LOWER(f.foo_name) = 'test'" ) );
//SELECT `f`.* FROM `foo` AS `f` WHERE LOWER(f.foo_name) = 'test'
?>
The two above automatically create a Predicate\Literal object for you if the indexed value (column identifier) of the array or argument supplied to the Select::where method is a string.
Predicate\Expression (manual)
<?php
$select->where( new Predicate\Expression( "LOWER(f.foo_name) = 'test'" ) );
//SELECT `f`.* FROM `foo` AS `f` WHERE LOWER(f.foo_name) = 'test'
?>

Related

Zend Framework2 how to make Concat and Left Join

Does not work:
$sql = new Sql($this->adapter);
$select = $sql->select();
$select->from('request')
->columns(array('*', new Expression("CONCAT(up1.value,' ',up2.value) as display_name")))
->join(array('up1'=>'user_profile'), "up1.user_id = request.request_user_id AND up1.key = 'user_first_name'", array('up1.value'), 'left')
->join(array('up2'=>'user_profile'), "up2.user_id = request.request_user_id AND up2.key = 'user_last_name'", array('up2.value'), 'left')
;
return $select;
How to make the right?
You specified fields for both joins:
->join(array('up1'=>'user_profile'), "up1.user_id = request.request_user_id AND up1.key = 'user_first_name'", array('up1.value'), 'left')
->join(array('up2'=>'user_profile'), "up2.user_id = request.request_user_id AND up2.key = 'user_last_name'", array('up2.value'), 'left')
When query is converted to actual sql, these fields will be automatically namespaced, so you will get "up1.up1.value" in fields list.
Remove fields references from joins and it should work.
UPD. Right, there's more to it. You can't pass 'user_first_name' as a string value to the "on" parameter of join as it will be interpreted as a column name. So you have to pass an expression:
$select = $sql->select();
$select->from('request')
->columns(array('*', new Expression('CONCAT(up1.value,"#",up2.value) as display_name')));
$expressionString = '? = ? AND ? = ?';
$types = array(Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE);
$parameters = array('request.user_id', 'up1.user_id', 'up1.key', 'first_name');
$expression1 = new Expression($expressionString, $parameters, $types);
$parameters = array('request.user_id', 'up2.user_id', 'up2.key', 'last_name');
$expression2 = new Expression($expressionString, $parameters, $types);
$select->join(array('up1'=>'user_profile'), $expression1, array('value'), 'left')
->join(array('up2'=>'user_profile'), $expression2, array('value'), 'left');
Y tri to:
$select = $sql->select();
$select->from('request')
->columns(array('*', new Expression('CONCAT(up1.value,"#",up2.value) as display_user_name')));
$expressionString = '? = ? AND ? = ?';
$types = array(Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE);
$parameters1 = array('request.user_id', 'up1.user_id', 'up1.key', 'user_first_name');
$expression1 = new Expression($expressionString, $parameters1, $types);
$parameters2 = array('request.user_id', 'up2.user_id', 'up2.key', 'user_last_name');
$expression2 = new Expression($expressionString, $parameters2, $types);
$select->join(array('up1'=>'user_profile'), $expression1, array('value'), 'left')
->join(array('up2'=>'user_profile'), $expression2, array('value'), 'left');
does not work, where the error

How to use multiple on clause in joining in zend framework 2

I am doing like this sql into zend framework sql pattern.
SELECT
jobs . *,
c.id AS cid,
c.name AS name,
c.companyImage AS companyImage,
c.logo AS logo,
count(app.userId) AS t_app,
app.applyStatus AS applyStatus,
app.userId AS appUserId
FROM
jobs
LEFT JOIN
companies AS c ON jobs.companyName = c.id
LEFT JOIN
applicants AS app ON jobs.id = app.jobId AND app.applyStatus = 1
WHERE
jobs.ownerId = 16 AND jobs.draftId != 0
GROUP BY jobs.id
ORDER BY jobs.id DESC
LIMIT 3
For this sql I already write this code for zend framework 2
$adapter = $this->tableGateway->getAdapter();
$sql = new Sql($adapter);
$select = $sql->select();
$select->from('jobs')
->join(array('c' => 'companies'), 'jobs.companyName = c.id', array('cid' => 'id', 'name', 'companyImage', 'logo'), 'left')
->join(array('app' => 'applicants'), ' jobs.id = app.jobId AND app.applyStatus = 1', array('t_app' => new Expression('count(app.userId)'), 'applyStatus', 'appUserId' => 'userId'), 'left')
->where("jobs.ownerId ={$userId} AND jobs.draftId != 0")
->group('jobs.id')
->order('jobs.id DESC')
->limit(3);
$statement = $sql->getSqlStringForSqlObject($select);
$results = $adapter->query($statement, $adapter::QUERY_MODE_EXECUTE);
but does not work properly and its give a message like below.
SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'on clause'
The issue is this part:
app.applyStatus = 1
The framework is escaping 1 as if it were a column name, 1.
You need to enclose this part in an Expression too
new Expression('jobs.id = app.jobId AND app.applyStatus = 1')
I think the use of Expressions in the 'ON' parameter of the join method may depend on the version of ZF2 you are using, I think it was added 2.1+
Building on this answer. If you also want your table & column identifiers to be escaped, use this syntax:
use Zend\Db\Sql\Expression;
...
$onExpression = new Expression('? = ? AND ? = ?',
['jobs.id', 'app.jobId', 'app.applyStatus', 1],
[Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER,
Expression::TYPE_IDENTIFIER, Expression::TYPE_LITERAL]
);
$select->from('jobs')
->join(array('app' => 'applicants'), $onExpression, array('t_app' => new Expression('count(app.userId)'), 'applyStatus', 'appUserId' => 'userId'), 'left');
The Expression constructor accepts the string, then arguments, then argument types.
public function __construct($expression = '', $parameters = null, array $types = [])
This will create a security issue. Zf2 changes your query to this:
Select * from tableA inner join tableB
on `tableA`.`column` = `tableB`.`column`
AND `tableB`.`column` = `1`
It adds
`
to each part for security issues! By using new Expression you are bypassing it and if you get applyStatus from user entry, get sure about its filtering!

How to get "not equal" in Where?

I have something like this:
$sql = new Sql($this->tableGateway->getAdapter());
$select = $sql->select();
$select
-> from('mytable')
-> join('exp', 'field1_id=field1.id', '*', 'LEFT');
$where = new Where();
$where -> equalTo('field2_id', $field2_id);
$select->where($where);
$statement = $sql->prepareStatementForSqlObject($select);
but what if I want use "not equalTo"? How to do it?
You need to use notEqualTo
$where->notEqualTo('field2_id', $field2_id);
http://framework.zend.com/apidoc/2.1/classes/Zend.Db.Sql.Predicate.Predicate.html#notEqualTo
This is how I am using it, most of us might be searching for such querystring, having such clasues in a single query.
return $details = $this->select(function(Select $select) use ($domain, $domainWithoutWWW){
$select->columns(['dealername'=> 'dealer.name']);
$select->join('template',"dealer.template_id = template.id", ['template_name' => 'name','template_html' => 'html','template_css' => 'css'], 'left');
$select->where($select->where->notEqualTo('dealer.used_car_dealer_id',1826));
$select->where(array('dealer.parent' => 0));
$select->where(new In('domain', ['abc.com', 'xyz.com', 'lmn.com']));
});
The simplest way is to write the condition inline:
$select->where('field1 != 0');

count in sub-query in zend framework 2

I am new bee of Zf2, so please suggest how should I create following sql in Zend framework 2?
Select mt1.*,
(select count(mt2.parent_id)
from md_type as mt2
where mt2.parent_id = mt1.id)) as cnt
from md_type as mt1
You can try this:
$sub = new Select('md_type');
$sub->columns(array(new Expression('COUNT(mt2.parent_id) as total')))
->where(array(
new \Zend\Db\Sql\Predicate\Expression('mt2.parent_id = mt1.id')
))
;
$subquery = new \Zend\Db\Sql\Expression("({$sub->getSqlString()})");
$select = new \Zend\Db\Sql\Select('mt1');
$select->columns(array('*', 'cnt' => $subquery));
this would produce:
SELECT mt1.*,
(SELECT COUNT(mt2.parent_id) as total
FROM "md_type"
WHERE mt2.parent_id = mt1.id
) AS cnt
FROM mt1
Please try this
$sql = new Sql($this->_adapter);
$mainSelect = $sql->select()->from('mt1');
$subQry = $sql->select()
->from('md_type')
->columns(array('orderCount' => new \Zend\Db\Sql\Expression('COUNT(md_type.parent_id)')))
->where('mt2.parent_id = mt1.id');
$mainSelect->columns(
array(
'id',
'total' => new \Zend\Db\Sql\Expression('?', array($subQry)),
)
);
$statement = $sql->prepareStatementForSqlObject($mainSelect);
$comments = $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($comments);
return $resultSet->toArray();
Reference: ZF2 - subqueries

ZF2 tableGateway select

I started with the ZendSkeletonApplication and added a model extending Zend\Db\TableGateway\TableGateway.
I have the following method:
public function findByType($type) {
$rowset = $this->select('type' => $type);
return $rowset;
}
This works, but now if i do this:
$foo = $table->findBytype('foo');
$bar = $table->findBytype('bar');
the first one works, the query it executes is:
SELECT * FROM table WHERE 'type' = 'foo'
The second one however executes the following query:
SELECT * FROM table WHERE 'type' = 'foo' AND 'type' = 'bar'
is this expected behavior?
If so how can i have the second time i call the method execute the following query:
SELECT * FROM table WHERE 'type' = 'bar'
thanks in advance!
Should use select in tableGateway like this:
$select = $this->getSql()->select();
$select->where(array('type' => 'foo'))
->where(array('type' => 'bar'));
$rowset = $this->selectWith($select);
select() will be reset the where() paramters when you call it next time.
See more usage in my blog:
http://avnpc.com/pages/advanced-database-select-usage-in-zf2

Resources