2011-01-29 55 views
1

因此,由于没有人回答我以前的问题,我决定复制我的问题与Doctrine沙箱本身,以更好地了解哪些作为回报希望回复。获取./doctrine orm:run-dql与沙盒一起工作

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

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

回答

0

与教义的邮件列表用户的一些想法,我已经改变了一些代码和它的工作:

删除namespace Entities

Address.php

<?php 

//namespace Entities; 

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

用户。 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" 
0

您运行

./doctrine orm:validate-schema 

时,这是不正常得到一个失败... - >检查是什么原因造成这一点。

+0

的错误信息是不是非常有帮助,你有如何的任何想法我可以看到有关错误的其他消息? – amree 2011-01-29 10:00:38