<commit_before><?php

namespace Oro\Bundle\EntityBundle\Form\Handler;

use Doctrine\ORM\QueryBuilder;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;

use Oro\Bundle\EntityBundle\Form\Type\EntitySelectType;
use Oro\Bundle\EntityBundle\ORM\OroEntityManager;
use Oro\Bundle\FormBundle\Autocomplete\SearchHandlerInterface;
use Oro\Bundle\SearchBundle\Engine\Indexer;

class EntitySelectHandler implements SearchHandlerInterface
{
    /**
     * @var OroEntityManager
     */
    protected $entityManager;

    /**
     * @var string
     */
    protected $entityName;

    /**
     * @var string
     */
    protected $fieldName;

    /**
     * @var array
     */
    protected $properties = array('id', 'text');

    /**
     * @var boolean
     */
    private $hasMore;

    /**
     * @param OroEntityManager $entityManager
     */
    public function __construct(OroEntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /**
     * {@inheritdoc}
     */
    public function convertItem($item)
    {
        $result = array();

        if ($this->entityName && $this->fieldName) {
            $result[$this->fieldName] = $this->getPropertyValue($this->fieldName, $item);
            foreach ($this->properties as $property) {
                $result[$property] = $this->getPropertyValue($property, $item);
            }
        }

        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function search($query, $page, $perPage)
    {
        list($query, $targetEntity, $targetField) = explode(',', $query);

        $this->entityName = str_replace('_', '\\', $targetEntity);
        $this->fieldName  = $targetField;

        return $this->formatResult($this->searchEntities($query, $targetField));
    }

    /**
     * {@inheritdoc}
     */
    public function getProperties()
    {
        return $this->properties;
    }

    /**
     * {@inheritdoc}
     */
    public function getEntityName()
    {
        return $this->entityName;
    }

    /**
     * Search and return entities
     *
     * @param $search
     * @param $targetField
     * @return array
     */
    protected function searchEntities($search, $targetField)
    {
        /** @var QueryBuilder $queryBuilder */
        $queryBuilder = $this->entityManager->getRepository($this->entityName)->createQueryBuilder('e');

        $queryBuilder->where(
            $queryBuilder->expr()->like(
                'e.' . $targetField,
                $queryBuilder->expr()->literal($search . '%')
            )
        );

        return $queryBuilder->getQuery()->getArrayResult();
    }

    /**
     * @param array $items
     * @return array
     */
    protected function formatResult(array $items)
    {
        return array(
            'results' => $this->convertItems($items),<commit_msg>BAP-1869 Fix bug with search autocomplete for *:1 relation<commit_after><?php

namespace Oro\Bundle\EntityBundle\Form\Handler;

use Doctrine\ORM\QueryBuilder;

use Oro\Bundle\EntityExtendBundle\Extend\ExtendManager;
use Oro\Bundle\EntityExtendBundle\Tools\ExtendConfigDumper;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;

use Oro\Bundle\EntityBundle\Form\Type\EntitySelectType;
use Oro\Bundle\EntityBundle\ORM\OroEntityManager;
use Oro\Bundle\FormBundle\Autocomplete\SearchHandlerInterface;
use Oro\Bundle\SearchBundle\Engine\Indexer;

class EntitySelectHandler implements SearchHandlerInterface
{
    /**
     * @var OroEntityManager
     */
    protected $entityManager;

    /**
     * @var string
     */
    protected $entityName;

    /**
     * @var string
     */
    protected $fieldName;

    /**
     * @var array
     */
    protected $properties = array('id', 'text');

    /**
     * @var bool
     */
    private $hasMore;

    /**
     * @var bool
     */
    protected $isCustomField = false;

    /**
     * @param OroEntityManager $entityManager
     */
    public function __construct(OroEntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /**
     * {@inheritdoc}
     */
    public function convertItem($item)
    {
        $result = array();

        if ($this->entityName && $this->fieldName) {
            if ($this->isCustomField) {
                $result[$this->fieldName] = $this->getPropertyValue(
                    ExtendConfigDumper::PREFIX . $this->fieldName,
                    $item
                );
            } else {
                $result[$this->fieldName] = $this->getPropertyValue($this->fieldName, $item);
            }

            foreach ($this->properties as $property) {
                $result[$property] = $this->getPropertyValue($property, $item);
            }
        }

        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function search($query, $page, $perPage)
    {
        list($query, $targetEntity, $targetField) = explode(',', $query);

        $this->entityName = str_replace('_', '\\', $targetEntity);

        $fieldConfig = $this->entityManager->getExtendManager()->getConfigProvider()
            ->getConfig($this->entityName, $targetField);

        if ($fieldConfig->is('owner', ExtendManager::OWNER_CUSTOM)) {
            $this->isCustomField = true;
        }

        $this->fieldName  = $targetField;

        return $this->formatResult($this->searchEntities($query, $targetField));
    }

    /**
     * {@inheritdoc}
     */
    public function getProperties()
    {
        return $this->properties;
    }

    /**
     * {@inheritdoc}
     */
    public function getEntityName()
    {
        return $this->entityName;
    }

    /**
     * Search and return entities
     *
     * @param $search
     * @param $targetField
     * @return array
     */
    protected function searchEntities($search, $targetField)
    {
        /** @var QueryBuilder $queryBuilder */
        $queryBuilder = $this->entityManager->getRepository($this->entityName)->createQueryBuilder('e');

        if ($this->isCustomField) {
            $targetField = ExtendConfigDumper::PREFIX . $targetField;
        }

        $queryBuilder->where(
            $queryBuilder->expr()->like(
                'e.' . $targetField,
                $queryBuilder->expr()->literal($search . '%')
            )
        );

        return $queryBuilder->getQuery()->getArrayResult();
    }

    /**
     * @param array $items
     * @return array
     */
    protected function formatResult(array $items)
    {
        return array(
            'results' => $this->convertItems($items),
{'fraction_non_alphanumeric': 0.14035881200060304, 'fraction_numerical': 0.0007538067239559777, 'mean_word_length': 2.232943469785575, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3834508', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1859, 'n_words': 324}