来自我的Codeigniter应用程序/libraries/Doctrine.php
<?php
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSQLLogger;
/**
*
* How to create advanced configurations
* http://docs.doctrine-project.org/en/2.0.x/reference/configuration.html
*
**/
class Doctrine {
public $em = null;
public function __construct()
{
if (!defined('APPPATH')){
define('APPPATH', 'application/');
}
// load database configuration from CodeIgniter
require_once APPPATH.'config/database.php';
$doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'libraries');
$doctrineClassLoader->register();
$entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
$entitiesClassLoader->register();
$proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
$proxiesClassLoader->register();
//Set up caches
$config = new Configuration;
$cache = new ArrayCache;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models/Entities'));
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
// Proxy configuration
// Sets the directory where Doctrine generates any necessary proxy class files.
$config->setProxyDir(APPPATH.'/models/proxies');
$config->setProxyNamespace('Proxies');
// Set up logger
$logger = new EchoSQLLogger;
$config->setSQLLogger($logger);
$config->setAutoGenerateProxyClasses( TRUE );
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);
}
}
从我的 `application/config/database.php` 文件中
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = '127.0.0.1';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'Inbox';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
从我的 application/cli-config.php
文件中
<?php
// You are missing a "cli-config.php" or "config/cli-config.php" file in your
// project, which is required to get the Doctrine Console working. You can use the
// following sample as a template:
use Doctrine\ORM\Tools\Console\ConsoleRunner;
define('BASEPATH', APPPATH . '/../system/');
// replace with file to your own project bootstrap
require __DIR__ . '/application/libraries/Doctrine.php';
// replace with mechanism to retrieve EntityManager in your app
$entity = new Doctrine();
$doctrine = new Doctrine;
$em = $doctrine->em;
$helperSet = new Symfony\Component\Console\Helper\HelperSet(array(
'db' => new Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
Doctrine是通过Composer安装的。因此,我的应用程序文件结构如下:
{root}
--application/
-----libraries/
---------Doctrine.php
--system/
--vendor/
-----bin/
---------doctrine <-------- this is the command line utility
---------doctrine.php
-----composer/
-----doctrine/
-----symfony/
-----autoload.php
--index.php
autoload.php当前正在index.php中加载。该文件底部如下所示:
/*
* --------------------------------------------------------------------
* LOAD COMPOSER CLASSES
* --------------------------------------------------------------------
*/
include_once 'vendor/autoload.php';
/*
* --------------------------------------------------------------------
* LOAD THE BOOTSTRAP FILE
* --------------------------------------------------------------------
*
* And away we go...
*
*/
require_once BASEPATH.'core/CodeIgniter.php';
/* End of file index.php */
/* Location: ./index.php */
为了方便起见,我将Doctrine添加到application/config/autoload.php
中。这样,我可以像这样从我的控制器访问Doctrine:
class Welcome extends CI_Controller {
public function index()
{
echo "<pre>";
print_r($this->doctrine->em);
//the line above Prints the EntityManager created by Doctrine Library
//with this line -> $this->em = EntityManager::create($connectionOptions, $config);
echo "</pre>";
$this->load->view('welcome_message');
}
}
From the output of the above print_r
i can see which database settings where assigned when i first created the EntityManager connection
[_expr:protected] => Doctrine\DBAL\Query\Expression\ExpressionBuilder Object
(
[connection:Doctrine\DBAL\Query\Expression\ExpressionBuilder:private] => Doctrine\DBAL\Connection Object
*RECURSION*
)
[_isConnected:Doctrine\DBAL\Connection:private] =>
[_transactionNestingLevel:Doctrine\DBAL\Connection:private] => 0
[_transactionIsolationLevel:Doctrine\DBAL\Connection:private] => 2
[_nestTransactionsWithSavepoints:Doctrine\DBAL\Connection:private] =>
[_params:Doctrine\DBAL\Connection:private] => Array
(
[driver] => pdo_mysql
[user] => root
[password] => root
[host] => 127.0.0.1
[dbname] => Inbox
)
[_platform:protected] => Doctrine\DBAL\Platforms\MySqlPlatform Object
(
[doctrineTypeMapping:protected] =>
[doctrineTypeComments:protected] =>
[_eventManager:protected] => Doctrine\Common\EventManager Object
(
[_listeners:Doctrine\Common\EventManager:private] => Array
(
)
)
[_keywords:protected] =>
)
[_schemaManager:protected] =>
[_driver:protected] => Doctrine\DBAL\Driver\PDOMySql\Driver Object
(
)
[_isRollbackOnly:Doctrine\DBAL\Connection:private] =>
[defaultFetchMode:protected] => 2
)
So here comes the problem. Having everything properly installed i did open the terminal and navigated to the root of my codeigniter project. From there i typed:
php vendor/bin/doctrine orm:convert-mapping --from-database annotation application/models
according to the help function this is the syntax
orm:convert-mapping [--filter="..."] [--force] [--from-database] [--extend[="..."]] [--num-spaces[="..."]] [--namespace[="..."]] to-type dest-path
This is what I'm getting printed on my terminal:
**[Doctrine\DBAL\DBALException]**
An exception occurred while executing 'SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'':
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
**[PDOException]**
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
I'm getting frustrated. Can someone help me?