【发布时间】:2014-07-08 23:37:54
【问题描述】:
我有两份文件,一份叫做 Gigs,一份叫做 Tracks。曲目应该是 Gigs 上的嵌入式文档,但我似乎无法让它工作。我已经学习了几个教程,但无法完全到达那里。
演出实体
<?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,
));
}
我认为的逻辑(在 Twig 中):
{% for track in gig.tracks %}
<tr>
<td>{{ track.title }}</td>
<td>{{ track.price }}</td>
<td><input name="{{ track.id }}" type="checkbox"></td>
</tr>
{% endfor %}
我尝试了 ArrayCollection,但并没有走得太远,还有其他一些方法,但我很困惑。提前致谢!
【问题讨论】:
标签: php mongodb symfony doctrine