php - 获取 ./doctrine orm :run-dql to work with the sandbox

标签 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 命名方案重命名了非工作沙箱的所有实体:

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

最佳答案

运行失败

./doctrine orm:validate-schema

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

关于php - 获取 ./doctrine orm :run-dql to work with the sandbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4835186/

相关文章:

mysql - 使用大 in() 子句优化 MySQL 查询

php - 在选择框中按名称排序在谷歌浏览器和 IE 中不起作用

php - Preg_replace 用 PHP 和 MySQL,允许破折号和括号

Windows 上的 Mysql 服务未启动

mysql - Rundeck - 使用执行日志和缓存

mysql - Zend Framework 1 查询,按满足条件对结果进行排序

php - 阻止访问页面 Zend Framework

php - 如何获取最后一个完整的 MS SQL Server 错误消息?

php - 类反射 : Missing Properties

php - 使用 php 运行 c 程序