获取子实体返回null而不是arrayCollection对象 [英] Get child entities returns null instead of arrayCollection object

查看:125
本文介绍了获取子实体返回null而不是arrayCollection对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有oneToMany关系的实体:



发布实体:

  ... 
oneToMany:
图像:
mappedBy:post
targetEntity:Shop\Bundle\ManagementBundle\Entity\Image

图像实体:

  ... 
manyToOne:
post:
targetEntity:Shop\Bundle\ManagementBundle\Entity\Post
inversedBy:images
joinColumn:
onDelete:cascade

$实体实例 Post ,当我在做 $ entity-> getImages()时,我收到的东西如下: / p>

 对象(Doctrine\ORM\PersistentCollection)[65] 
private'snapshot'=>
array(size = 0)
empty
private'owner'=>
object(Acme\Bundle\ImageUpBundle\Entity\Post)[54]
private'id'=> int 41
private'title'=> string'kbd'(length = 3)
private'images'=>
& object(Doctrine\ORM\PersistentCollection)[65]
private'association'=>
数组(size = 15)
'fieldName'=> string'images'(length = 6)
'targetEntity'=>字符串'Shop\Bundle\ManagementBundle\Entity\Image'(length = 38)
'mappedBy'=> string'post'(length = 4)
'type'=> int 4
'inversedBy'=> null
'isOwningSide'=> boolean false
'sourceEntity'=> string'Shop\Bundle\ManagementBundle\Entity\Post'(length = 37)
'fetch'=> int 2
'cascade'=>
array(size = 0)
empty
'isCascadeRemove'=> boolean false
'isCascadePersist'=> boolean false
'isCascadeRefresh'=> boolean false
'isCascadeMerge'=> boolean false
'isCascadeDetach'=> boolean false
'orphanRemoval'=> boolean false
private'em'=>
....

但是现在我不幸得到 null



我真的很尽力找出可能导致这个问题的原因。您的帮助非常感激。



编辑:



给定一个整数$ id,我使用以下方式获取一个Post实体:

  $ em = $ this-> getDoctrine() - > getManager() ; 
$ entity = $ em-> getRepository('ShopManagementBundle:Post') - > find($ id);

我成功获取了Post实体的所有属性,除了图像

解决方案

这里有解决我的问题的东西:



1-我在配置文件中收集了一个 oneToMany 参数之间的一对多关联。换句话说,而不是:

  oneToMany:
图像:
targetEntity ....
....
oneToMany:
消息:
targerEntity ...
....

我会有:

  oneToMany:
图片:
targerEntity ...
....
消息:
targerEntity ...
....
/ pre>


  1. 我再次使用 app / console生成实体doc:gen:entities 使唯一的构造函数构造两个 ArrayCollection



< blockquote>

  / ** 
*构造函数
* /
public function __construct()
{
$ this-> messages = new \Doctrine\Common\Collections\ArrayCollection();
$ this-> images = new \Doctrine\Common\Collections\ArrayCollection();
}


现在,当我打电话 $ em-> getRepository('ShopManagementBundle:Post) - > find($ id)我有子实体( Images )当数据库中存在具体的记录,而不是Null时,附加到我的父实体( Post )。当我使用新的Post()删除新的实体时,我有空的ArrayCollection而不是空。



我知道这个答案是缺乏编程逻辑,似乎是任意的努力,但是我写这个是为了分享这个例子突然出现这个问题(正如我所说)。我希望它有帮助。


I have two entities with a oneToMany relationship:

Post entity:

...
oneToMany:
  images:
    mappedBy: post
    targetEntity: Shop\Bundle\ManagementBundle\Entity\Image

Image entity:

...
    manyToOne:
        post:
            targetEntity: Shop\Bundle\ManagementBundle\Entity\Post
            inversedBy: images
            joinColumn:
                onDelete: cascade

With $entity instance of Post, when I was doing $entity->getImages(), I was receiving something like:

     object(Doctrine\ORM\PersistentCollection)[65]
      private 'snapshot' => 
        array (size=0)
          empty
      private 'owner' => 
        object(Acme\Bundle\ImageUpBundle\Entity\Post)[54]
          private 'id' => int 41
          private 'title' => string 'kbd' (length=3)
          private 'images' => 
            &object(Doctrine\ORM\PersistentCollection)[65]
      private 'association' => 
        array (size=15)
          'fieldName' => string 'images' (length=6)
          'targetEntity' => string 'Shop\Bundle\ManagementBundle\Entity\Image' (length=38)
          'mappedBy' => string 'post' (length=4)
          'type' => int 4
          'inversedBy' => null
          'isOwningSide' => boolean false
          'sourceEntity' => string 'Shop\Bundle\ManagementBundle\Entity\Post' (length=37)
          'fetch' => int 2
          'cascade' => 
            array (size=0)
              empty
          'isCascadeRemove' => boolean false
          'isCascadePersist' => boolean false
          'isCascadeRefresh' => boolean false
          'isCascadeMerge' => boolean false
          'isCascadeDetach' => boolean false
          'orphanRemoval' => boolean false
      private 'em' =>
....

But now i unfortunately get null.

I really did all my best to figure out what might cause such issue. Your help is much appreciated.

Edit:

given an in integer $id, I fetch a Post entity using:

$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ShopManagementBundle:Post')->find($id);

I successfully get all the attributes of Post entity except from images.

解决方案

Well here are the things that solved my issue:

1- I gathered all One To Many associations under one oneToMany parameter in config file. In other words, instead of having:

oneToMany: 
  images:
     targetEntity....
     ....
oneToMany:
   messages:
      targerEntity...
      ....

I would have:

oneToMany:
   images:
      targerEntity...
      ....
   messages:
      targerEntity...
      ....

  1. I generated entities again using app/console doc:gen:entities making the only one constructor constructs the two ArrayCollections.

/**
     * Constructor
     */
    public function __construct()
    {
        $this->messages = new \Doctrine\Common\Collections\ArrayCollection();
        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
    }

Now when I call $em->getRepository('ShopManagementBundle:Post)->find($id) I have the child entities (Images) attached to my parent entity (Post) when concrete records exist in database, and not Null. When I insantiate a new entity using new Post(), I have empty ArrayCollection and not Null.

I know this answer is lacking of programming logic and seems arbitrary effort, but I write it in the sake of sharing in the case someone encounters suddenly this problem (As I did). I hope it helps.

这篇关于获取子实体返回null而不是arrayCollection对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆