src/CRUDlex/MySQLData.php
protected function fetchReferencesForField(array &$entities, $field)
    {
        $nameField    = $this->definition->getSubTypeField($field, 'reference', 'nameField');
        $queryBuilder = $this->database->createQueryBuilder();

        $ids = $this->getReferenceIds($entities, $field);

        $referenceEntity = $this->definition->getSubTypeField($field, 'reference', 'entity');
        $table           = $this->definition->getService()->getData($referenceEntity)->getDefinition()->getTable();
        $queryBuilder
            ->from('`'.$table.'`', '`'.$table.'`')
            ->where('id IN (?)')
        ;
        $this->addSoftDeletionToQuery($this->definition, $queryBuilder);
        if ($nameField) {
            $queryBuilder->select('id', $nameField);
        } else {
            $queryBuilder->select('id');
        }

        $queryBuilder->setParameter(0, $ids, Connection::PARAM_STR_ARRAY);

        $queryResult = $queryBuilder->execute();
        $rows        = $queryResult->fetchAll(\PDO::FETCH_ASSOC);
        $amount      = count($entities);
        foreach ($rows as $row) {
            for ($i = 0; $i < $amount; ++$i) {
                if ($entities[$i]->get($field) == $row['id']) {
                    $value = ['id' => $entities[$i]->get($field)];
                    if ($nameField) {
                        $value['name'] = $row[$nameField];
                    }
                    $entities[$i]->set($field, $value);
                }
            }
        }
    }
protected function fetchReferencesForField(array &$entities, $field)
    {
        $nameField    = $this->definition->getSubTypeField($field, 'reference', 'nameField');
        $queryBuilder = $this->database->createQueryBuilder();

        $ids = $this->getReferenceIds($entities, $field);

        $referenceEntity = $this->definition->getSubTypeField($field, 'reference', 'entity');
        $table           = $this->definition->getService()->getData($referenceEntity)->getDefinition()->getTable();
        $queryBuilder
            ->from('`'.$table.'`', '`'.$table.'`')
            ->where('id IN (?)')
        ;
        $this->addSoftDeletionToQuery($this->definition, $queryBuilder);
        if ($nameField) {
            $queryBuilder->select('id', $nameField);
        } else {
            $queryBuilder->select('id');
        }

        $queryBuilder->setParameter(0, $ids, Connection::PARAM_STR_ARRAY);

        $queryResult = $queryBuilder->execute();
        $rows        = $queryResult->fetchAll(\PDO::FETCH_ASSOC);
        $amount      = count($entities);
        foreach ($rows as $row) {
            for ($i = 0; $i < $amount; ++$i) {
                if ($entities[$i]->get($field) == $row['id']) {
                    $value = ['id' => $entities[$i]->get($field)];
                    if ($nameField) {
                        $value['name'] = $row[$nameField];
                    }
                    $entities[$i]->set($field, $value);
                }
            }
        }
    }
protected function fetchReferencesForField ( array & $ entities , $ field ) { $ nameField = $ this -> definition -> getSubTypeField ( $ field , 'reference' , 'nameField' ) ; $ queryBuilder = $ this -> database -> createQueryBuilder ( ) ; $ ids = $ this -> getReferenceIds ( $ entities , $ field ) ; $ referenceEntity = $ this -> definition -> getSubTypeField ( $ field , 'reference' , 'entity' ) ; $ table = $ this -> definition -> getService ( ) -> getData ( $ referenceEntity ) -> getDefinition ( ) -> getTable ( ) ; $ queryBuilder -> from ( '`' . $ table . '`' , '`' . $ table . '`' ) -> where ( 'id IN (?)' ) ; $ this -> addSoftDeletionToQuery ( $ this -> definition , $ queryBuilder ) ; if ( $ nameField ) { $ queryBuilder -> select ( 'id' , $ nameField ) ; } else { $ queryBuilder -> select ( 'id' ) ; } $ queryBuilder -> setParameter ( 0 , $ ids , Connection :: PARAM_STR_ARRAY ) ; $ queryResult = $ queryBuilder -> execute ( ) ; $ rows = $ queryResult -> fetchAll ( \ PDO :: FETCH_ASSOC ) ; $ amount = count ( $ entities ) ; foreach ( $ rows as $ row ) { for ( $ i = 0 ; $ i < $ amount ; ++ $ i ) { if ( $ entities [ $ i ] -> get ( $ field ) == $ row [ 'id' ] ) { $ value = [ 'id' => $ entities [ $ i ] -> get ( $ field ) ] ; if ( $ nameField ) { $ value [ 'name' ] = $ row [ $ nameField ] ; } $ entities [ $ i ] -> set ( $ field , $ value ) ; } } } }
Adds the id and name of referenced entities to the given entities. The
reference field is before the raw id of the referenced entity and after
the fetch, it's an array with the keys id and name.

@param Entity[] &$entities
the entities to fetch the references for
@param string $field
the reference field
Adds the id and name of referenced entities to the given entities . The reference field is before the raw id of the referenced entity and after the fetch it s an array with the keys id and name .