除了使用构造函数注入之外,还有另一种设置DbAdapter
和表名的方法吗?TableGateway
我通过扩展AbstractTableGateway
下面的类来实现它,但我想将此逻辑移动到顶级配置的某个地方并摆脱它,因为它是不必要的。
namespace Application\Repository;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\AdapterAwareInterface;
use Zend\Db\Adapter\Adapter;
abstract class AbstractRepository extends AbstractTableGateway implements AdapterAwareInterface
{
public function setDbAdapter(Adapter $adapter) {
$this->table = preg_replace('/.*\\\([a-zA-Z]+)Repository/', '$1', get_class($this));
$this->table = strtolower($this->table);
$this->adapter = $adapter;
$this->initialize();
}
}