开发者

Getting ./doctrine orm:run-dql to work with the sandbox

So, since no one answered my previous question, I've decided to replicate my problems with the Doctrine Sandbox itself for a better understanding which in return hoping for replies.

My directories (for working and non-working)

|-- library
|   |-- Doctrine
|   |   |-- Common
|   |   |-- DBAL
|   |   |-- ORM
|   |   `-- Symfony
|   `-- MyApp
|       |-- Entities
|       |   |-- Address.php
|       |   `-- User.php
|       `-- Proxies
`-- tools
    `-- sandbox
        |-- cli-config.php
        |-- database.sqlite
        |-- doctrine
        |-- doctrine.php
        |-- index.php
        |-- xml
        |   |-- Entities.Address.dcm.xml
        |   `-- Entities.User.dcm.xml
        `-- yaml
            |-- Entities.Address.dcm.yml
            `-- Entities.User.dcm.yml

Working Sandbox

$ ./doctrine orm:run-dql "SELECT a FROM Entities\Address a"
array(0) {
}

开发者_开发问答Non-Working Sandbox

$ ./doctrine orm:run-dql "SELECT a FROM Entities\Address a"

Fatal error: Cannot redeclare class Entities\MyApp_Entities_Address in /Users/foo/Lab/doctrine/test/library/MyApp/Entities/Address.php on line 7

Both of them are using the same cli-config.php and doctrine.php

cli-config.php

<?php

require_once '../../library/Doctrine/Common/ClassLoader.php';

$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', realpath(__DIR__ . '/../../library'));
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', realpath(__DIR__ . '/../../library'));
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', realpath(__DIR__ . '/../../library'));
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', realpath(__DIR__ . '/../../library/Doctrine'));
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(__DIR__ . '/../../library/MyApp'));
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', realpath(__DIR__ . '/../../library/MyApp'));
$classLoader->register();

$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$driverImpl = $config->newDefaultAnnotationDriver(array(realpath(__DIR__."/../../library/MyApp/Entities")));
$config->setMetadataDriverImpl($driverImpl);

$config->setProxyDir(realpath(__DIR__."/../../library/MyApp/Entities"));
$config->setProxyNamespace('Proxies');

$connectionOptions = array(
    'driver' => 'pdo_sqlite',
    'path' => 'database.sqlite'
);

$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);

$helpers = array(
    'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
    'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
);

doctrine.php

<?php

require_once '../../library/Doctrine/Common/ClassLoader.php';

$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', realpath(__DIR__ . '/../../library'));
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', realpath(__DIR__ . '/../../library'));
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', realpath(__DIR__ . '/../../library'));
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', realpath(__DIR__ . '/../../library/Doctrine'));
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(__DIR__ . '/../../library/MyApp'));
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', realpath(__DIR__ . '/../../library/MyApp'));
$classLoader->register();

// Variable $helperSet is defined inside cli-config.php
require __DIR__ . '/cli-config.php';

$cli = new \Symfony\Component\Console\Application('Doctrine Command Line Interface', Doctrine\Common\Version::VERSION);
$cli->setCatchExceptions(true);
$helperSet = $cli->getHelperSet();
foreach ($helpers as $name => $helper) {
    $helperSet->set($helper, $name);
}
$cli->addCommands(array(
    // DBAL Commands
    new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(),
    new \Doctrine\DBAL\Tools\Console\Command\ImportCommand(),

    // ORM Commands
    new \Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(),
    new \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(),
    new \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(),
    new \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(),
    new \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(),
    new \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(),
    new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(),
    new \Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(),
    new \Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(),
    new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(),
    new \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(),
    new \Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(),
    new \Doctrine\ORM\Tools\Console\Command\RunDqlCommand(),
    new \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(),

));
$cli->run();

The only difference is that, I renamed all the entities for the non-working sandbox based on Zend Framework naming scheme:

Address.php

<?php

namespace Entities;

/** @Entity @Table(name="addresses") */
class MyApp_Entities_Address
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;
    /** @Column(type="string", length=255) */
    private $street;
    /** @OneToOne(targetEntity="MyApp_Entities_User", mappedBy="address") */
    private $user;

    public function getId()
    {
        return $this->id;
    }

    public function getStreet()
    {
        return $this->street;
    }

    public function setStreet($street)
    {
        $this->street = $street;
    }

    public function getUser()
    {
        return $this->user;
    }

    public function setUser(MyApp_Entities_User $user)
    {
        if ($this->user !== $user) {
            $this->user = $user;
            $user->setAddress($this);
        }
    }
}

User.php

<?php

namespace Entities;

/** @Entity @Table(name="users") */
class MyApp_Entities_User
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;
    /** @Column(type="string", length=50) */
    private $name;
    /**
     * @OneToOne(targetEntity="MyApp_Entities_Address", inversedBy="user")
     * @JoinColumn(name="address_id", referencedColumnName="id")
     */
    private $address;

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getAddress()
    {
        return $this->address;
    }

    public function setAddress(MyApp_Entities_Address $address)
    {
        if ($this->address !== $address) {
            $this->address = $address;
            $address->setUser($this);
        }
    }
}

So, what did I do wrong? Where can I fix so that I can run ./doctrine orm:run-dql without any problems?


you get a FAIL when running

./doctrine orm:validate-schema

which is not ok... -> check what is causing that.


With some idea from a user in Doctrine's mailing list, I've changed some codes and it working:

Removed namespace Entities

Address.php

<?php

//namespace Entities;

/** @Entity @Table(name="addresses") */
class MyApp_Entities_Address
{

User.php

<?php

//namespace Entities;

/** @Entity @Table(name="users") */
class MyApp_Entities_User
{

And then, load it up using

$classLoader = new \Doctrine\Common\ClassLoader('MyApp_Entities', realpath(__DIR__ . '/../../library'));
$classLoader->setNamespaceSeparator('_');
$classLoader->register();

Finally, I used this DQL that ran without any errors:

$ ./doctrine orm:run-dql "SELECT a FROM MyApp_Entities_Address a"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜