我已将其安装到我的Codeigniter项目中。现在,我遇到了一个问题,即我无法与数据库进行通信以从中生成我的实体。
来自我的codigniter application/libraries/Doctrine.php
<?php
use DoctrineCommonClassLoader,
DoctrineORMConfiguration,
DoctrineORMEntityManager,
DoctrineCommonCacheArrayCache,
DoctrineDBALLoggingEchoSQLLogger;
/**
*
* 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);
}
}
来自我的`申请/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 DoctrineORMToolsConsoleConsoleRunner;
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 SymfonyComponentConsoleHelperHelperSet(array(
'db' => new DoctrineDBALToolsConsoleHelperConnectionHelper($em->getConnection()),
'em' => new DoctrineORMToolsConsoleHelperEntityManagerHelper($em)
));
DoctrineORMToolsConsoleConsoleRunner::run($helperSet);
通过作曲家安装了学说。因此,我的应用程序文件结构看起来像这样:
{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 */
为了自己的说服力,我将学说添加到application/config/autoload.php
这样,我就可以从控制器访问教义:
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');
}
}
从上述print_r
的输出中,我可以查看第一次创建EntityManager Connection
[_expr:protected] => DoctrineDBALQueryExpressionExpressionBuilder Object
(
[connection:DoctrineDBALQueryExpressionExpressionBuilder:private] => DoctrineDBALConnection Object
*RECURSION*
)
[_isConnected:DoctrineDBALConnection:private] =>
[_transactionNestingLevel:DoctrineDBALConnection:private] => 0
[_transactionIsolationLevel:DoctrineDBALConnection:private] => 2
[_nestTransactionsWithSavepoints:DoctrineDBALConnection:private] =>
[_params:DoctrineDBALConnection:private] => Array
(
[driver] => pdo_mysql
[user] => root
[password] => root
[host] => 127.0.0.1
[dbname] => Inbox
)
[_platform:protected] => DoctrineDBALPlatformsMySqlPlatform Object
(
[doctrineTypeMapping:protected] =>
[doctrineTypeComments:protected] =>
[_eventManager:protected] => DoctrineCommonEventManager Object
(
[_listeners:DoctrineCommonEventManager:private] => Array
(
)
)
[_keywords:protected] =>
)
[_schemaManager:protected] =>
[_driver:protected] => DoctrineDBALDriverPDOMySqlDriver Object
(
)
[_isRollbackOnly:DoctrineDBALConnection:private] =>
[defaultFetchMode:protected] => 2
)
所以问题出现了。正确安装了所有内容,我确实打开了终端并导航到我的Codeigniter项目的根。从那里我输入:
php供应商/bin/doctrine orm:转换 - 映射 - 从数据库注释应用程序/模型
根据帮助功能,这是语法
orm:convert-mapping [ - filter =" ..."] [ - force] [ - from-database] [ - extend [=" ..."]][=" ..."]] [-namespace [=" ..."] to-type dest-path
这是我在终端上打印的内容:
**[DoctrineDBALDBALException]**
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
我感到沮丧。有人可以帮我吗?
$db['default']['username'] = 'root';
$db['default']['password'] = '';
因为本地主机的默认密码将为空白
挖掘问题后,我发现MAMP Pro设置正在引起学说和我的DBMS(MySQL)之间的问题。
解决问题,我尝试了3件不同的事情(我正在运行Mac)
- 添加套接字的快捷方式到TMP文件夹和var/mysql文件夹中
sudo mkdir/var/mysql sudo ln -s /applications/mamp/tmp/mysql/mysql.sock/var/mysql/mysql.sock sudo ln -s/applications/mamp/tmp/mysql/mysql.sock/tmp/mysql.sock
- 将mysql添加到环境变量
Open Terminal Type in: touch ~/.bash_profile; open ~/.bash_profile "this will open a text editor with all the environment variables" Alternative vim ~/.bash_profile Add the following line /Applications/MAMP/Library/bin SAVE CHANGES Type in (into command line) source ~/.bash_profile
- 要从数据库生成实体,我们只需要在我们的终端中键入以下命令
cd到/root/folder
php供应商/bin/doctrine orm:生成范围应用程序/模型/实体
输出=>处理实体"消息"生成"/applications/mamp/htdocs/doctrinetest/application/application/models/entities"的实体类别"
应用程序/模型/实体是我希望找到生成的模型的地方。全部