Initialized ArrayCollection is null #6434

Closed
opened 2026-01-22 15:33:12 +01:00 by admin · 2 comments
Owner

Originally created by @FabianKoestring on GitHub (Mar 27, 2020).

Q A
Version 2.7.2

Support Question

Hey there,

iam facing following crazy behavior. I have 2 Entities. When i try to access an attribute from the one entity i will get null but this attribute is defined and initialized as an ArrayCollection. I expected a filled or empty ArrayCollection but not null. What may be the problem?

First Entity

...
/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="profiles")
 * @ORM\JoinColumn(name="KAT_ID", referencedColumnName="KAT_ID")
 * @var Category
 */
private $category;
...

Second Entity

...
/**
 * @ORM\OneToMany(targetEntity="Profile", mappedBy="category")
 * @var ArrayCollection
 */
private $profiles;

public function __construct()
{
    $this->profiles = new ArrayCollection();
}
...

Test

// Warning: get_class() expects parameter 1 to be object, null given
var_dump(get_class($category->getProfiles()));
Originally created by @FabianKoestring on GitHub (Mar 27, 2020). <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ----- | Version | 2.7.2 <!-- Before asking question here, please try asking on Gitter or Slack first. Find out more about Doctrine support channels here: https://www.doctrine-project.org/community/ Keep in mind that GitHub is primarily an issue tracker. --> ### Support Question Hey there, iam facing following crazy behavior. I have 2 Entities. When i try to access an attribute from the one entity i will get `null` but this attribute is defined and initialized as an `ArrayCollection`. I expected a filled or empty `ArrayCollection` but not `null`. What may be the problem? ### First Entity ```php ... /** * @ORM\ManyToOne(targetEntity="Category", inversedBy="profiles") * @ORM\JoinColumn(name="KAT_ID", referencedColumnName="KAT_ID") * @var Category */ private $category; ... ``` ### Second Entity ```php ... /** * @ORM\OneToMany(targetEntity="Profile", mappedBy="category") * @var ArrayCollection */ private $profiles; public function __construct() { $this->profiles = new ArrayCollection(); } ... ``` ### Test ```php // Warning: get_class() expects parameter 1 to be object, null given var_dump(get_class($category->getProfiles())); ```
admin added the Question label 2026-01-22 15:33:12 +01:00
admin closed this issue 2026-01-22 15:33:12 +01:00
Author
Owner

@greg0ire commented on GitHub (Mar 28, 2020):

Do you instantiate $category yourself or do you get it from Doctrine? Do you know how to validate your mapping and have you tried doing so?

@greg0ire commented on GitHub (Mar 28, 2020): Do you instantiate `$category` yourself or do you get it from Doctrine? Do you know how to validate your mapping and have you tried doing so?
Author
Owner

@FabianKoestring commented on GitHub (Mar 28, 2020):

Sry. Gonna close this. It happend because of a missing join in a native query with resultset Mapping which selects the Same object.

My bad!

@FabianKoestring commented on GitHub (Mar 28, 2020): Sry. Gonna close this. It happend because of a missing join in a native query with resultset Mapping which selects the Same object. My bad!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6434