2011-11-18 12 views
5
의 다른 번들에서 엔터티를로드하는 방법

이의 내가 두 번들 있다고 가정 해 봅시다 ? 내 Compagny/InterfaceBundleSymfony2

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(); 
    } 
} 

EntityCompagny/UserBundle :

<?php 

namespace Compagny\UserBundle\Entity 

class User { 
public $name; 
public function setName($name) { 
    // ... 
} 
public function getName() { 
    // ... 
} 
} 

(은 '아무튼 때문에, 이제 사용자 클래스가 Doctrine2를 사용하지 않는이 예라고하자 데이터베이스에 연결할 필요가 없음).

답변

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(); 
    } 
} 

물론 다른 네임 스페이스의 클래스를 사용할 수 있습니다. 엔티티라는 사실은 전혀 중요하지 않습니다! 물론 해당 엔터티에 대한 엔터티 관리자를 쿼리 할 수도 있습니다.