sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

sonata_type_model_autocomplete not working with KNP Translatable #4323

Closed pixelsucht closed 4 years ago

pixelsucht commented 7 years ago

Environment

Sonata packages

$ composer show sonata-project/*
sonata-project/admin-bundle              3.13.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.3.0  Symfony SonataBlockBundle
sonata-project/cache                     1.0.7  Cache library
sonata-project/core-bundle               3.2.0  Symfony SonataCoreBundle
sonata-project/doctrine-orm-admin-bundle 3.1.3  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  1.7.0  Lightweight Exporter library
sonata-project/translation-bundle        2.1.0  SonataTranslationBundle

Symfony packages

$ composer show symfony/*
symfony/assetic-bundle     v2.8.1 Integrates Assetic into Symfony2
symfony/monolog-bundle     v3.0.3 Symfony MonologBundle
symfony/phpunit-bridge     v3.2.3 Symfony PHPUnit Bridge
symfony/polyfill-apcu      v1.3.0 Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-intl-icu  v1.3.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring  v1.3.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php56     v1.3.0 Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70     v1.3.0 Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util      v1.3.0 Symfony utilities for portability of PHP codes
symfony/security-acl       v3.0.0 Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle v2.4.2 Symfony SwiftmailerBundle
symfony/symfony            v3.2.3 The Symfony PHP framework

PHP version

$ php -v
PHP 5.6.29

Subject

sonata_type_model_autocomplete Doctrine Query without "where" when using KNP Translatable

Steps to reproduce

class RecipeAdmin

protected function configureFormFields(FormMapper $formMapper)
{
        $formMapper
                ->with('Texte', ['class' => 'col-md-8'])
                ->add('translations', TranslationsType::class, [
                    ->with('Settings', ['class' => 'col-md-4'])
                    ->add('tags', 'sonata_type_model_autocomplete', [
                        'property' => 'translations.name',
                        'allow_extra_fields' => true,
                        'multiple' => true,
                        'required' => false,
                    ])
                ->end()
}

class TagAdmin

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper->addIdentifier('name') ;
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('translations.name');;
    }

Entity Tag

class Tag implements TranslatableInterface
{

    use ORMBehaviors\Translatable\Translatable;

    public function __call($method, $arguments)
    {
        // If arguments are passed can't use the property accessor, so no free obj.name in twig.
        if (count($arguments)) {
            return $this->proxyCurrentLocaleTranslation($method, $arguments);
        } else {
            return \Symfony\Component\PropertyAccess\PropertyAccess::createPropertyAccessor()->getValue(
                $this->translate(),
                $method
            );
        }
    }

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="Recipe", mappedBy="tags")
     */
    private $recipies;

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

    public function getRecipies()
    {
        return $this->recipies;
    }

    public function setRecipies($recipies)
    {
        $this->recipies = $recipies;
    }

    public function getId()
    {
        return $this->id;
    }
    /**
     * @param string $locale
     */
    public function setLocale($locale)
    {
        $this->setCurrentLocale($locale);

        return $this;
    }

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

    public function __toString()
    {
        return $this->getId() ? (string)$this->getName() : '-';
    }

}

Entity TagTranslation

/**
 * @ORM\Entity
 * @ORM\Table(name="tag_translation")
 */
class TagTranslation
{
    use ORMBehaviors\Translatable\Translation;

    /**
     * @ORM\Column(type="string")
     */
    private $name;

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    function __toString()
    {
        return $this->getName() ? : 'Tag';
    }
}

Expected results

Doctrine Query with where claus

Actual results

Doctrine is not adding where clause:

1   0.53 ms 
SELECT count(DISTINCT t0_.id) AS sclr_0 FROM tag t0_ LEFT JOIN tag_translation t1_ ON t0_.id = t1_.translatable_id
Parameters: []

2   0.47 ms 
SELECT DISTINCT t0_.id AS id_0, t0_.id AS id_1 FROM tag t0_ LEFT JOIN tag_translation t1_ ON t0_.id = t1_.translatable_id ORDER BY t0_.id ASC LIMIT 10 OFFSET 0
Parameters: []

3   1.38 ms 
SELECT t0_.id AS id_0 FROM tag t0_ LEFT JOIN tag_translation t1_ ON t0_.id = t1_.translatable_id WHERE t0_.id IN (?) ORDER BY t0_.id ASC
Parameters: [ 0 => [ 0 => 9, 1 => 10, 2 => 11, 3 => 12, 4 => 13, 5 => 14, 6 => 15, 7 => 16, 8 => 17, 9 => 18 ] ]
lalop commented 7 years ago

Hello @rechengehirn did you solve your issue ?

pixelsucht commented 7 years ago

@lalop I fixed it with a callback. Like so:

            ->add('ingredient', 'sonata_type_model_autocomplete', [
                    'property' => 'translations.name',
                    'allow_extra_fields' => true,
                    'multiple' => false,
                    'required' => false,
                    'callback' => function ($admin, $property, $value) {
                        $datagrid = $admin->getDatagrid();
                        $queryBuilder = $datagrid->getQuery();
                        $queryBuilder
                            ->andWhere('s_translations.name LIKE :input')
                            ->setParameter('input', $value.'%');
                    }
                ])
pulzarraider commented 7 years ago

This bug report seems to be incomplete. Are you using a2lix/TranslationFormBundle?

pixelsucht commented 7 years ago

@pulzarraider Yes, I'm using a2lix/TranslationFormBundle

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.