2015-04-14 74 views
0

我有一個表(ayrshireminis_car_category),它是用分層結構構建的。使用Gedmo擴展獲得層次結構中的Symfony/Doctrine實體的級別

我正在嘗試使用Doctrine Gedmo extension來拉出我在其他地方使用的「level」成員變量,所以我期望父級的級別爲1,孩子的級別爲2級孫子有3個級別等等...

這是我的映射實體:

AyrshireMinis\CarBundle\Entity\Category: 
    type: entity 
    table: ayrshireminis_car_category 
    gedmo: 
     tree: 
      type: nested 
    id: 
     id: 
      type: integer 
      length: 11 
      generator: { strategy: AUTO } 
    fields: 
     name: 
      type: string 
      length: 150 
     active: 
      type: boolean 
      default: true 
     position: 
      type: integer 
      length: 11 
     level: 
      type: integer 
      gedmo: 
       - treeLevel 
     createdAt: 
       type: datetime 
       gedmo: 
        timestampable: 
         on: create 
       column: created_at 
     updatedAt: 
      type: datetime 
      gedmo: 
       timestampable: 
        on: update 
      column: updated_at 
    manyToOne: 
     parent: 
      targetEntity: AyrshireMinis\CarBundle\Entity\Category 
      inversedBy: children 
      gedmo: 
       - treeParent 
     createdBy: 
      targetEntity: \Sylius\Component\Core\Model\UserInterface 
      joinColumn: 
       nullable: false 
       name: created_by 
      gedmo: 
       blameable: 
        on: create 
     updatedBy: 
      targetEntity: \Sylius\Component\Core\Model\UserInterface 
      joinColumn: 
       nullable: false 
       name: updated_by 
      gedmo: 
       blameable: 
        on: update 
    oneToMany: 
     products: 
      targetEntity: AyrshireMinis\CarBundle\Entity\Product 
      mappedBy: category 

有了這個映射,我得到了以下錯誤:

Missing properties: left, right in class - AyrshireMinis\CarBundle\Entity\Category 500 Internal Server Error - InvalidMappingException

^h不過,我不想在我的表格中留下「左」和「右」列。我怎樣才能解決這個問題?

回答

0

在不改變模式,並在Gedmo擴展倚重我能提取「級別」是這樣的:

/** 
* get a numeric representation of the press category level 
* i.e. parent categories - 0, child categories - 1, grandchild categories - 2 etc... 
* 
* @return int 
*/ 
public function getLevel() 
{ 
    $level = 0; 

    $category = $this; 

    // parent category 
    if ($category->hasParent() === false) { 
     return $level; 
    } 

    while ($category = $category->getParent()) { 
     $level++; 
    } 

    return $level; 
} 

/** 
* @return bool 
*/ 
public function hasParent() 
{ 
    return ($this->parent != null ? true : false); 
} 
相關問題