选择具有关联键的实体上的所有记录

时间:2013-03-28 14:52:34

标签: symfony doctrine-orm symfony-2.1

我有两个实体,计划和定价。该计划映射到我的计划表,并有一个名为pricing_tier_id的列。这通过其id列映射到PricingTiers表。实体具有从Plan到PricingTier的多个关联关系,以及从PricingTier到Plan的OneToMany关联。这些是:

计划实体

etrak\OnlineOrderProcessingBundle\Entity\Plan:
  type: entity
  table: plans
  id:
    id:
      type: integer
      generator: { strategy: AUTO }
  fields:
    name:
      type: string
      length: 255
      nullable: true
    description:
      type: text
      nullable: true
    termsConditions:
      column: terms_conditions
      type: text
      nullable: true
    active:
      type: boolean
      nullable: true
    amount:
      type: decimal
      nullable: true
      scale: 2
      precision: 5
    affinity:
      type: boolean
      nullable: true
    deactivationFee:
      column: deactivation_fee
      type: decimal
      scale: 2
      precision: 5
      nullable: true
    gracePeriodDays:
      column: grace_period_days
      type: integer
      nullable: true
    recurringInMonths:
      column: recurring_in_months
      type: integer
      nullable: true
    activeStartDate:
      column: active_start_date
      type: date
    activeEndDate:
      column: active_end_date
      type: date
  lifecycleCallbacks:
    prePersist: [ prePersist ]
  manyToOne:
    pricingTier:
      targetEntity: PricingTier
      inversedBy: plans
      joinColumn:
        name: pricing_tier_id
        referencedColumnName: id

PricingTier实体

etrak\OnlineOrderProcessingBundle\Entity\PricingTier:
  type: entity
  table: pricing_tiers
  id:
    id:
      type: integer
      generator: { strategy: AUTO }
  fields:
    name:
      type: string
      length: 50
    description:
      type: string
      length: 100
      nullable: true
    minimumDevices:
      column: minimum_devices
      type: integer
    isAffinity:
      column: is_affinity
      type: boolean
    keyname:
      type: string
      length: 55
    createdBy:
      column: created_by
      type: string
      length: 20
    createdOn:
      column: created_on
      type: datetime
    updatedOn:
      column: updated_on
      type: datetime
      nullable: true
  lifecycleCallbacks:
    prePersist: [ onPrePersist ]
    preUpdate: [ onPreUpdate ]
  oneToMany:
    plans:
      targetEntity: Plan
      mappedBy: pricingTier
  oneToOne:
    product:
      targetEntity: Product
      joinColumn:
        name: product_id
        referencedColumnName: id

我需要做的是:SELECT * FROM plans WHERE pricing_tier_id = 2

如果可能的话,我想远离SQL / DQL。当然,Doctrine有一个内置的方法来做到这一点?请注意,我在Symfony 2.1中使用Doctrine作为捆绑包。

1 个答案:

答案 0 :(得分:0)

你能不能只使用存储库找到你想要的定价层。

$pricingTier = $this->getDoctrine()
                    ->getRepository('OnlineOrderProcessingBundle:PricingTier')
                    ->find($id);

然后您可以使用..

访问所有相关计划
$pricingTier->getPlans()

或在树枝上

{{ pricingTier.plans }}
相关问题