【问题标题】:Doctrine ODM with Symfony, embedded documents带有 Symfony 的 Doctrine ODM,嵌入式文档
【发布时间】: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,但并没有走得太远,还有其他一些方法,但我很困惑。提前致谢!

【问题讨论】:

  • 如果您从 MongoDB shell 检查原始文档,其中包含哪些数据?我在this gist 中复制了您的模型,它可以正常工作。您说 ArrayCollection 更可取是对的,但我用一个普通的 PHP 数组进行了测试,效果也很好。
  • 另外,您能否确认 Twig 是否能够访问 gig 的其他属性并且只有 gig.tracks 是一个问题?基于它的rules 用于属性访问,我想它会立即调用getTracks(),因为$tracks 被声明为私有,但无论如何最好验证一下。

标签: php mongodb symfony doctrine


【解决方案1】:

如果您想在一场演出中制作多首曲目,则必须进行“一对多”或“多对多”映射,并且您应该进行表单嵌入,为此您可以点击链接:How to Embed a Collection of Forms

【讨论】:

  • 没有像 OneToMany 或 ManyToMany 这样的注解,它们应该是嵌入文档。然而,EmbedMany 确实存在。我认为您正在考虑关系数据,而不是嵌入式?
猜你喜欢
  • 1970-01-01
  • 2012-06-11
  • 2012-12-20
  • 1970-01-01
  • 2012-03-05
  • 2012-01-16
  • 2016-08-04
  • 2014-03-20
  • 1970-01-01
相关资源
最近更新 更多