假设我有两个bundle:
-
Compagny'InterfaceBundle
-
Compagny'UserBundle
如何在InterfaceBundle的控制器中加载UserBundle的实体?
我的Compagny/InterfaceBundle
的Controller
:
<?php
// src/Compagny/InterfaceBundle/Controller/DefaultController.php
namespace Compagny'InterfaceBundle'Controller;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Compagny'UserBundle'Entity; // I believed this line will do the trick, but it doesn't
class DefaultController extends Controller
{
public function indexAction()
{
$user = new User();
}
}
my Compagny/UserBundle
的Entity
:
<?php
namespace Compagny'UserBundle'Entity
class User {
public $name;
public function setName($name) {
// ...
}
public function getName() {
// ...
}
}
(在这个例子中,我们假设User类不使用Doctrine2,因为它不需要连接到数据库)
<?php
// src/Compagny/InterfaceBundle/Controller/DefaultController.php
namespace Compagny'InterfaceBundle'Controller;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Compagny'UserBundle'Entity'User; // It's not a trick, it's PHP 5.3 namespacing!
class DefaultController extends Controller
{
public function indexAction()
{
$user = new User();
}
}
当然可以,你只是使用了另一个命名空间中的类。它是一个实体的事实一点也不重要!当然,您也可以查询该实体的实体管理器