我想在 zf2 中执行自定义查询。现在我有一个 Album Controller 和 AlbumTable。在 AlbumTable 中,我想执行一个连接操作。但我无法做到这一点。请给我一些建议。
在我的代码下面:
namespace WebApp\Table;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Sql;
new Zend\Db\Adapter\Adapter;
class UserTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function searchUser($search)
{
$search = "mehedi";
$adapter = new Adapter();
$sql = new Sql($adapter);
$select = $sql->select();
$select->from('foo');
$select->join('profiles', 'user.user_id = profiles.ownerId', array('name'));
$select->where(array('id' => 2));
$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
return $results;
}
}
最佳答案
问题是您试图在不带参数的情况下实例化您的适配器,而它至少需要一个驱动程序:
$adapter = new Adapter(); // Bad
$adapter = new Adapter($driver); // ..
您应该使用 ServiceManager 来获取您的适配器,您是从骨架应用程序开始的吗?
它应该已经为您注入(inject)到 TableGateway 中了..
$adapter = $this->getAdapter();
实例化适配器的示例:
$config = $serviceLocator->get('Config');
$adapter = new Adapter($config['db']);
在配置中指定设置的地方,local.php 将执行以下操作:
return array(
/**
* Database Config
*/
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=dbname;host=localhost',
'username' => 'root',
'password' => 'password',
),
关于mysql - 使用 zend 框架 2 编写自定义查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15222197/