【问题标题】:Getting ./doctrine orm:run-dql to work with the sandbox让 ./doctrine orm:run-dql 与沙箱一起工作
【发布时间】:2011-01-29 04:38:23
【问题描述】:

因此,由于没有人回答我之前的问题,我决定将我的问题复制到 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

【问题讨论】:

    标签: php mysql zend-framework doctrine-orm


    【解决方案1】:

    运行时出现 FAIL

    ./doctrine orm:validate-schema
    

    这不好... -> 检查是什么原因造成的。

    【讨论】:

    • 错误消息不是很有帮助,您知道我如何查看有关错误的其他消息吗?
    • 对我来说,这看起来像您的学说架构(在文件夹实体的 php 文件中定义)并且您的数据库中可用的内容不匹配。
    • 有点猜想,大声笑,但问题是我已经检查了所有内容(很可能我错过了一些东西),但我的数据库似乎与我的实体相同。我实际上已经在 Doctrine 中打开了一张票,希望他们能提供更多有用的信息(例如不同步的地方)。
    • 刚刚设法让 orm:validate-schema 上的一切正常,但我仍然遇到 orm:run-dql 相同的错误,所以,我认为它不相关
    【解决方案2】:

    根据 Doctrine 邮件列表中的用户的一些想法,我更改了一些代码并且它可以工作:

    删除namespace Entities

    地址.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"
    

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 2013-01-02
      • 2016-12-13
      • 2016-10-06
      • 2011-11-30
      • 2014-02-19
      • 2011-09-03
      相关资源
      最近更新 更多