如何将Doctrine Entity Manager包括在视图生成的文件中



我使用Mini PHP MVC。bootstrap的变量不包括在生成的view中。我可以将entityManager包括到index.php,但是我无法包括entityManager来查看文件。为什么?如何将Doctrine Entity Manager包括在视图生成的文件中?

如何将Entity Manager包括到下面的_register1Db.php文件?该视图正确显示,它没有显示出bootstrap.php的错误(如果我chaneg路径,显示此类错误),但是视图不包括Bootstrap.php

的Entity Manager。

//1。 src bootstrap.php

use DoctrineORMToolsSetup;
use DoctrineORMEntityManager;
$isDevMode = true;
$entitiesPaths = array(__DIR__.'/CrmBundle/Entity');
$dbParams = array(
  'dbname' => 'dbname',
  'user' => 'user',
  'password' => 'pswd',
  'host' => 'localhost',
  'driver' => 'pdo_mysql',
);   // 'pdo_mysql',

$config = Setup::createAnnotationMetadataConfiguration($entitiesPaths, $isDevMode);
$em = DoctrineORMEntityManager::create($dbParams, $config);
$some='some';

//2。 public index.php

require_once '../src/bootstrap.php'; //which requires autoload.php
print_r('<br><br> index some='.$some); // works
var_dump($em);  //works
use coreApp;
$app = new App(); //App from urk identifines controller and action and pass url paramteters to them, controller action analize parameters and displays views, in this case  securityregister 

//3。src crmbundle resources views security security register.php

include ('_register1Db.php');

//4。src crmbundle resources views security_register1db.php

require_once '/../../../../bootstrap.php';  // 
var_dump($em);  // Undefined variable: em 
print_r('<br><br> register some='.$some); //Undefined variable: some

//我生成视图的方式:src core view.php

ob_start();
include_once( __DIR__.'/../bootstrap.php' );
include_once( $this->viewTemplatePath.$this->file );
ob_end_flush();  

//从bootstrap.php:src core view.php

的调试变量查看生成
    include_once( __DIR__.'/../bootstrap.php' );
    ob_start();
    // think how to render extract variable in order to pass to the file
    include_once( __DIR__.'/../bootstrap.php' );  
    var_dump($em);  //undefined
    print_r('<br><br> parseViewPhpinside some=' . $some); //undefined
    print_r('<br><br> parseViewPhpinside __DIR__=' . __DIR__);  // ....apache2htdocsownlogsrccore 
    include_once( $this->viewTemplPath.$this->file );
    if($store) return ob_get_clean(); 
    else ob_end_flush();
    var_dump($em);  //undefined
    print_r('<br><br> parseViewPhp some='.$some);  //undefined

它是使事情起作用的解决方案,但不能答案。

解决方案是创建一个文件dbcon.php并将其包含在register.phpregister1Db.php中。

//dbcon.php的内容

use DoctrineORMToolsSetup;
use DoctrineORMEntityManager;
$isDevMode = true;
$entitiesPaths = array(__DIR__.'/CrmBundle/Entity');
$dbParams = array(
  'dbname' => 'dbname',
  'user' => 'user',
  'password' => 'pswd',
  'host' => 'localhost',
  'driver' => 'pdo_mysql',
);   // 'pdo_mysql',
$config = Setup::createAnnotationMetadataConfiguration($entitiesPaths, $isDevMode);
$em = DoctrineORMEntityManager::create($dbParams, $config);
print_r('<br><br> end of dbcon.php ');

我不明白,如果我包括bootsrap.php,为什么不工作?但是,如果我在另一个文件中包含类似的内容,则可以使用。

也许是因为我已经在index.php中包含了一次,该index.php创建了object app.php,显示查看文件,而所说的视图文件不包括bootsrap第二次,因为看起来已经包含了bootstrap,尽管变量已包含不可用?

原因是什么?

最新更新