These are chat archives for symfony2admingenerator/GeneratorBundle

22nd
Apr 2015
Stéphane
@sescandell
Apr 22 2015 05:34
Hi @loostro actally, given that code, it seems collection are not handle.
I'm wondering if {{ filter }} in ->getManagerForClass(get_class($filters['{{ filter }}'])) should be replaced by filterModel ... and we probably need to make specific process if the collection is used as multiple => true
that's a good point...
ioleo
@ioleo
Apr 22 2015 09:35
I've managed to make it work with this custom code
```php
/**
 * Store in the session service the current filters
 *
 * @param array the filters
 */
protected function setFilters(array $filters)
{
    if (isset($filters['promoted'])) {
        $filters['promoted'] = array (
            'id' => $filters['promoted'] instanceof ArrayCollection
                ? $filters['promoted']->map(function($item){ return $item->getId(); })->toArray()
                : is_array($filters['promoted'])
                    ? array_map(function($item){ return $item->getId(); }, $filters['promoted'])
                    : array($filters['promoted']->getId()),
            'entityName' => 'Kidoz\EventBundle\Entity\Event'
        );
    }
    $this->get('session')->set($this->getSessionPrefix().'List\Filters', $filters);
}

/**
 * Get filters from session
 */
protected function getFilters()
{
    $filters = $this->get('session')->get($this->getSessionPrefix().'List\Filters', array());

    if (isset($filters['promoted'])) {
        $filters['promoted'] = $this->getDoctrine()
            ->getManagerForClass($filters['promoted']['entityName'])
            ->createQueryBuilder()
            ->select('q')
            ->from($filters['promoted']['entityName'], 'q')
            ->where('q.id IN ('.implode(',', $filters['promoted']['id']).')')
            ->getQuery()
            ->getResult();
    }

    return $filters;
}

protected function getFilterForm()
{
    $filters = $this->getFilters();

    if (isset($filters['promoted'])) {
        $data = is_array($filters['promoted']) ? $filters['promoted'] : array($filters['promoted']);

        $uow = $this->getDoctrine()
                ->getManagerForClass('Kidoz\EventBundle\Entity\Event')
                ->getUnitOfWork();

        foreach ($data as $item) {
            $uow->registerManaged($item, array('id' => $item->getId()), array());
        }
    }

    return $this->createForm($this->getFiltersType(), $filters, array_merge(
        $this->getFiltersOptions(),
        array('groups' => $this->getGroups())
    ));
}

/**
 * Add filters to the query for promoted
 *
 * @param queryFilter The queryFilter
 * @param mixed The value
 */
protected function filterPromoted($queryFilter, $value)
{
    $queryFilter->addCollectionFilter('promoted.id', $value);
}
@sescandell This is CategoryController/ListController.php and entity Category has a OneToMany relation with Event in field category.promoted
@sescandell and, as you can see, Event's primary key is called id
I was wondering if I should make a PR from this
ioleo
@ioleo
Apr 22 2015 09:41
the tricky part is in setFilters, where $filters['promoted'] sometimes is an ArrayCollection, and sometimes an Array,