繁体   English   中英

具有Symfony的Doctrine ODM,嵌入式文档

[英]Doctrine ODM with Symfony, embedded documents

我有两个文档,一个叫做Gigs,一个叫做Tracks。 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,但是在其他方面还没有做到,但是我很困惑。 提前致谢!

如果您想一次演出多首曲目,则必须进行“一对多”或“多对多”映射,并且还应该进行表单嵌入,因此您可以按照以下链接进行操作: 如何嵌入表单集合

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM