0

我在创建自定义字段类型时遇到了一些问题。

我拥有的是一个实体:

Category  
Id | Name | parent_id    

最后我想要的是一个看起来像这样的选择字段:

Category 1
- SubCategory 1
- SubCategory 2
Category 2
- Subcategory 3
- SubCategory 4
Category 3
Category 4

所以我的方法是创建一个从 CategoryRepository 返回函数的实体字段。像这样:

$builder->add('category', 'entity', array(
                'class' => "AcmeAppBundle:Category",
                "property" => "Name",
                 'query_builder' => function (\path\to\CategoryRepository $repository)
                 {
                     return $repository->getOrderedCategories();
                 }
                )  

并且存储库包含:

class CategoryRepository extends EntityRepository
{
 public function getCategories()
{
    return $this->getEntityManager()
        ->createQuery('SELECT u FROM AcmeAppBundle:Category u WHERE u.parent is NULL ORDER BY u.priority DESC')
        ->getResult();
}

public function getOrderedCategories()
{
   //get the Parent-"Nodes"
   $entity = $this->getCategories();
   $arr = array();

    foreach ($entity as $cat) {  

        $arr[]=$cat;  

        foreach ($cat->getChildren() as $child)
        { 
           $arr[] = $child;
        }
    }
}  

我当前的错误消息是:

Expected argument of type "Doctrine\ORM\QueryBuilder", "array" given   

这是一个好方法吗?有更好的吗?

编辑:
根据要求:类别实体

/**
* Category
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Acme\AppBundle\Entity\CategoryRepository")
*/
class Category
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="Name", type="string", length=255)
 */
private $name;

/**
 * @var integer
 *
 * @ORM\Column(name="Priority", type="integer", nullable=true)
 * 
 */
private $priority;

/**
 * @ORM\OneToMany(targetEntity="Phrase", mappedBy="category")
 */
protected $phrases;

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

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

public function __construct() {
    $this->children = new ArrayCollection();
}

public function getChildren()
{
    return $this->children;
}

public function getParent()
{
    return $this->parent;
}

public function setParent($parent)
{
    if($parent -> getParent() == NULL)
    {
            $this->parent = $parent;
    }  
}

 public function getPhrases()
{
    return $this->phrases;
}
...some Setters&Getters  
4

1 回答 1

2

实体字段类型中的 'query_builder' 选项必须返回一个 QueryBuilder实例,而不是 $queryBuilder->getQuery()->getResult()。(在你的情况下是一个数组)

'query_builder' => function (\path\to\CategoryRepository $repository)
{
    return $repository->createQueryBuilder('category')
        ->where('category.parent is null')
        ->leftJoin('category.children', 'child', 'WITH', 'child.parent = category')
        ->orderBy('category.priority', 'DESC');
 }

试图为您的类别实现树结构?试一试 Gedmo 的扩展 :)

于 2013-05-26T19:39:23.217 回答