[英]Mysql query in zend framework 2
$dbAdapter = $this->adapter;
$sql = new Sql($dbAdapter);
$sQuery = $sql->select()
->from(array('c' => 'company'), array('name', 'jobtitle', 'experience', 'skill'))
->joinInner(array('j' => 'jobpost'), 'c.cid = j.cid');
In this query, I am getting error: 在此查询中,我收到错误消息:
PHP Fatal error: Call to undefined method Zend\\Db\\Sql\\Select::joinInner(). PHP致命错误:调用未定义的方法Zend \\ Db \\ Sql \\ Select :: joinInner()。
And classes i used in this : 我在此使用的类:
use Zend\\Db\\Adapter\\Adapter; 使用Zend \\ Db \\ Adapter \\ Adapter;
use Zend\\Db\\Sql\\Sql; 使用Zend \\ Db \\ Sql \\ Sql;
use Zend\\Db\\TableGateway\\AbstractTableGateway; 使用Zend \\ Db \\ TableGateway \\ AbstractTableGateway;
use Zend\Db\Sql\Sql;
use Zend\Db\Adapter\Adapter;
$dbAdapterConfig = array(
'driver' => 'Mysqli',
'database' => 'dbname',
'username' => 'dbusername',
'password' => 'dbuserpassword'
);
$dbAdapter = new Adapter($dbAdapterConfig);
$sql = new Sql($dbAdapter);
$sQuery = $sql->select();
$sQuery->from(array('c' => 'company'), array('name', 'jobtitle', 'experience', 'skill'));
$sQuery->joinInner(array('j' => 'jobpost'), 'c.cid = j.cid');
$statement = $sql->prepareStatementForSqlObject($sQuery);
$result = $statement->execute();
Doc: http://framework.zend.com/manual/2.1/en/index.html#zend-db / http://framework.zend.com/manual/2.1/en/modules/zend.db.sql.html Doc: http : //framework.zend.com/manual/2.1/en/index.html#zend-db / http://framework.zend.com/manual/2.1/en/modules/zend.db.sql。 HTML
You should remove joinInner and add join 您应该删除joinInner并添加联接
$dbAdapter = $this->adapter;
$sql = new Sql($dbAdapter);
$sQuery = $sql->select()
->from(array('c' => 'company'), array('name', 'jobtitle', 'experience', 'skill'))
->join(array('j' => 'jobpost'), 'c.cid = j.cid');
By default it will do an Inner join. 默认情况下,它将进行内部联接。 If you want to specify the join then the documentation example from zend
如果要指定联接,则来自zend的文档示例
should make it clear. 应该说清楚。
$select->join(
'foo' // table name,
'id = bar.id', // expression to join on (will be quoted by platform object before insertion),
array('bar', 'baz'), // (optional) list of columns, same requiremetns as columns() above
$select::JOIN_OUTER // (optional), one of inner, outer, left, right also represtned by constants in the API
);
You will also need the two lines Hary added in his answer, 您还需要哈里在回答中添加的两行,
$statement = $sql->prepareStatementForSqlObject($sQuery);
$result = $statement->execute();
and the you can return your result 然后您可以返回结果
return $result->toArray();
返回$ result-> toArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.