简体   繁体   English

保存相关实体学说/ Symfony2

[英]Saving Related Entities Doctrine/Symfony2

I have one specific issue. 我有一个具体问题。 I have two entities: 我有两个实体:

    class MyPlaylist {
   ...
    /**
     * @var Array 
     * @ORM\OneToMany(targetEntity="MyPlaylistContent", mappedBy="myPlaylist", orphanRemoval=true)
     * @ORM\OrderBy({"position" = "DESC"})
     */
    private $myPlaylistItems;

and

class MyPlaylistContent {
....

    /**
     * @ORM\ManyToOne(targetEntity="MyPlaylist", inversedBy="myPlaylistItems")
     */
    private $myPlaylist;

Now I have this in my service 现在我有这个服务

....    
    $myPlaylist = new MyPlaylist();
    $myPlaylist->setUser($user);
    $myPlaylist->setActive(true);

    // add tracks
    foreach ($playlist->getMyPlaylistItems() as $item) {
      $entity = new MyPlaylistContent();
      $entity->setTrack($item->getTrack());
      $entity->setMyPlaylist($myPlaylist);
      $this->em->persist($entity);
    }

    $this->em->persist($myPlaylist);
    $this->em->flush();

    \Doctrine\Common\Util\Debug::dump($myPlaylist);

    return $myPlaylist;

so, I return a new playlist. 因此,我返回了一个新的播放列表。 If I look at the database, all works fine. 如果我查看数据库,一切正常。 I have both entities and in MyPlaylistContent - 3 tracks. 我既有实体,又有MyPlaylistContent-3个音轨。 But

\\Doctrine\\Common\\Util\\Debug::dump($myPlaylist); \\ Doctrine \\ Common \\ Util \\ Debug :: dump($ myPlaylist); shows next 显示下一个

["active"]=> bool(true) ["myPlaylistItems"]=> array(0) { } [“ active”] => bool(true)[“ myPlaylistItems”] => array(0){}

On the page, the app shows the empty playlist (no tracks). 该应用程序在页面上显示空的播放列表(无曲目)。 If I refresh the page, I can see all tracks. 如果刷新页面,则可以看到所有曲目。

The point is, if you open the page, the controller will call the service, build the content and return the list as a response. 关键是,如果您打开页面,则控制器将调用服务,构建内容并返回列表作为响应。 It looks as the same example, but it does not work for me http://symfony.com/doc/current/book/doctrine.html#saving-related-entities 它看起来像一个例子,但对我不起作用http://symfony.com/doc/current/book/doctrine.html#saving-related-entities

What is wrong here? 怎么了 Why don't I get tracks for the current entity? 为什么我没有当前实体的跟踪?

You forget to add MyPlaylistContent to MyPlaylist . 您忘记将MyPlaylistContent添加到MyPlaylist

Use this snippet into foreach 使用此代码段进入foreach

$myPlaylist->addMyPlaylistContent($myPlaylistContent);

Of course change name or implement method accordingly 当然要相应地更改名称或实现方法

First note: this is because objects are "normal" php objects, they have nothing to do with doctrine so, relationships are only a doctrine concept. 首先要注意:这是因为对象是“正常”的php对象,它们与教义无关,因此,关系只是一个教义概念。 EntityManager in doctrine will handle this kind of processes, not php itself. 理论上的EntityManager将处理此类过程,而不是PHP本身。 If you take a look to your classes methods you will probably notice that no "connection" (assignments) are made between those objects. 如果您查看类方法,您可能会注意到在这些对象之间没有进行“连接”(分配)。 If you would like, you can modify MyPlaylistContent to add itself to MyPlaylist once assigned. 如果愿意,您可以修改MyPlaylistContent,以便在分配后将其自身添加到MyPlaylist中。 Something like 就像是

class MyPlaylistContent { [...] 类MyPlaylistContent {[...]

public function setMyPlaylist(MyPlaylist $mp) 
{
    $this->myPlaylist = $myPlaylist;
    $mp->addMyPlaylistContent($this);

    return $this;
}

Second note: hope your names are more consistents of these ones :) 第二点:希望您的名字与这些名字更一致:)

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

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