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

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

问题描述

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

发布实体:

<代码>...一对多:图片:映射者:post目标实体:商店捆绑管理捆绑实体图像

图像实体:

<代码>...多对一:邮政:目标实体:商店捆绑管理捆绑实体邮政inversedBy: 图片加入列:onDelete:级联

使用 Post$entity 实例,当我在执行 $entity->getImages() 时,我收到如下信息:

 对象(DoctrineORMPersistentCollection)[65]私人快照"=>数组(大小=0)空的私人所有者"=>对象(AcmeBundleImageUpBundleEntityPost)[54]私人'id' =>诠释 41私人标题"=>字符串kbd"(长度=3)私人图片"=>&object(DoctrineORMPersistentCollection)[65]私人协会"=>数组(大小=15)'字段名' =>字符串图像"(长度=6)'目标实体' =>字符串'ShopBundleManagementBundleEntityImage'(长度=38)'mappedBy' =>字符串'post'(长度=4)'类型' =>诠释 4'inversedBy' =>空值'isOwningSide' =>布尔假'sourceEntity' =>字符串ShopBundleManagementBundleEntityPost"(长度=37)'获取' =>诠释 2'级联' =>数组(大小=0)空的'isCascadeRemove' =>布尔假'isCascadePersist' =>布尔假'isCascadeRefresh' =>布尔假'isCascadeMerge' =>布尔假'isCascadeDetach' =>布尔假'orphanRemoval' =>布尔假私人'em' =>……

但现在我不幸得到 null.

我真的尽了最大努力找出可能导致此类问题的原因.非常感谢您的帮助.

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

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

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

解决方案

以下是解决我问题的方法:

1- 我在配置文件的一个 oneToMany 参数下收集了所有一对多关联.换句话说,而不是:

oneToMany:图片:目标实体....……一对多:留言:目标实体...……

我会的:

oneToMany:图片:目标实体...……留言:目标实体...……

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

<块引用>

/*** 构造函数*/公共函数 __construct(){$this->messages = new DoctrineCommonCollectionsArrayCollection();$this->images = new DoctrineCommonCollectionsArrayCollection();}

现在,当我调用 $em->getRepository('ShopManagementBundle:Post)->find($id) 时,我附加了子实体(Images)当数据库中存在具体记录而不是 Null 时,发送到我的父实体 (Post).当我使用 new Post() 实例化一个新实体时,我有空的 ArrayCollection 而不是 Null.

我知道这个答案缺乏编程逻辑并且看起来很随意,但是我写它是为了分享以防有人突然遇到这个问题(就像我一样).希望对你有帮助.

I have two entities with a oneToMany relationship:

Post entity:

...
oneToMany:
  images:
    mappedBy: post
    targetEntity: ShopBundleManagementBundleEntityImage

Image entity:

...
    manyToOne:
        post:
            targetEntity: ShopBundleManagementBundleEntityPost
            inversedBy: images
            joinColumn:
                onDelete: cascade

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

     object(DoctrineORMPersistentCollection)[65]
      private 'snapshot' => 
        array (size=0)
          empty
      private 'owner' => 
        object(AcmeBundleImageUpBundleEntityPost)[54]
          private 'id' => int 41
          private 'title' => string 'kbd' (length=3)
          private 'images' => 
            &object(DoctrineORMPersistentCollection)[65]
      private 'association' => 
        array (size=15)
          'fieldName' => string 'images' (length=6)
          'targetEntity' => string 'ShopBundleManagementBundleEntityImage' (length=38)
          'mappedBy' => string 'post' (length=4)
          'type' => int 4
          'inversedBy' => null
          'isOwningSide' => boolean false
          'sourceEntity' => string 'ShopBundleManagementBundleEntityPost' (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 DoctrineCommonCollectionsArrayCollection();
        $this->images = new DoctrineCommonCollectionsArrayCollection();
    }

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天全站免登陆