2
votes

I have a unidirectional many-to-one relation between capacity and product. When i want to get a property of an object in the array collection i get "null".

The mapping: ''' http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="\Capacity" table="capacity">
        <id name="id" type="integer">
             <generator strategy="AUTO" />
        </id>

        <!-- Relation: product -->
        <many-to-one field="product" target-entity="IDT\Component\Paylogic\Model\Product">
             <join-column name="product_id"      referenced-column-name="uid"  nullable="false" />
        </many-to-one>
    </entity>
</doctrine-mapping>

The method:

<?php

class Capacity 
{
    public function getCapacity()
    {
        // Validate the capacity.
        $productsAndCapacity = $this->getProductCapacities()->map(function($entity)  {
          dump($entity->getProduct()->getName());
          die;
        })->toArray();

   return $productsAndCapacity;
}

The dumped object:

PersistentCollection {#430 ▼
-snapshot: []
-owner: null
-association: null
-em: null
-backRefFieldName: null
-typeClass: null
-isDirty: false
-initialized: true
-coll: ArrayCollection {#431 ▼
    -_elements: array:2 [▼
      0 => Capacity {#432 ▼
        #id: 3
        #product: Product {#512 ▼
          +__isInitialized__: false
          #uid: "de4b1153f3854d6b9e6230704c1ffe88"
          #name: null
          #subtitle: null
          #availability: null
          #maxPerOrder: null
          #minPerOrder: null
          #price: null
          #separatelySaleable: null
          #soldOut: null
          #soldOutText: null
          #event: null
          #shippingMethods: null
          #paymentMethods: null
          #createdAt: null
          #updatedAt: null
          #uri: null
          #lastSyncedAt: null
       …4
    }
    #maxCapacity: 5
    #currentCapacity: 0
    #ticketUrl: TicketUrl {#418 ▶}
    #createdAt: DateTime {#510 ▶}
    #updatedAt: DateTime {#509 ▶}
  }
  1 => Capacity {#508 ▶}
]

} }

1

1 Answers