1

I want to access a PostgreSQL Database with Doctrine 2 in a Zend 2 Application, but I get the error, that the table I want to access is not defined.

The same access on a MySQL Database works fine.

My Doctrine configuration (local.php) is:

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',
            ),
        ),
    ),
  ),
);

My Controller tries to display an Entity "Horse":

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,
    ));
  }
}

The Entity Horse is:

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)

When trying to access PostgreSQL I get this error:

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...

I have a PostgreSQL Database "mydb" with the schema "public" and the table "Horse"

-- Table: "Horse"

CREATE TABLE "Horse"
(
  id serial NOT NULL,
  .
  .
  . 
);

exists and can be accessed using pgAdminIII.

Any help is welcome!

Best regards rholtermann

1 Answer 1

1

Found the answer, now it works.

I just had to add the schema and had to escape it a little:

In my Horse-Entity:

@ORM\Table("Horse")

had to be replaced with:

@ORM\Table(name="""public"".""Horse""")

I got the hint from here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.