[英]How to load an Entity from another Bundle on Symfony2
Let's say I have two Bundles : 假设我有两个捆绑包:
Compagny\\InterfaceBundle
Compagny\\UserBundle
How can I load an Entity of UserBundle in the controller of InterfaceBundle ? 如何在InterfaceBundle的控制器中加载UserBundle实体?
The Controller
of my Compagny/InterfaceBundle
: 我的
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();
}
}
The Entity
of my Compagny/UserBundle
: 我的
Compagny/UserBundle
的Entity
:
<?php
namespace Compagny\UserBundle\Entity
class User {
public $name;
public function setName($name) {
// ...
}
public function getName() {
// ...
}
}
(Let's says for this example that the User class doesn't use Doctrine2, because it doesn't need to connect to the database). (我们在这个例子中说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();
}
}
You can of course, you are just using a class from another namespace. 当然,您可以使用其他命名空间中的类。 The fact that it is an entity is not important at all!
事实上它是一个实体并不重要! You can of course query the entity manager for that entity as well.
您当然可以查询该实体的实体管理器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.