【问题标题】:Doctrine many-to-one returns only the identifierDoctrine 多对一仅返回标识符
【发布时间】:2014-12-15 17:38:18
【问题描述】:

容量和产品之间存在单向多对一关系。 当我想获取数组集合中对象的属性时,我得到“null”。

映射: ''' 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>

方法:

<?php

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

   return $productsAndCapacity;
}

转储对象:

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 ▶}
]

} }

【问题讨论】:

    标签: php symfony doctrine-orm doctrine


    【解决方案1】:

    【讨论】:

    • 我已经尝试过获取模式,我忘了提及它,它没有工作它返回 NULL,我只需要他们的。如果我将使用 fetchmode 会不会产生任何性能问题?
    • 您是从 DQL、Repository 还是 $em-&gt;getReference(..) 获取这些对象?
    • 我获取容量:$productCapacity = $this->repository->findOneById(1);然后我在模型“getCapacity”上有一个方法,它将映射所有容量,请参阅原始帖子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2010-09-26
    • 1970-01-01
    相关资源
    最近更新 更多