Extract relations data from given entity fields.

@param array $data
323811
public function extractRelations(array &$data)
    {
        //Fetch all relations
        $relations = array_intersect_key($data, $this->schema);

        foreach ($relations as $name => $relation) {
            $this->relations[$name] = $relation;
            unset($data[$name]);
        }
    }
385909
protected function determineMorphModelsForRelationData(ModelRelationData $data)
    {
        if ($data->morphModels && count($data->morphModels)) {
            return $data->morphModels;
        }

        // Use information for other models in the CMS to find (some of) the related models
        $context = $this->enricher->getAllModelInformation();

        // If there is no context, silently ignore it.
        if ( ! $context) {
            // @codeCoverageIgnoreStart
            return [];
            // @codeCoverageIgnoreEnd
        }

        $models = [];

        foreach ($context as $information) {

            // If a relation is related to this model by a reverse morph relation,
            // it is an intended MorphTo targetable model.
            foreach ($information->relations as $relation) {

                if (    $this->info->modelClass() !== $relation->relatedModel
                    ||  ! in_array($relation->type, [ RelationType::MORPH_ONE, RelationType::MORPH_MANY ])
                ) {
                    continue;
                }

                $models[ $information->modelClass() ] = [];
            }
        }

        return $models;
    }
445279
public function getExportRecords($data)
    {
        $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default');

        //var_dump($data);

        $gameId         = $data['gameid'];

        $lastName       = $data['lastname'];
        $lastNameInput  = $data['lastname-input'];

        $firstName      = $data['firstname'];
        $firstNameInput     = $data['firstname-input'];

        $sexe           = $data['sexe'];

        $memberId       = $data['memberid'];
        $memberIdInput  = $data['memberid-input'];

        $email          = $data['email'];
        $emailInput     = $data['email-input'];

        $zipcode        = $data['zipcode'];
        $zipcodeInput   = $data['zipcode-input'];

        $birthdate      = $data['birthdate'];
        $dobStartIni    = $data['birthdate-start'];
        $dobEndIni      = $data['birthdate-end'];
        $dobEqualIni    = $data['birthdate-equal'];
        $dobStart       = \DateTime::createFromFormat('d/m/Y', $dobStartIni);
        $dobEnd         = \DateTime::createFromFormat('d/m/Y', $dobEndIni);
        $dobEqual       = \DateTime::createFromFormat('d/m/Y', $dobEqualIni);

        $hobbies        = $data['prizeCategory'];

        $optin          = $data['optin'];

        $optinPartner   = $data['optinpartner'];

        $suscribeDate   = $data['inscriptiondate'];
        $suscribeStartIni = $data['inscriptiondate-start'];
        $suscribeEndIni = $data['inscriptiondate-end'];
        $suscribeStart  = \DateTime::createFromFormat('d/m/Y', $suscribeStartIni);
        $suscribeEnd    = \DateTime::createFromFormat('d/m/Y', $suscribeEndIni);

        $hardbounce         = $data['hardbounce'];
        $hardbounceStartIni = $data['hardbounce-start'];
        $hardbounceEndIni = $data['hardbounce-end'];
        $hardbounceStart    = \DateTime::createFromFormat('d/m/Y', $hardbounceStartIni);
        $hardbounceEnd  = \DateTime::createFromFormat('d/m/Y', $hardbounceEndIni);

        $nbpart             = $data['nbpart'];
        $nbpartMin      = $data['nbpart-min'];
        $nbpartMax      = $data['nbpart-max'];
        $nbpartStartIni = $data['nbpart-start'];
        $nbpartEndIni   = $data['nbpart-end'];
        $nbpartStart    = \DateTime::createFromFormat('d/m/Y', $nbpartStartIni);
        $nbpartEnd      = \DateTime::createFromFormat('d/m/Y', $nbpartEndIni);

        $validatedemail = $data['validatedemail'];

        $player         = $data['player'];
        $goldfather     = $data['goldfather'];
        $brain          = $data['brain'];
        $ambassador     = $data['ambassador'];
        $anniversary    = $data['anniversary'];

        /*
         if($gameId != '')  : $gameidFilter = "AND u.id IN (SELECT r FROM PlaygroundGame\Entity\Entry e LEFT JOIN e.user r WHERE e.game = " . $gameId . " GROUP BY e.user)"; else : $gameidFilter = ""; endif;

        switch ($lastName){
        case 'all'      : $lastnameFilter = "WHERE u.lastname IS NOT NULL"; break;
        case 'start'    : $lastnameFilter = "WHERE u.lastname like '" . $lastNameInput . "%'"; break;
        case 'contain'  : $lastnameFilter = "WHERE u.lastname like '%" . $lastNameInput . "%'"; break;
        case 'equal'    : $lastnameFilter = "WHERE u.lastname = '" . $lastNameInput . "'"; break;
        }

        switch ($firstName){
        case 'all'      : $firstnameFilter = ""; break;
        case 'start'    : $firstnameFilter = "AND u.firstname like '" . $firstNameInput . "%'"; break;
        case 'contain'  : $firstnameFilter = "AND u.firstname like '%" . $firstNameInput . "%'"; break;
        case 'equal'    : $firstnameFilter = "AND u.firstname = '" . $firstNameInput . "'"; break;
        }

        switch ($sexe){
        case 'all'      : $sexeFilter = ""; break;
        case 'male'     : $sexeFilter = "AND u.title = 'M'"; break;
        case 'female'   : $sexeFilter = "AND u.title = 'Me'"; break;
        }

        if($memberId == 'equal') : $memberidFilter = "AND u.id = " . $memberIdInput; else : $memberidFilter = ""; endif;

        switch ($email){
        case 'all'      : $emailFilter = ""; break;
        case 'contain'  : $emailFilter = "AND u.email like '%" . $emailInput . "%'"; break;
        case 'equal'    : $emailFilter = "AND u.email = '" . $emailInput . "'"; break;
        }

        switch ($zipcode){
        case 'all'      : $zipcodeFilter = ""; break;
        case 'start'    : $zipcodeFilter = "AND u.postal_code like '" . $zipcodeInput . "%'"; break;
        case 'equal'    : $zipcodeFilter = "AND u.postal_code = '" . $zipcodeInput . "'"; break;
        }

        switch ($birthdate){
        case 'all'      : $birthdateFilter = ""; break;
        case 'between'  : $birthdateFilter = "AND (u.dob >= '" . $dobStart->format('Y-m-d') . "' AND u.dob <= '" . $dobEnd->format('Y-m-d') . "')"; break;
        case 'equal'    : $birthdateFilter = "AND u.dob = '" . $dobEqual->format('Y-m-d') . "'"; break;
        }

        if($hobbies != NULL) {
        foreach($hobbies as $key => $hobby) : $hobbiesSql[] = '\''.$hobby.'\''; endforeach;
        $hobbies = implode(',',$hobbiesSql);
        $hobbiesFilter = "AND u.id IN (SELECT t FROM PlaygroundGame\Entity\PrizeCategoryUser c LEFT JOIN c.user t WHERE c.prizeCategory IN (" . $hobbies . ") GROUP BY c.user)";
        } else {
        $hobbiesFilter = "";
        }

        switch ($optin){
        case 'all'      : $optinFilter = ""; break;
        case 'yes'      : $optinFilter = "AND u.optin = 1"; break;
        case 'no'       : $optinFilter = "AND u.optin = 0"; break;
        }

        switch ($optinPartner){
        case 'all'      : $optinPartnerFilter = ""; break;
        case 'yes'      : $optinPartnerFilter = "AND u.optinPartner = 1"; break;
        case 'no'       : $optinPartnerFilter = "AND u.optinPartner = 0"; break;
        }

        if($suscribeDate == 'all') : $suscribeFilter = ""; else : $suscribeFilter = "AND (u.created_at >= '" . $suscribeStart->format('Y-m-d') . " 00:00:00' AND u.created_at <= '" . $suscribeEnd->format('Y-m-d') . " 00:00:00')"; endif;

        switch ($hardbounce){
        case 'all'      : $hardbounceFilter = ""; break;
        case 'yes'      : $hardbounceFilter = "AND u.isHardbounce = 1"; break;
        case 'no'       : $hardbounceFilter = "AND u.isHardbounce = 0"; break;
        case 'between'  : $hardbounceFilter = "AND u.isHardbounce = 1 AND (u.created_at >= '" . $hardbounceStart->format('Y-m-d') . " 00:00:00' AND u.created_at <= '" . $hardbounceEnd->format('Y-m-d') . " 00:00:00')"; break;
        }

        switch ($nbpart) {
        case 'all'      : $nbpartFilter = ""; break;
        case 'betweennb': $nbpartFilter = "AND u.id IN (SELECT l FROM PlaygroundGame\Entity\Entry m LEFT JOIN m.user l GROUP BY m.user HAVING (COUNT(m.user) >= " . $nbpartMin . " AND COUNT(m.user) <= " . $nbpartMax . "))"; break;
        case 'between'  : $nbpartFilter = "AND u.id IN (SELECT l FROM PlaygroundGame\Entity\Entry m LEFT JOIN m.user l WHERE (m.created_at >= '" . $nbpartStart->format('Y-m-d') . " 00:00:00' AND m.created_at <= '" . $nbpartEnd->format('Y-m-d') . " 00:00:00') GROUP BY m.user)"; break;
        }

        switch ($validatedemail){
        case 'all'      : $validatedemailFilter = ""; break;
        case 'yes'      : $validatedemailFilter = "AND u.state IS NOT NULL"; break;
        case 'no'       : $validatedemailFilter = "AND u.state IS NULL"; break;
        }

        switch ($player){
        case 'all'      : $playerFilter = ""; break;
        case 'bronze'   : $playerFilter = "AND u.id IN (SELECT p FROM PlaygroundReward\Entity\Achievement h LEFT JOIN h.user p WHERE h.category = 'player' AND h.level = 1 GROUP BY h.user)"; break;
        case 'silver'   : $playerFilter = "AND u.id IN (SELECT p FROM PlaygroundReward\Entity\Achievement h LEFT JOIN h.user p WHERE h.category = 'player' AND h.level = 2 GROUP BY h.user)"; break;
        case 'gold'     : $playerFilter = "AND u.id IN (SELECT p FROM PlaygroundReward\Entity\Achievement h LEFT JOIN h.user p WHERE h.category = 'player' AND h.level = 3 GROUP BY h.user)"; break;
        }

        switch ($goldfather){
        case 'all'      : $goldfatherFilter = ""; break;
        case 'bronze'   : $goldfatherFilter = "AND u.id IN (SELECT s FROM PlaygroundReward\Entity\Achievement a LEFT JOIN a.user s WHERE a.category = 'goldfather' AND a.level = 1 GROUP BY a.user)"; break;
        case 'silver'   : $goldfatherFilter = "AND u.id IN (SELECT s FROM PlaygroundReward\Entity\Achievement a LEFT JOIN a.user s WHERE a.category = 'goldfather' AND a.level = 2 GROUP BY a.user)"; break;
        case 'gold'     : $goldfatherFilter = "AND u.id IN (SELECT s FROM PlaygroundReward\Entity\Achievement a LEFT JOIN a.user s WHERE a.category = 'goldfather' AND a.level = 3 GROUP BY a.user)"; break;
        }

        switch ($brain){
        case 'all'      : $brainFilter = ""; break;
        case 'bronze'   : $brainFilter = "AND u.id IN (SELECT b FROM PlaygroundReward\Entity\Achievement n LEFT JOIN n.user b WHERE n.category = 'brain' AND n.level = 1 GROUP BY n.user)"; break;
        case 'silver'   : $brainFilter = "AND u.id IN (SELECT b FROM PlaygroundReward\Entity\Achievement n LEFT JOIN n.user b WHERE n.category = 'brain' AND n.level = 2 GROUP BY n.user)"; break;
        case 'gold'     : $brainFilter = "AND u.id IN (SELECT b FROM PlaygroundReward\Entity\Achievement n LEFT JOIN n.user b WHERE n.category = 'brain' AND n.level = 3 GROUP BY n.user)"; break;
        }

        switch ($ambassador){
        case 'all'      : $ambassadorFilter = ""; break;
        case 'bronze'   : $ambassadorFilter = "AND u.id IN (SELECT d FROM PlaygroundReward\Entity\Achievement f LEFT JOIN f.user d WHERE f.category = 'ambassador' AND f.level = 1 GROUP BY f.user)"; break;
        case 'silver'   : $ambassadorFilter = "AND u.id IN (SELECT d FROM PlaygroundReward\Entity\Achievement f LEFT JOIN f.user d WHERE f.category = 'ambassador' AND f.level = 2 GROUP BY f.user)"; break;
        case 'gold'     : $ambassadorFilter = "AND u.id IN (SELECT d FROM PlaygroundReward\Entity\Achievement f LEFT JOIN f.user d WHERE f.category = 'ambassador' AND f.level = 3 GROUP BY f.user)"; break;
        }

        switch ($anniversary){
        case 'all'      : $anniversaryFilter = ""; break;
        case 'bronze'   : $anniversaryFilter = "AND u.id IN (SELECT g FROM PlaygroundReward\Entity\Achievement j LEFT JOIN j.user g WHERE j.category = 'anniversary' AND j.level = 1 GROUP BY j.user)"; break;
        case 'silver'   : $anniversaryFilter = "AND u.id IN (SELECT g FROM PlaygroundReward\Entity\Achievement j LEFT JOIN j.user g WHERE j.category = 'anniversary' AND j.level = 2 GROUP BY j.user)"; break;
        case 'gold'     : $anniversaryFilter = "AND u.id IN (SELECT g FROM PlaygroundReward\Entity\Achievement j LEFT JOIN j.user g WHERE j.category = 'anniversary' AND j.level = 3 GROUP BY j.user)"; break;
        }

        $query = $em->createQuery('
                SELECT u FROM PlaygroundUser\Entity\User u
                ' . $lastnameFilter . '
                ' . $firstnameFilter . '
                ' . $sexeFilter . '
                ' . $memberidFilter . '
                ' . $emailFilter . '
                ' . $zipcodeFilter . '
                ' . $birthdateFilter . '
                ' . $hobbiesFilter . '
                ' . $optinFilter . '
                ' . $optinPartnerFilter . '
                ' . $suscribeFilter . '
                ' . $hardbounceFilter . '
                ' . $gameidFilter . '
                ' . $nbpartFilter . '
                ' . $validatedemailFilter . '
                ' . $playerFilter . '
                ' . $goldfatherFilter . '
                ' . $brainFilter . '
                ' . $ambassadorFilter . '
                ' . $anniversaryFilter . '
                    
                ');
        */

        //$sql = "SELECT distinct u.user_id, u.username, u.email";
        $sql = "SELECT distinct u.user_id, u.username, u.email, u.title, u.firstname, u.lastname, u.dob, u.address, u.address2, u.postal_code, u.city, u.telephone, u.mobile, u.optin_partner, u.registration_source, u.optin, u.created_at, u.state";

        /*
         if($validatedemail != 'all'){
        $sql .= ', u.state';
        }

        if($birthdate != 'all'){
        $sql .= ', u.dob';
        }

        if($suscribeDate != 'all'){
        $sql .= ', u.created_at';
        }

        if($optin != 'all'){
        $sql .= ', u.optin';
        }
        */

        if ($hardbounce != 'all') {
            $sql .= ', u.date_set_hardbounce';
        }

        $sql .= " FROM user u";

        if (($gameId != '')||($nbpart == 'between')) {
            $sql .= " INNER JOIN game_entry ge ON ge.user_id = u.user_id";
        }

        if ($hobbies != null) {
            $sql .= " INNER JOIN game_prize_category_user pcu ON pcu.user_id = u.user_id";
        }

        if ($player != 'all') {
            $sql .= " INNER JOIN reward_achievement ra1 ON ra1.user_id = u.user_id AND ra1.category = 'player'";
        }
        if ($goldfather != 'all') {
            $sql .= " INNER JOIN reward_achievement ra2 ON ra2.user_id = u.user_id AND ra2.category = 'goldfather'";
        }
        if ($brain != 'all') {
            $sql .= " INNER JOIN reward_achievement ra3 ON ra3.user_id = u.user_id AND ra3.category = 'brain'";
        }
        if ($ambassador != 'all') {
            $sql .= " INNER JOIN reward_achievement ra4 ON ra4.user_id = u.user_id AND ra4.category = 'ambassador'";
        }
        if ($anniversary != 'all') {
            $sql .= " INNER JOIN reward_achievement ra5 ON ra5.user_id = u.user_id AND ra5.category = 'anniversary'";
        }

        $sql .= " WHERE 1=1";

        switch ($lastName) {
            case 'start':
                $sql .= " AND u.lastname like '" . $lastNameInput . "%'";
                break;
            case 'contain':
                $sql .= " AND u.lastname like '%" . $lastNameInput . "%'";
                break;
            case 'equal':
                $sql .=  " AND u.lastname = '" . $lastNameInput . "'";
                break;
        }

        switch ($firstName) {
            case 'start':
                $sql .= " AND u.firstname like '" . $firstNameInput . "%'";
                break;
            case 'contain':
                $sql .= " AND u.firstname like '%" . $firstNameInput . "%'";
                break;
            case 'equal':
                $sql .= " AND u.firstname = '" . $firstNameInput . "'";
                break;
        }

        switch ($sexe) {
            case 'male':
                $sql .= " AND u.title = 'M'";
                break;
            case 'female':
                $sql .= " AND u.title = 'Me'";
                break;
        }

        if ($memberId == 'equal') {
            $sql .= " AND u.user_id = " . $memberIdInput;
        }

        switch ($email) {
            case 'contain':
                $sql .= " AND u.email like '%" . $emailInput . "%'";
                break;
            case 'equal':
                $sql .= " AND u.email = '" . $emailInput . "'";
                break;
        }

        switch ($zipcode) {
            case 'start':
                $sql .= " AND u.postal_code like '" . $zipcodeInput . "%'";
                break;
            case 'equal':
                $sql .= " AND u.postal_code = '" . $zipcodeInput . "'";
                break;
        }

        switch ($birthdate) {
            case 'between':
                $sql .= " AND (u.dob >= '" . $dobStart->format('Y-m-d') . "' AND u.dob <= '" . $dobEnd->format('Y-m-d') . "')";
                break;
            case 'equal':
                $sql .= " AND u.dob = '" . $dobEqual->format('Y-m-d') . "'";
                break;
        }


        switch ($optin) {
            case 'yes':
                $sql .= " AND u.optin = 1";
                break;
            case 'no':
                $sql .= " AND u.optin = 0";
                break;
        }

        switch ($optinPartner) {
            case 'yes':
                $sql .= " AND u.optin_partner = 1";
                break;
            case 'no':
                $sql .= " AND u.optin_partner = 0";
                break;
        }

        if ($suscribeDate != 'all') {
            $sql .= " AND (u.created_at >= '" . $suscribeStart->format('Y-m-d') . " 00:00:00' AND u.created_at <= '" . $suscribeEnd->format('Y-m-d') . " 00:00:00')";
        }

        switch ($validatedemail) {
            case 'yes':
                $sql .= " AND u.state IS NOT NULL";
                break;
            case 'no':
                $sql .= " AND u.state IS NULL";
                break;
        }




        if ($gameId != '') {
            $sql .= " AND ge.game_id=$gameId";
        }
        if ($nbpart == 'between') {
            $sql .= " AND ge.created_at >= '" . $nbpartStart->format('Y-m-d') . " 00:00:00' AND ge.created_at <= '" . $nbpartEnd->format('Y-m-d') . " 00:00:00'";
        }

        //à retirer si la performance est mauvaise
        if ($nbpart == 'betweennb') {
            $sql .= " AND u.user_id IN (SELECT ge.user_id FROM game_entry ge LEFT JOIN user v ON (v.user_id = ge.user_id) GROUP BY ge.user_id HAVING (COUNT(ge.user_id) >= " . $nbpartMin . " AND COUNT(ge.user_id) <= " . $nbpartMax . "))";
        }



        if ($hobbies != null) {
            foreach ($hobbies as $key => $hobby) :
                $hobbiesSql[] = '\''.$hobby.'\'';

            endforeach;
                
            $hobbies = implode(',', $hobbiesSql);
            $sql .= " AND pcu.prize_category_id IN (" . $hobbies . ")";
        }

        switch ($player) {
            case 'bronze':
                $sql .= " AND ra1.level=1";
                break;
            case 'silver':
                $sql .= " AND ra1.level=2";
                break;
            case 'gold':
                $sql .= " AND ra1.level=3";
                break;
        }

        switch ($goldfather) {
            case 'bronze':
                $sql .= " AND ra2.level=1";
                break;
            case 'silver':
                $sql .= " AND ra2.level=2";
                break;
            case 'gold':
                $sql .= " AND ra2.level=3";
                break;
        }

        switch ($brain) {
            case 'bronze':
                $sql .= " AND ra3.level=1";
                break;
            case 'silver':
                $sql .= " AND ra3.level=2";
                break;
            case 'gold':
                $sql .= " AND ra3.level=3";
                break;
        }

        switch ($ambassador) {
            case 'bronze':
                $sql .= " AND ra4.level=1";
                break;
            case 'silver':
                $sql .= " AND ra4.level=2";
                break;
            case 'gold':
                $sql .= " AND ra4.level=3";
                break;
        }

        switch ($anniversary) {
            case 'bronze':
                $sql .= " AND ra5.level=1";
                break;
            case 'silver':
                $sql .= " AND ra5.level=2";
                break;
            case 'gold':
                $sql .= " AND ra5.level=3";
                break;
        }

        $rsm = new ResultSetMapping();

        $query = $em->createNativeQuery($sql, $rsm);

        //echo $sql;

        $method = new \ReflectionMethod($query, '_doExecute');
        $method->setAccessible(true);
        $result = $method->invokeArgs($query, array());
        //$result = $query->execute(array(),\Doctrine\ORM\Query::HYDRATE_ARRAY);
        return $result;
    }
307752
private function extractContentIds($data)
    {
        $ids = [];

        foreach ($data->response->docs as $doc) {
            $ids[] = $doc->content_id_id;
        }

        return $ids;
    }
322447
private static function buildRelationships(array $data): MapInterface
    {
        $relationships = new Map('int', Relationship::class);

        foreach ($data as $response) {
            foreach ($response['graph']['relationships'] as $rel) {
                $relationships = $relationships->put(
                    (int) $rel['id'],
                    new Relationship\Relationship(
                        new Id((int) $rel['id']),
                        new Type($rel['type']),
                        new Id((int) $rel['startNode']),
                        new Id((int) $rel['endNode']),
                        self::buildProperties($rel['properties'])
                    )
                );
            }
        }

        return $relationships;
    }
605555
private function loadRelationships($data): void
    {
        $isPaginated = $data instanceof PaginatedDataInterface;
        $hasData     = ($isPaginated === true && empty($data->getData()) === false) ||
            ($isPaginated === false && $data !== null);

        if ($hasData === true && $this->hasIncludes() === true) {
            $modelStorage = $this->getFactory()->createModelStorage($this->getModelSchemas());
            $modelsAtPath = $this->getFactory()->createTagStorage();

            // we gonna send these objects via function params so it is an equivalent for &array
            $classAtPath = new ArrayObject();
            $idsAtPath   = new ArrayObject();

            $registerModelAtRoot = function ($model) use ($modelStorage, $modelsAtPath, $idsAtPath): void {
                self::registerModelAtPath(
                    $model,
                    static::ROOT_PATH,
                    $this->getModelSchemas(),
                    $modelStorage,
                    $modelsAtPath,
                    $idsAtPath
                );
            };

            $model = null;
            if ($isPaginated === true) {
                foreach ($data->getData() as $model) {
                    $registerModelAtRoot($model);
                }
            } else {
                $model = $data;
                $registerModelAtRoot($model);
            }
            assert($model !== null);
            $classAtPath[static::ROOT_PATH] = get_class($model);

            foreach ($this->getPaths($this->getIncludes()) as list ($parentPath, $childPaths)) {
                $this->loadRelationshipsLayer(
                    $modelsAtPath,
                    $classAtPath,
                    $idsAtPath,
                    $modelStorage,
                    $parentPath,
                    $childPaths
                );
            }
        }
    }
7156
protected function updateRelation($relationsData)
    {
        foreach ($relationsData as $name => $values) {
            if (!method_exists($this->model, $name)) {
                continue;
            }

            $relation = $this->model->$name();

            $oneToOneRelation = $relation instanceof Relations\HasOne
                || $relation instanceof Relations\MorphOne
                || $relation instanceof Relations\BelongsTo;

            $prepared = $this->prepareUpdate([$name => $values], $oneToOneRelation);

            if (empty($prepared)) {
                continue;
            }

            switch (true) {
                case $relation instanceof Relations\BelongsToMany:
                case $relation instanceof Relations\MorphToMany:
                    if (isset($prepared[$name])) {
                        $relation->sync($prepared[$name]);
                    }
                    break;
                case $relation instanceof Relations\HasOne:

                    $related = $this->model->$name;

                    // if related is empty
                    if (is_null($related)) {
                        $related = $relation->getRelated();
                        $qualifiedParentKeyName = $relation->getQualifiedParentKeyName();
                        $localKey = Arr::last(explode('.', $qualifiedParentKeyName));
                        $related->{$relation->getForeignKeyName()} = $this->model->{$localKey};
                    }

                    foreach ($prepared[$name] as $column => $value) {
                        $related->setAttribute($column, $value);
                    }

                    $related->save();
                    break;
                case $relation instanceof Relations\BelongsTo:
                case $relation instanceof Relations\MorphTo:

                    $parent = $this->model->$name;

                    // if related is empty
                    if (is_null($parent)) {
                        $parent = $relation->getRelated();
                    }

                    foreach ($prepared[$name] as $column => $value) {
                        $parent->setAttribute($column, $value);
                    }

                    $parent->save();

                    // When in creating, associate two models
                    $foreignKeyMethod = (app()->version() < '5.8.0') ? 'getForeignKey' : 'getForeignKeyName';
                    if (!$this->model->{$relation->{$foreignKeyMethod}()}) {
                        $this->model->{$relation->{$foreignKeyMethod}()} = $parent->getKey();

                        $this->model->save();
                    }

                    break;
                case $relation instanceof Relations\MorphOne:
                    $related = $this->model->$name;
                    if (is_null($related)) {
                        $related = $relation->make();
                    }
                    foreach ($prepared[$name] as $column => $value) {
                        $related->setAttribute($column, $value);
                    }
                    $related->save();
                    break;
                case $relation instanceof Relations\HasMany:
                case $relation instanceof Relations\MorphMany:

                    foreach ($prepared[$name] as $related) {
                        /** @var Relations\Relation $relation */
                        $relation = $this->model()->$name();

                        $keyName = $relation->getRelated()->getKeyName();

                        $instance = $relation->findOrNew(Arr::get($related, $keyName));

                        if ($related[static::REMOVE_FLAG_NAME] == 1) {
                            $instance->delete();

                            continue;
                        }

                        Arr::forget($related, static::REMOVE_FLAG_NAME);

                        $instance->fill($related);

                        $instance->save();
                    }

                    break;
            }
        }
    }
414346
function(data, payload) {
        if (data.relationships) {
          this.extractRelationships(data.relationships, data);
        }
        payload[data.type] = data;
        delete data.type;
      }
126953
public function addRelations($relations)
	{
		if(is_numeric($relations))
		{
			
			$relations = intval($relations);
			$this->relations = $relations;
		}
		else
		{
			$relations = ( is_array($relations) ) ? $relations : explode(',', strval($relations));
			foreach ($relations as $prop)
			{
				if( ! in_array($prop, $this->relations) )
				{
					$this->relations[] = trim($prop);
				}
			}
		}

		if($this instanceof Collection)
		{
			foreach ($this->data as $model)
			{
				$model->addRelations($this->relations);
			}
		}
	}
668073
public function queueUnresolvedObjects($data, $relations)
	{
		if( is_object($data) )
		{

			if( $data instanceof Model )
			{
				$data = $data->getData();
			}
			if( !$this->resolved($data) )
			{
				$this->addToQueue($data, $relations);
				return;
			}

			$data = get_object_vars($data);

		}

		if( is_array($data) )
		{
			foreach ($data as $key => $value)
			{
				if(is_int($key))
				{
					$this->queueUnresolvedObjects($value, $relations);
				}
				if($this->hasRelation($key, $relations))
				{
					$nestedRelations = $this->getNestedRelations($key, $relations);
					$this->queueUnresolvedObjects($value, $nestedRelations);
				}
				
			}
		}
	}
1139813
public function determineFormStoreStrategy(ModelRelationData $data)
    {
        $type       = null;
        $parameters = [];

        // Determine strategy alias
        switch ($data->type) {

            case RelationType::BELONGS_TO:
            case RelationType::BELONGS_TO_THROUGH:
            case RelationType::MORPH_ONE:
            case RelationType::HAS_ONE:
                $type = FormStoreStrategy::RELATION_SINGLE_KEY;
                break;

            case RelationType::BELONGS_TO_MANY:
            case RelationType::HAS_MANY:
            case RelationType::MORPH_MANY:
            case RelationType::MORPH_TO_MANY:
            case RelationType::MORPHED_BY_MANY:
                $type = FormStoreStrategy::RELATION_PLURAL_KEYS;
                break;

            case RelationType::MORPH_TO:
                $type = FormStoreStrategy::RELATION_SINGLE_MORPH;
                break;
        }


        // Determine parameters

        if ($data->translated) {
            $parameters[] = 'translated';
        }

        if (count($parameters)) {
            $type .= ':' . implode(',', $parameters);
        }

        return $type;
    }
263994
private function findRelations(string $class, array $relations): array
    {
        $results = [];

        foreach ($relations as $relation) {
            $definition = $relation->getDefinition();
            if ($definition->sourceContext()->getClass() === $class) {
                $name = $definition->getName();
                $types = [];

                $relationType = $definition->getType();
                if (in_array($relationType, [RecordEntity::MANY_TO_MANY, RecordEntity::HAS_MANY])) {
                    $types[] = $definition->getTarget() . '[]';
                } else {
                    $types[] = $definition->getTarget();
                }

                $types[] = $this->relationsConfig->relationClass($relationType,
                    RelationsConfig::ACCESS_CLASS);

                $options = $definition->getOptions();
                if (array_key_exists(RecordEntity::NULLABLE, $options)
                    && true === $options[RecordEntity::NULLABLE]
                ) {
                    $types[] = 'null';
                }

                $results[$name] = $types;
            }
        }

        return $results;
    }
1031110
protected function extractTypeValues(array $data, array $types)
    {
        $typeValues = array();

        foreach ($data as $k => $_) {
            $typeValues[] = isset($types[$k])
                ? $types[$k]
                : \PDO::PARAM_STR;
        }

        return $typeValues;
    }
334655
public function takeData($data)
    {
        if ($this->debug === true) {
            $fbRelations = [];
            $fbColumns   = $this->getColumnsInfo();
            foreach ($this->getRelationsInfo() as $relation) {
                if (is_array($relation) && isset($relation['url'])) {
                    $fbRelations[$relation['url']] = $relation['url'];
                }
            }
            if (count($fbColumns)) {
                foreach ($data as $key => $value) {
                    if (!array_key_exists($key, $fbColumns)) {

                        if (!array_key_exists($key, $fbRelations)) {
                            $this->addStatusMessage(sprintf('unknown column %s for evidence %s',
                                    $key, $this->getEvidence()), 'warning');
                        } else {
                            if (!is_array($value)) {
                                $this->addStatusMessage(sprintf('subevidence %s in evidence %s must bee an array',
                                        $key, $this->getEvidence()), 'warning');
                            }
                        }
                    }
                }
            }
        }
        return parent::takeData($data);
    }
1172444
private function getRelations()
	{
        if (is_null($this->relations)) {
            $builder = new RelationBuilder();
            $this->relations = $builder->build($this->data);
        }

        return $this->relations;
    }
25896
private function convertRelationships(EntityMetadata $metadata, array $data)
    {
        foreach ($metadata->getRelationships() as $key => $relMeta) {
            if (!isset($data[$key])) {
                continue;
            }
            if (true === $relMeta->isMany() && !is_array($data[$key])) {
                throw PersisterException::badRequest(sprintf('Relationship key "%s" is a reference many. Expected record data type of array, "%s" found on model "%s" for identifier "%s"', $key, gettype($data[$key]), $type, $identifier));
            }
            $references = $relMeta->isOne() ? [$data[$key]] : $data[$key];

            $extracted = [];
            foreach ($references as $reference) {
                $extracted[] =  $this->extractRelationship($relMeta, $reference);
            }
            $data[$key] = $relMeta->isOne() ? reset($extracted) : $extracted;
        }
        return $data;
    }
239776
public function load($data, $relations = [])
	{
		$this->clearQueue();

		$relations = $this->parseRelations($relations);	

		$this->queueUnresolvedObjects($data, $relations);

		$this->loadQueue();
	}