假设我有两个捆绑包:
Compagny\InterfaceBundle
Compagny\UserBundle
如何在InterfaceBundle的控制器中加载UserBundle实体?
Controller
的{{1}}:
Compagny/InterfaceBundle
<?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();
}
}
的{{1}}:
Entity
(我们在这个例子中说User类不使用Doctrine2,因为它不需要连接到数据库)。
答案 0 :(得分:12)
<?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();
}
}
您当然可以使用其他命名空间中的类。事实上它是一个实体并不重要!您当然也可以查询该实体的实体管理器。