2016-08-25 28 views
1

我有一個名爲「InterestGroup」的實體,它具有屬性「children」形式的自引用關聯(鄰接列表)(一對多)和「父母」(多對一)。Symfony實體字段類型查詢生成器中的where子句不工作一對多自引用關聯

在爲InterestGroup設置表單類型時,我試圖爲屬性父級提供一個選擇列表,該列表擁有所有「頂級」興趣組(其'父'屬性爲null')的選擇。當我將EntityType字段的where子句和null參數添加到query_builder時,即使我有幾個頂級(父爲空)興趣組持續存在,它也不會返回任何內容。如果我刪除where子句,它將返回表中的所有InterestGroups。我很難理解爲什麼where子句不起作用。

這是有問題的領域:

->add('parent',EntityType::class, 
    array(
     'placeholder' => 'Top Level (No Parent)', 
     'required' => false, 
     'class' => 'Common\ContentBundle\Entity\InterestGroup', 
     'query_builder' => function (EntityRepository $er) { 
      return $er->createQueryBuilder('ig') 
       ->where('ig.parent = :n') 
       ->setParameter('n',null) 
       ->orderBy('ig.title', 'ASC'); 
     }, 
     'choice_label' => 'title' 
    ) 
) 

以上會返回一個空的選擇菜單。通過刪除where子句和setparameter,我得到所有InterestGroup實體,包括所有那些具有空父母的實體。

以下是實體類的InterestGroup

<?php 

namespace Common\ContentBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use Symfony\Component\Validator\Constraints as Assert; 
use Gedmo\Mapping\Annotation as Gedmo; 

/** 
* InterestGroup 
* 
* @ORM\Table(name="interest_group") 
* @ORM\Entity(repositoryClass="Common\ContentBundle\Repository\InterestGroupRepository") 
*/ 
class InterestGroup 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="title", type="string", length=255, unique=true) 
    * @Assert\NotBlank(message="This is a required field.") 
    */ 
    private $title; 

    /** 
    * @ORM\OneToMany(targetEntity="InterestGroup", mappedBy="parent") 
    */ 
    private $children; 

    /** 
    * @ORM\ManyToOne(targetEntity="InterestGroup", inversedBy="children") 
    * @ORM\JoinColumn(name="parent_id", referencedColumnName="id") 
    */ 
    private $parent; 

    /** 
    * @Gedmo\Slug(fields={"title"}) 
    * @ORM\Column(length=128, unique=true) 
    */ 
    private $slug; 

    // ... 
    /** 
    * @ORM\ManyToMany(targetEntity="Product", mappedBy="interestGroups") 
    */ 
    private $products; 


    /** 
    * InterestGroup constructor. 
    */ 
    public function __construct() 
    { 
     $this->children = new ArrayCollection(); 
     $this->products = new ArrayCollection(); 

    } 

    /** 
    * Get id 
    * 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * @return string 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 

    /** 
    * @param string $title 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 
    } 


    /** 
    * @return mixed 
    */ 
    public function getSlug() 
    { 
     return $this->slug; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getChildren() 
    { 
     return $this->children; 
    } 

    /** 
    * @param mixed $children 
    */ 
    public function setChildren($children) 
    { 
     $this->children = $children; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getParent() 
    { 
     return $this->parent; 
    } 

    /** 
    * @param mixed $parent 
    */ 
    public function setParent($parent) 
    { 
     $this->parent = $parent; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getProducts() 
    { 
     return $this->products; 
    } 
} 

和表單類型類:提前

<?php 

namespace Common\ContentBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Form\Extension\Core\Type\TextType; 
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 
use Doctrine\ORM\EntityRepository; 

class InterestGroupType extends AbstractType 
{ 
    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('title',TextType::class) 
      ->add('parent',EntityType::class, 
       array(
       'placeholder' => 'Top Level (No Parent)', 
       'required' => false, 
       'class' => 'Common\ContentBundle\Entity\InterestGroup', 
       'query_builder' => function (EntityRepository $er) { 
        return $er->createQueryBuilder('ig') 
         ->where('ig.parent = :n') 
         ->setParameter('n',null) 
         ->orderBy('ig.title', 'ASC'); 
       }, 
       'choice_label' => 'title' 
      ) 
      ) 
     ; 
    } 

    /** 
    * @param OptionsResolver $resolver 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'Common\ContentBundle\Entity\InterestGroup' 
     )); 
    } 
} 

謝謝!

回答

1

檢查你的代碼在這裏:

return $er->createQueryBuilder('ig') 
      ->where('ig.parent = :n') // <--- 
      ->setParameter('n',null) // <--- 
      ->orderBy('ig.title', 'ASC'); 

這是相同於:

... WHERE ig.parent = NULL ... 

所以,此查詢總是返回null數據集。

權代碼:

return $er->createQueryBuilder('ig') 
      ->where('ig.parent IS NULL') 
      ->orderBy('ig.title', 'ASC'); 

使用IS NULL檢查空值。

此問題與what is "=null" and " IS NULL"

相關問題