<commit_before><?php

namespace Sidus\EAVModelBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\Query\Expr\Join;
use Sidus\EAVModelBundle\Model\FamilyInterface;

/**
 * Base repository for Data, not currently used
 *
 * @author <NAME> <<EMAIL>>
 */
class DataRepository extends EntityRepository
{
    /**
     * @param FamilyInterface $family
     * @param int|string      $reference
     * @return DataInterface|null
     * @throws NonUniqueResultException
     */
    public function findByIdentifier(FamilyInterface $family, $reference)
    {
        $identifierAttribute = $family->getAttributeAsIdentifier();
        if (!$identifierAttribute) {
            return $this->find($reference);
        }
        $qb = $this->createQueryBuilder('d');
        $dataBaseType = $identifierAttribute->getType()->getDatabaseType();
        $joinCondition = "(id.attributeCode = :attributeCode AND id.{$dataBaseType} = :reference)";
        $qb
            ->addSelect('values')
            ->join('d.values', 'id', Join::WITH, $joinCondition)
            ->join('d.values', 'values')
            ->setParameters([
                'attributeCode' => $identifierAttribute->getCode(),
                'reference' => $reference,
            ])
        ;

        return $qb->getQuery()->getOneOrNullResult();
    }

    /**
     * @param FamilyInterface $family
     * @return DataInterface
     */
    public function getInstance(FamilyInterface $family)
    {
        if (!$family->isSingleton()) {
            throw new \LogicException("Family {$family->getCode()} is not a singleton");
        }
        $qb = $this->createQueryBuilder('d')
            ->andWhere('d.family = :familyCode')
            ->addSelect('values')
            ->join('d.values', 'values')
            ->setParameters([
                'familyCode' => $family->getCode(),
            ])
        ;

        $instance = $qb->getQuery()->getOneOrNullResult();
        if (!$instance) {
            $dataClass = $family->getDataClass();
            $instance = new $dataClass($family);
        }

        return $instance;
    }
}
<commit_msg>findByIdentifier can now ignore families without attribute as identifiers<commit_after><?php

namespace Sidus\EAVModelBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\Query\Expr\Join;
use Sidus\EAVModelBundle\Model\FamilyInterface;

/**
 * Base repository for Data, not currently used
 *
 * @author <NAME> <<EMAIL>>
 */
class DataRepository extends EntityRepository
{
    /**
     * @param FamilyInterface $family
     * @param int|string      $reference
     * @param bool            $idFallback
     * @return null|DataInterface
     * @throws NonUniqueResultException
     */
    public function findByIdentifier(FamilyInterface $family, $reference, $idFallback = false)
    {
        $identifierAttribute = $family->getAttributeAsIdentifier();
        if (!$identifierAttribute) {
            if (!$idFallback) {
                return null;
            }

            return $this->findOneBy([
                'id' => $reference,
                'family' => $family->getCode(),
            ]);
        }
        $qb = $this->createQueryBuilder('d');
        $dataBaseType = $identifierAttribute->getType()->getDatabaseType();
        $joinCondition = "(id.attributeCode = :attributeCode AND id.{$dataBaseType} = :reference)";
        $qb
            ->addSelect('values')
            ->join('d.values', 'id', Join::WITH, $joinCondition)
            ->join('d.values', 'values')
            ->where('d.family = :familyCode')
            ->setParameters([
                'attributeCode' => $identifierAttribute->getCode(),
                'reference' => $reference,
                'familyCode' => $family->getCode(),
            ])
        ;

        return $qb->getQuery()->getOneOrNullResult();
    }

    /**
     * @param FamilyInterface $family
     * @return DataInterface
     */
    public function getInstance(FamilyInterface $family)
    {
        if (!$family->isSingleton()) {
            throw new \LogicException("Family {$family->getCode()} is not a singleton");
        }
        $qb = $this->createQueryBuilder('d')
            ->andWhere('d.family = :familyCode')
            ->addSelect('values')
            ->join('d.values', 'values')
            ->setParameters([
                'familyCode' => $family->getCode(),
            ])
        ;

        $instance = $qb->getQuery()->getOneOrNullResult();
        if (!$instance) {
            $dataClass = $family->getDataClass();
            $instance = new $dataClass($family);
        }

        return $instance;
    }
}

{'fraction_non_alphanumeric': 0.14267596702599875, 'fraction_numerical': 0.0, 'mean_word_length': 2.2455418381344305, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 67, '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': '4859651', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1264, 'n_words': 247}