2012-06-29 2 views
0

Article (ManyToOne)을 사용하여 Steps (OneToMany) 및 Steps와 관계가있는 테이블 기사가 있습니다.Doctrine/Symfony2 관계에있는 테이블에서 데이터를 가져옵니다.

내가 함께 기사를 얻을 :

$articles = $this->getDoctrine() 
      ->getRepository("IftodiDesignBundle:Article") 
      ->findAll(); 

그리고 foreach 문에 나는 모든 기사 및 단계 보여주고 싶은 : 나는 테이블을 usign 테이블 단계에서 데이터를 가져 오는 방법을 모르는

foreach($articles as $article) 
    { 
     $steps = $this->getDoctrine() 
       ->getRepository("IftodiDesignBundle:Steps") 
       ->createQueryBuilder('s') 
       ->where('s.article = \''.$article.'\'') 
       ->getQuery() 
       ->execute(); 

     echo "<b>".$article->getTitle()."</b><br />"; 
     echo $article->getText()."<br />"; 
    } 

을 Doctrine으로 생성 된 기사 및 메소드 getSteps().

도와주세요.


답변 해 주셔서 감사합니다. 테이블 조에서

내가 가진 :

/** 
* @ORM\OneToMany(targetEntity="Steps", mappedBy="Steps",cascade={"persist"}) 
* @ORM\JoinColumn(name="id", referencedColumnName="id_article") 
*/ 
protected $steps; 

테이블 단계 :

/** 
* @ORM\ManyToOne(targetEntity="Article", inversedBy="steps") 
* 
*/ 
protected $article; 

내가 할 경우 :

$articles = $this->getDoctrine() 
      ->getRepository("IftodiDesignBundle:Article") 
      ->findAll(); 

내가 할 경우 :

foreach($articles as $article) 
    { 
     $steps = $article->getSteps(); 

나는 오류를받을 :

Notice: Undefined index: Steps in /var/www/design/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1280 

내가 그렇게한다면 :

$articles = $this->getDoctrine() 
      ->getRepository("IftodiDesignBundle:Article") 
      ->findAll(); 
    foreach($articles as $article) 
    { 
     //Queries to DB 
     $steps = $this->getDoctrine() 
      ->getRepository("IftodiDesignBundle:Steps") 
      ->findBy(array(
       "article" => $article->getId() 
      )); 
     $media = $this->getDoctrine() 
       ->getRepository("IftodiDesignBundle:Media") 
       ->findBy(array(
        "step" => $steps[0]->getId() 
       )); 

내가 필요한 데이터를 얻을 수 있지만, 여기에 DB에 더 질문입니다. 당신이 제 모델에서 다음과 같은 관계가 있다고 가정

+0

함께 몇 가지 더 모범 사례를 넣어 저장소에 있으므로 같은 방법을 넣을 수 있습니다,하지만 난 본능적으로 시도 할 것 '$ article-> getSteps();'. 너 그거 해봤 니? – AJJ

+0

예, 시도했지만 수신 및 오류. 덕분에 –

답변

1

이 주석에서 @ORM\JoinColumn(name="id", referencedColumnName="id_article") 삭제를 할 수있을 것입니다 - 교리는 매핑합니다 당신의 현명한 기본값으로 MySQL에 연결.

삭제 한 후

, 당신의 SQL 재생 : 당신이 당신의 단계를 얻기 위해 당신의 기사를 통해 반복하는 경우 또한 doctrine:schema:update --force

을, 당신은 당신이 ->getSteps() 교리 것 전화 달리 할 때마다 저장소에서 사용자 지정 방법을 쓰는 것이 더 낫다 SQL 호출 (상상 100 개 이상의 기사를 상상해보십시오 - 당신은 결국 데이터베이스에 101 개의 호출을 할 것입니다!)이 문제를 방지하려면

당신은 내가 교리에 대해 거의 알고

public function all() 
{ 
    $queryBuilder = $this->getEntityManager()->createQueryBuilder(); 

    $queryBuilder 
     ->select('Article', 'Steps') 
     ->from('IftodiDesignBundle:Article') 
     ->leftJoin('Article.steps', 'Steps') 
    ; 

    // consider using ->getArrayResult() to use less memory 
    return $queryBuilder->getQuery()->getResult(); 
} 

가 나는 blog post

+0

. 나는 또한 문제가있다. 주석을 수정하여 데이터베이스를 삭제하고 다시 만들었습니다. 이제 오류는 : Notice : 정의되지 않은 색인 : /var/www/design/vendor/doctrine/lib/Doctrine/ORM/Query/SqlWalker.php 라인 746; @PeterSmith가 해결하도록 도와주세요. –

+0

다음은 $ steps = $ article-> getSteps() 할 때 var_dump ($ steps)입니다. http://pastebin.com/CZsxjRcg –

0

음,

:

/** 
* @OneToMany(targetEntity="Step", mappedBy="article") 
* @JoinColumn(name="id_article", referencedColumnName="id_article_in_step") 
*/ 
protected $steps; 

public function getSteps(){ return $this->steps; } 

당신이 $article->getSteps();

관련 문제