我正在尝试在我的Symfony2项目中创建一个演示系统,以允许为每个“演示”用户创建一个临时sqlite演示数据库,该数据库登录到我正在处理的系统中,以便每个用户都可以访问默认数据集,可以修改数据,而不会干扰登录系统的其他演示用户。
我已经通过定义一个演示连接和一个orm条目来启动它,该条目包含对我想要重定向到config.yml中的临时数据库的所有包的引用,然后在登录时覆盖连接详细信息(更确切地说是数据库路径)登录用户的会话ID。理论上,当他们登录时,他们可以访问一组默认的演示数据,当他们退出时,不会保留任何更改,只有他们才能看到更改。
$这 - > doctrine-> resetManager( '演示');
问题是,当我尝试修改演示连接以指向新路径时,当我调用上面的步骤时(如我发现散布在互联网周围的一些帖子中所述),系统会因错误而死亡我不熟悉,也不确定如何解决。如果有人能帮助我理解出了什么问题,我应该能够解决它,但此刻我已经碰到了一堵砖墙:(
错误输出:http://i.imgur.com/cllbIyy.png
过程是:
这是我的代码:
doctrine:
dbal:
default_connection: default
connections:
demo:
driver: pdo_sqlite
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# Following added to allow importing of existing mysql database. DZH
mapping_types:
enum: string
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
SPSettingsBundle: ~
SPUserBundle: ~
SPCompanyBundle: ~
SPVenueBundle: ~
SPAccessBundle: ~
SPHardwareBundle: ~
SPResellerBundle: ~
SPVouchersBundle: ~
SPTokenBundle: ~
SPAdminBundle: ~
SPSocialBundle: ~
demo:
connection: demo
mappings:
SPUserBundle: ~
SPCompanyBundle: ~
SPVenueBundle: ~
SPAccessBundle: ~
SPHardwareBundle: ~
SPResellerBundle: ~
SPVouchersBundle: ~
SPTokenBundle: ~
SPAdminBundle: ~
SPSocialBundle: ~
SPLegacyPortalBundle:
type: "annotation"
dir: "Entity"
SPLegacyPortalBundle:
type: "annotation"
dir: "Entity/Radius"
<?php
namespace SP\DemoBundle\Listener;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Doctrine\DBAL\Connection;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Console\Input\ArrayInput;
class DemoLoginListener {
private $defaultConnection;
private $doctrine;
private $kernel;
public function __construct(Connection $defaultConnection, Registry $doctrine, KernelInterface $kernel) {
$this->defaultConnection = $defaultConnection;
$this->doctrine = $doctrine;
$this->kernel = $kernel;
}
/**
* Create initial database schema
*/
public function createDbSchema() {
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->kernel);
$application->setAutoExit(false);
// Run initial schema install
$application->run(new ArrayInput(array(
'doctrine:schema:create',
'--em' => 'demo'
)));
}
/**
* Populate database with demo data
*/
public function populateDemoData() {
$query = <<<EOT
INSERT INTO `Company` (`account_manager_id`, `package_id`, `name`, `owner_first_name`, `owner_last_name`, `telephone`, `mobile`, `email`, `venues`, `created_by`, `date_created`) VALUES
(NULL, NULL, 'Demo Company', 'Demo', 'User', NULL, NULL, 'demo@company.com', 'N;', 1, '2013-05-16 15:06:16');
EOT;
$this->defaultConnection->query($query);
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) {
$token = $event->getAuthenticationToken();
$user = $token->getUser();
// Only create demo database with the 'demo' in_memory user logs in
if ('demo' == $user->getUsername()) {
$request = $event->getRequest();
$session = $request->getSession();
if ($session) {
$db_name = sprintf('/tmp/portal_demo_%s.db', $session->getId());
$this->defaultConnection->close();
$reflectionConn = new \ReflectionObject($this->defaultConnection);
$reflectionParams = $reflectionConn->getProperty('_params');
$reflectionParams->setAccessible('public');
$params = $reflectionParams->getValue($this->defaultConnection);
$params['driver'] = 'pdo_sqlite';
$params['dbname'] = $db_name;
$params['path'] = $db_name;
unset($params['host']);
unset($params['port']);
unset($params['password']);
unset($params['user']);
$reflectionParams->setValue($this->defaultConnection, $params);
$reflectionParams->setAccessible('private');
$this->doctrine->resetManager('demo');
// $this->defaultConnection->connect();
}
}
// Create the naked database
// $this->createDbSchema();
// Populate the database with demo data
// $this->populateDemoData();
}
}