2016-12-14 1 views
0

대부분의 경우 Symfony 2.8 서적 예제를 따르면 초보자로서 ManyToOne 관계로 데이터베이스에서 데이터를 가져 오는 데 문제가 있습니다. 데이터베이스 테이블이 이고 입니다. 배포에는 category_id이 있으며 categories.id을 나타냅니다. 그래서 하나의 카테고리는 많은 배치를 가질 수 있습니다. 데이터베이스에는 데이터가 있는데, Pma에서 사용자 정의 쿼리를 실행하면 결과가 표시됩니다.Symfony 2.8 Doctrine 가져 오기/표시 데이터 문제

내 질문 :내 컨트롤러 코드를 실행 한 후 Collection (이미지 참조)을 얻었지만 deploy 요소로 채워지지 않았습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 내가 배포 특정 CATEGORY_ID을 가진 요소 내 컨트롤러에서

를 표시 할 수 있습니다 어떻게 수행

$category = $this->getDoctrine()->getRepository('AppBundle:Category')->find(1);  
$deploys = $category->getDeploys();  
dump($deploys); 
die(); 

이 뜻을 표시 : enter image description here

내가 가지고 배포 엔티티 (그것의 일부를 응용 프로그램/콘솔)

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="deploys") 
*/ 
class Deploy { 

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

    /** 
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="deploys") 
    * @ORM\JoinColumn(name="category_id", referencedColumnName="id") 
    */ 
    private $category; 
    // etc 

그리고 분류 엔티티에 의해 생성되었습니다

namespace AppBundle\Entity; 

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

/** 
* @ORM\Entity 
* @ORM\Table(name="categories") 
*/ 
class Category { 

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

    /** 
    * @ORM\Column(type="string", length=255) 
    */ 
    private $name; 

    /** 
    * @ORM\Column(type="integer", length=10) 
    */ 
    private $weight; 

    /** 
    * @ORM\OneToMany(targetEntity="Deploy", mappedBy="category") 
    */ 
    private $deploys; 

    /** 
    * Constructor 
    */ 
    public function __construct() { 
    $this->deploys = new ArrayCollection(); 
    } 

    // etc 

답변

3

이것은 지연 시간을 지연시키는 기능입니다. DB에 액세스하려고하면 DB 형식으로로드됩니다.

이 컬렉션의 일부 요소에 액세스하기 만하면 (예 : $deploys 번으로 반복) 정상적으로 작동하는 것을 볼 수 있습니다.

화면에는 속성이 initialized: false입니다. 즉,이 콜렉션은 아직 액세스되지 않았으므로 Doctrine은이 콜렉션을로드 할 필요가 없었습니다.

+0

감사합니다. 작동하도록했습니다. – vaultboy