symfony - How to use Repository custom functions in a FormType -
the problem i'm facing have create selectbox in form holds parent entities (category entity). managed with:
$builder->add('parent', 'entity', array( 'class' => 'kprcentarzdravljabundle:category', 'query_builder' => function($repository) use ($param, $catid) { return $repository->createquerybuilder('p') ->where('p.id != :id , p.parent = :parent') ->setparameters(array('id' => $param, 'parent' => $catid));}, 'property' => 'name', 'required' => false, 'attr' => array('data-placeholder' => '--izaberite opciju--'), ));
as u can see pass 2 arguments first current category.id(a category cant own parent) , second parent id, because want children parent. works nice doesn't give me parents children's children. created categoryrepository recursive function returns children:
<?php namespace kpr\centarzdravljabundle\entity; use doctrine\orm\entityrepository; use doctrine\common\collections\arraycollection; use kpr\centarzdravljabundle\entity\category; class categoryrepository extends entityrepository { public function findbyparenting($parent) { $qb = $this->getentitymanager()->createquerybuilder(); $qb->add('select', 'cat') ->add('from', 'kprcentarzdravljabundle:category cat') ->add('where', 'cat.parent = :parent') ->setparameter('parent', $parent); // $qb instanceof querybuilder $query = $qb->getquery(); $results = $query->getresult(); foreach($results $result){ if($result->getparent()){ $newresult = $this->findbyparenting($result->getid()); $results = array_merge($results, $newresult); } } return $results; } }
how can use findbyparenting($parent) function in entity field?
i posted answer: symfony2 choice field not working. redbirdo.
Comments
Post a Comment