简体   繁体   中英

Doctrine ODM with Symfony, embedded documents

I've got two documents, one called Gigs, one called Tracks. Tracks should be embedded docs on Gigs, but I can't seem to get it working. I've followed several tutorials and can't quite get there.

Gig entity

<?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;
    }
}

Track entity

<?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;
    }
}

I removed all the irrelevant fields just to demonstrate the relationship. However when I call the embedded documents in my controller/view, I get nothing?

The function in my controller:

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,
        ));
    }

And the logic in my view (in 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 %}

I've tried ArrayCollection, but didn't get far with that, and a few other ways, but I'm pretty confused. Thanks in advance!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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