我想在Zend 2应用程序中使用Doctrine 2访问PostgreSQL数据库,但我得到了一个错误,即我想要访问的表没有定义。
对MySQL数据库进行相同的访问效果良好。
我的条令配置(local.php)是:
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
// MySQL - Config
// 'driverClass' => 'Doctrine'DBAL'Driver'PDOMySql'Driver',
// 'params' => array(
// 'host' => '192.168.1.100',
// 'port' => '3306',
// 'user' => 'user',
// 'password' => 'password',
// 'dbname' => 'mydb',
// 'charset' => 'utf8',
// ),
// PostgreSQL
'driverClass' => 'Doctrine'DBAL'Driver'PDOPgSql'Driver',
'params' => array(
'host' => '192.168.1.100',
'port' => '5432',
'user' => 'user',
'password' => 'password',
'dbname' => 'mydb',
'charset' => 'utf8',
),
),
),
),
);
我的控制器试图显示实体"马":
class HorsesController extends AbstractActionController
{
/**
* @var Doctrine'ORM'EntityManager
*/
protected $em;
public function getEntityManager()
{
if (null === $this->em) {
$this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
}
return $this->em;
}
/**
* The default action - show the home page
*/
public function indexAction()
{
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
$horse = $this->getEntityManager()->find('Doctrine2mapper'Entity'Horse', $id);
return new ViewModel(array(
'horse' => $horse,
));
}
}
实体马是:
use Doctrine'ORM'Mapping as ORM;
use Doctrine'Common'Collections'ArrayCollection;
use Doctrine'Common'Collections'Collection;
use Zend'Form'Annotation; // !!!! Absolutely neccessary
use Zend'Db'Sql'Ddl'Column'BigInteger;
/**
* Horses
*
* @ORM'Table(name="Horse")
* @ORM'Entity(repositoryClass="Doctrine2mapper'Entity'Repository'HorseRepository")
* @Annotation'Name("Horse")
* @Annotation'Hydrator("Zend'Stdlib'Hydrator'ClassMethods")
*/
class Horse
{
/**
* @var integer
*
* @ORM'Column(name="id", type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="IDENTITY")
* @Annotation'Exclude()
*/
private $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
.
.
.
(many attributes, getter and setter)
当试图访问PostgreSQL时,我得到了这个错误:
An exception occurred while executing 'SELECT t0.id AS id1, ...
FROM Horse t0 WHERE t0.id = ?' with params [1]:
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "horse" does not exist
LINE 1: ...ated AS created29, t0.modified AS modified30 FROM Horse t0 W...
我有一个PostgreSQL数据库"mydb",模式为"public",表为"Horse"
-- Table: "Horse"
CREATE TABLE "Horse"
(
id serial NOT NULL,
.
.
.
);
存在,并且可以使用pgAdminIII进行访问。
欢迎任何帮助!
致以最良好的问候rholtermann
找到了答案,现在它工作了。
我只需要添加模式,并对其进行一点转义:
在我的马实体:
@ORM'Table("Horse")
必须替换为:
@ORM'Table(name="""public"".""Horse""")
我从这里得到了提示