获取./doctrine orm:run-dql以使用沙箱

时间:2011-01-29 04:38:24

标签: php mysql zend-framework doctrine-orm

所以,既然没有人回答我之前提出的问题,我决定用Doctrine Sandbox本身复制我的问题,以便更好地理解,希望能够回复。

我的目录(工作和非工作)

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

工作沙箱

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

非工作沙箱

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

他们都使用相同的cli-config.phpdoctrine.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();

唯一的区别是,我基于Zend Framework命名方案重命名了非工作沙箱的所有实体:

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

那么,我做错了什么?我可以在哪里修复,以便我可以毫无问题地运行./doctrine orm:run-dql

2 个答案:

答案 0 :(得分:0)

运行时出现失败

./doctrine orm:validate-schema

哪个不行...... - &gt;检查导致这种情况的原因。

答案 1 :(得分:0)

根据Doctrine邮件列表中的用户的一些想法,我已经更改了一些代码并且工作正常:

删除了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
{

然后,使用

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

最后,我使用了这个没有任何错误的DQL:

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