我是Zend Framework和php的新手.
我浏览了Zend Framework 2教程,并尝试使用AbstractTableGateway查询多个表.
但是在网页上得到以下消息:
提供的选择对象的表名必须与表的名称相匹配
这是我的代码的一部分:
类PublicationTable扩展了AbstractTableGateway {
protected $table = 'publication';
public function fetchAll()
{
$sql = new Sql($this->adapter);
$select = $sql->select();
$select->from(array('p' => 'publication'))
->join('author','publication_fk=p.publication_pk');
$resultSet = $this->selectWith($select);
return $resultSet;
}
...
Run Code Online (Sandbox Code Playgroud)
}
我知道变量"protected $ table"是一个String.那怎么能解决这个问题呢?谢谢您的帮助!
EC
该from()方法采用表名,而不是列列表.使用columns()指定你想要的列.我从来没有试过过TableGateway,虽然好像你正在加入,但TableGateway并不是最好的模式.
如果您直接使用DbAdapater,那么这样的事情应该有效:
use Zend\Db\Sql\Select,
Zend\Db\ResultSet\ResultSet;
$select = new Select;
$select->from('publication')
->join('author', 'publication.publication_pk = author.publication_fk',
array('columnnamefromauthortable1', 'columnnamefromauthortable2'));
$statement = $adapter->createStatement();
$select->prepareStatement($adapter, $statement);
$resultSet = new ResultSet();
$resultSet->initialize($statement->execute());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4925 次 |
| 最近记录: |