두 개의 문서가 있습니다. 하나는 Gigs라고하고 하나는 Tracks입니다. 트랙은 Gigs의 임베디드 문서 여야하지만 작동시키지 못합니다. 나는 몇몇 자습서를 따라 왔고 거기에 도달 할 수 없다.Symfony가 포함 된 Doctrine ODM
공연 엔티티
<?php
namespace IGIG\GigBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @MongoDB\Document(repositoryClass="IGIG\GigBundle\Document\GigRepository")
*/
class Gig
{
/**
* @MongoDB\Id
*/
private $id;
/**
* @MongoDB\EmbedMany(targetDocument="Track")
*/
private $tracks = array();
/**
* Gets the value of tracks.
*
* @return mixed
*/
public function getTracks()
{
return $this->tracks;
}
/**
* Sets the value of tracks.
*
* @param mixed $tracks the tracks
*
* @return self
*/
public function setTracks($tracks)
{
$this->tracks = $tracks;
return $this;
}
}
트랙 엔티티
<?php
namespace IGIG\GigBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\EmbeddedDocument()
*/
class Track
{
/**
* @MongoDB\Id
*/
private $id;
/**
* @MongoDB\String
*/
private $title;
/**
* Gets the value of id.
*
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* Sets the value of id.
*
* @param mixed $id the id
*
* @return self
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Gets the value of title.
*
* @return mixed
*/
public function getTitle()
{
return $this->title;
}
/**
* Sets the value of title.
*
* @param mixed $title the title
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
}
그냥 관계를 보여주기 위해 모든 무관 필드를 제거 하였다. 그러나 컨트롤러/뷰에서 임베디드 문서를 호출하면 아무 것도 얻을 수 없습니까?
내 컨트롤러 기능 :
public function selectTracksAction($id)
{
$gig = $this->get('doctrine_mongodb')
->getManager()
->getRepository('IGIGGigBundle:Gig')
->findOneById($id);
return $this->render('IGIGPaymentGatewayBundle:Store:selectTracksPerGig.html.twig', array(
'gig' => $gig,
));
}
그리고 (나뭇 가지에) 내보기에서 논리 : 내가있는 ArrayCollection을 시도했지만, 그와 함께 멀리하지 않았다
{% for track in gig.tracks %}
<tr>
<td>{{ track.title }}</td>
<td>{{ track.price }}</td>
<td><input name="{{ track.id }}" type="checkbox"></td>
</tr>
{% endfor %}
, 몇 가지 다른 방법이 있지만 꽤 혼란 스럽습니다. 미리 감사드립니다!
을 MongoDB 셸에서 원시 문서를 검사하면 어떤 데이터가 포함되어 있습니까? 나는 당신의 모델을 [이 요지] (https://gist.github.com/jmikola/a62cea4d5865369d8d3e)에서 재현했으며 예상대로 작동합니다. 당신은 ArrayCollection을 사용하는 것이 좋지만 일반 PHP 배열을 사용하여 테스트 했으므로 잘 작동합니다. – jmikola
또한, Twig가'gig'의 다른 속성에 접근 할 수 있는지 그리고'gig.tracks'만이 문제인지 확인할 수 있습니까? 속성 액세스를위한 [rules] (http://twig.sensiolabs.org/doc/templates.html#variables)를 기반으로'$ tracks'이 private으로 선언되었으므로 즉시 getTracks()를 호출한다고 생각합니다. 어쨌든 그 경우인지 확인하는 것이 좋습니다. – jmikola