2014-11-06 64 views
0

我post.php中爲什麼連接表在symfony2中不起作用?

<?php 
// src/Acme/UserBundle/Entity/User.php 

namespace Acme\PostBundle\Entity; 


use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 

/** 
* @ORM\Entity 
* @ORM\Table(name="posts") 
*/ 
class Post 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * 
    * @ORM\Column(type="integer") 
    */ 
    public $user_id; 
    /** 
    * @ORM\ManyToOne(targetEntity="Acme\PostBundle\Entity\User", inversedBy="posts") 
    * @ORM\JoinColumn(name="id", referencedColumnName="id") 
    */ 
    protected $user; 
    /** 
    * @ORM\Column(type="string", length=255) 
    * @Assert\NotBlank(message="Введите текст") 
    *) 
    */ 
    protected $text; 

    /** 
    * @ORM\Column(type="string", length=255) 
    */ 
    protected $address; 
    /** 
    * 
    * @ORM\Column(type="datetime") 
    */ 
    protected $date; 



} 

而且我user.php的:

<?php 
// src/Acme/UserBundle/Entity/User.php 

namespace Acme\PostBundle\Entity; 


use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity 
* @ORM\Table(name="users") 
*/ 
class User 
{ 

    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 
    /** 
    * 
    * @ORM\Column(type="string") 
    */ 
    protected $path; 

    /** 
    * @ORM\OneToMany(targetEntity="Acme\PostBundle\Entity\Post", mappedBy="users") 
    */ 
    protected $posts; 

    public function __construct() { 

     $this->posts = new ArrayCollection(); 

    } 


} 

我想通過做表 '崗位' 列 'user_ID的' 連接表用戶和帖子。接下來我讓查詢中使用的QueryBuilder:

$repository = $this->getDoctrine()->getManager()->getRepository('AcmePostBundle:Post'); 


    $queryPosts = $repository->createQueryBuilder('p') 
        ->orderBy('p.id', 'DESC') 
        ->getQuery(); 

    return new Response(var_dump($queryPosts->getResult())); 

而我得到:

object(Acme\PostBundle\Entity\Post)[476] 
     protected 'id' => int 1 
     public 'user_id' => int 1 
     protected 'user' => 
     object(Proxies\__CG__\Acme\PostBundle\Entity\User)[475] 
      public '__initializer__' => 
      object(Closure)[469] 
       ... 
      public '__cloner__' => 
      object(Closure)[470] 
       ... 
      public '__isInitialized__' => boolean false 
      protected 'id' => int 1 
      protected 'path' => null 
      protected 'posts' => null 
     protected 'text' => string 'Test' (length=4) 
     protected 'address' => string 'Test' (length=4) 
     protected 'date' => 
     object(DateTime)[477] 
      public 'date' => string '2014-11-06 14:39:13' (length=19) 
      public 'timezone_type' => int 3 
      public 'timezone' => string 'Europe/Kiev' (length=11) 

爲什麼用戶>路徑爲空?如何使使這個連接表

使用輔助功能時

回答

1

學說利用延遲加載的方法,如find()findAll()其獲取需求相關的實體,但是當你使用查詢生成器,你正在創建明確的查詢/結果這不會被Doctrine的lazy-loader處理。

在使用查詢生成器,你必須明確地加入其他相關實體:

$queryPosts = $this->getDoctrine()->getManager()->createQueryBuilder() 
       ->select('p, u') 
       ->from('AcmePostBundle:Post', 'p') 
       ->leftJoin('p.user','u') // handles the ManyToOne association automatically 
       ->orderBy('p.id', 'DESC') 
       ->getQuery(); 

需要注意的是,你可以很容易地使用innerJoin代替leftJoin,但爲安定,若您有任何潛在的Post項目有空關聯。

你也必須確保你的實體映射是正確的:

/** 
* @ORM\ManyToOne(targetEntity="Acme\PostBundle\Entity\User", inversedBy="posts") 
* @ORM\JoinColumn(name="user_id", referencedColumnName="id") 
*/ 
protected $user; 

不然原則將嘗試後的id對陣用戶的id。 (類似LEFT JOIN Post ON Post.id = User.id到達原始查詢時)

+0

它的工作,但現在我得到這個:http://pastebin.com/C9NUXcwT – engilexial 2014-11-06 18:23:30

+0

@ivaaaan編輯你的問題,但是你應該做一個新的問題,因爲你有任何新問題是不相關的這一個。 – sjagr 2014-11-06 18:23:58

+0

@ivaaaan其實,你的新輸出有什麼問題?有什麼問題? – sjagr 2014-11-06 18:25:07

相關問題