2017-02-03 41 views
0

我在想,如果有,我可以初始化FOSUserBundle的實體用戶的業主的方式,使其包含誰創造了郵政Symfony的3獲得當前用戶內部實體

我想這樣做的用戶在構造函數內部如下所示。

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Table(name="post") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository") 
*/ 
class Post 
{ 
    /* here are defined some attributs */ 
    /** 
    * @ORM\ManyToOne(targetEntity="User", inversedBy="posts") 
    * @ORM\JoinColumn(name="owner", referencedColumnName="id") 
    */ 
    private $owner; 

    public function __construct() 
    { 
    $this->owner = /* get current user */ ; 
    } 

} 

有沒有辦法通過用構造函數替換註釋來做到這一點?

謝謝您的回答

+0

主義實際上並未使用的構造函數,因此沒有。爲什麼不直接在Post和User之間設置關係? – Cerad

+1

關係已經存在,當創建一個新帖子時,所有者屬性將使用當前連接的用戶進行初始化 – paris93

+0

因此,您只需在首次創建新發布時初始化所有者?如果是這樣,下面的兩個答案都是有效的。就我個人而言,我會採用工廠方法,可能會將創建方法添加到發佈存儲庫而不是獨立類。但是列出的所有方法都是有意義的。 – Cerad

回答

3

我認爲你是過度複雜化這個問題。當你在你的控制器創建一個新的帖子,無論是在控制器或在倉庫中做這樣的事情:

use AppBundle\Entity\Post; //at top of controller 

$em = $this->getDoctrine()->getManager(); 
$user = $this->container->get('security.token_storage')->getToken()->getUser(); 
$post = new Post(); 
$em->persist($post); 
$post->setOwner($user); 
// set other fields in your post entity 
$em->flush(); 
4

不,沒有。 [*]

至少有兩種方法可以解決這個問題:

  • 創建安置自己的實體,通過它填充 owner財產廠服務:

    namespace My\Bundle\EntityFactory; 
    
    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; 
    use My\Bundle\Entity\Post; 
    
    class PostFactory 
    { 
        private $tokenStorage; 
    
        public function __construct(TokenStorageInterface $tokenStorage) 
        { 
         $this->tokenStorage = $tokenStorage; 
        } 
    
        public function createPost() 
        { 
         $user = $this->tokenStorage()->getToken()->getUser(); 
         $post = new Post($user); 
        } 
    } 
    

    (用於這個例子,你將不得不修改你的Post構造函數爲 接受所有者作爲參數)

    在services.yml:

    services: 
        post_factory: 
         class: My\Bundle\EntityFactory\PostFactory 
         arguments: [@security.token_storage] 
    

    從您的控制器創建一個實體:

    $post = $this->container->get('post_factory')->createPost(); 
    
  • 如果你能忍受,一旦你堅持下去的 實體店主纔會被置,你可以使用教義事件監聽器:

    namespace My\Bundle\EventListener; 
    
    use Doctrine\ORM\Event\LifecycleEventArgs; 
    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; 
    use My\Bundle\Entity\Post; 
    
    class PostOwnerAssignmentListener 
    { 
        private $tokenStorage; 
    
        public function __construct(TokenStorageInterface $tokenStorage) 
        { 
         $this->tokenStorage = $tokenStorage; 
        } 
    
        public function prePersist(LifecycleEventArgs $event) 
        { 
         $entity = $event->getEntity(); 
         if ($entity instanceof Post && !$entity->getOwner()) { 
          $entity->setOwner($this->tokenStorage->getToken()->getUser()); 
         } 
        } 
    } 
    

    In services.yml:

    services: 
        post_owner_assignment_listener: 
         class: My\Bundle\EventListener\PostOwnerAssignmentListener 
         arguments: [@security.token_storage] 
         tags: 
          - { name: doctrine.event_listener, event: prePersit } 
    

    的優勢在這裏是用老闆不管如何以及在哪裏創建 的Post分配。

[*]:當然,從技術上與默認app.php你可以通過聲明在構造函數global $kernel;訪問 內核,並從那裏走, 然而,這是非常強烈反對,並在陌生的可能斷裂和細微的 方式。

+0

其更有用謝謝 – ALWAN