Lookup the new or existing model for this row.

@return ImportModel
975687
private function lookupModel(&$row)
    {
        $query = new ImportModel();

        // Set the connection and table.
        $query->setConnection($this->connection($this->argument('dataset')));
        $query->setTable(sprintf('data_%s', array_get($this->config, 'table')));

        $count_keys = 0;

        foreach (array_get($this->config, 'import_keys', []) as $key) {
            if (!is_null($value = array_get($row, $key, null))) {
                $query = $query->where($key, $value);
                $count_keys++;
            }
        }

        if ($count_keys > 0) {
            $model = $query->first();
        }

        if (is_null($model)) {
            $model = new ImportModel();
        }

        // Ensure connection and table are set.
        $model->setConnection($this->connection($this->argument('dataset')));
        $model->setTable(sprintf('data_%s', array_get($this->config, 'table')));

        return $model;
    }
1082732
public function getModel() {
        $model = new $this->_options['model']();
        foreach ($this->_options['bindings'] as $binding => $bindingOptions) {
            if (is_numeric($binding)) {
                $model->bindModel($bindingOptions);
            } elseif (is_string($binding)) {
                $model->bindModel($binding, $bindingOptions);
            }
        }
        return $model;
    }
38761
public function getTransactionsManager() {
		if ($this->_transactionsManager === null) {
			if ($this->hasLocator()) {
				$locator = $this->getLocator();

				if ($locator->has(self::PHSRV_TRANSACTIONS)) {
					$this->_transactionsManager = $locator->get(self::PHSRV_TRANSACTIONS);
				}
			}
			
			if ($this->_transactionsManager === null) {
				$this->_transactionsManager = new TransactionsManager($this->getTableModelDi());
			}
			
			$this->_transactionsManager->setDbService($this->getAdapterName());
		}
		
		return $this->_transactionsManager;
	}
244313
public function import()
    {
        $result = [
            'languages' => ['new' => 0, 'updated' => 0],
            'languageSources' => ['new' => 0, 'updated' => 0],
            'languageTranslations' => ['new' => 0, 'updated' => 0],
        ];

        $data = $this->parseImportFile();

        /** @var Language[] $languages */
        $languages = Language::find()->indexBy('language_id')->all();

        foreach ($data['languages'] as $importedLanguage) {
            if (isset($languages[$importedLanguage['language_id']])) {
                $language = $languages[$importedLanguage['language_id']];
            } else {
                $language = new Language();
            }

            //cast integers
            $importedLanguage['status'] = (int) $importedLanguage['status'];

            $language->attributes = $importedLanguage;
            if (count($language->getDirtyAttributes())) {
                $saveType = $language->isNewRecord ? 'new' : 'updated';
                if ($language->save()) {
                    ++$result['languages'][$saveType];
                } else {
                    $this->throwInvalidModelException($language);
                }
            }
        }

        /** @var LanguageSource[] $languageSources */
        $languageSources = LanguageSource::find()->indexBy('id')->all();

        /** @var LanguageTranslate[] $languageTranslations */
        $languageTranslations = LanguageTranslate::find()->all();

        /*
         *  Create 2 dimensional array for current and imported translation, first index by LanguageSource->id
         *  and than indexed by LanguageTranslate->language.
         *  E.g.: [
         *      id => [
         *          language => LanguageTranslate (for $languageTranslations) / Array (for $importedLanguageTranslations)
         *          ...
         *      ]
         *      ...
         * ]
         */
        $languageTranslations = ArrayHelper::map($languageTranslations, 'language', function ($languageTranslation) {
            return $languageTranslation;
        }, 'id');
        $importedLanguageTranslations = ArrayHelper::map($data['languageTranslations'], 'language', function ($languageTranslation) {
            return $languageTranslation;
        }, 'id');

        foreach ($data['languageSources'] as $importedLanguageSource) {
            $languageSource = null;

            //check if id exist and if category and messages are matching
            if (isset($languageSources[$importedLanguageSource['id']]) &&
                ($languageSources[$importedLanguageSource['id']]->category == $importedLanguageSource['category']) &&
                ($languageSources[$importedLanguageSource['id']]->message == $importedLanguageSource['message'])
            ) {
                $languageSource = $languageSources[$importedLanguageSource['id']];
            }

            if (is_null($languageSource)) {
                //no match by id, search by message
                foreach ($languageSources as $languageSourceSearch) {
                    if (($languageSourceSearch->category == $importedLanguageSource['category']) &&
                        ($languageSourceSearch->message == $importedLanguageSource['message'])
                    ) {
                        $languageSource = $languageSourceSearch;
                        break;
                    }
                }
            }

            if (is_null($languageSource)) {
                //still no match, create new
                $languageSource = new LanguageSource([
                    'category' => $importedLanguageSource['category'],
                    'message' => $importedLanguageSource['message'],
                ]);

                if ($languageSource->save()) {
                    ++$result['languageSources']['new'];
                } else {
                    $this->throwInvalidModelException($languageSource);
                }
            }

            //do we have translations for the current source?
            if (isset($importedLanguageTranslations[$importedLanguageSource['id']])) {
                //loop through the translations for the current source
                foreach ($importedLanguageTranslations[$importedLanguageSource['id']] as $importedLanguageTranslation) {
                    $languageTranslate = null;

                    //is there already a translation for this souce
                    if (isset($languageTranslations[$languageSource->id]) &&
                        isset($languageTranslations[$languageSource->id][$importedLanguageTranslation['language']])
                    ) {
                        $languageTranslate = $languageTranslations[$languageSource->id][$importedLanguageTranslation['language']];
                    }

                    //no translation found, create a new one
                    if (is_null($languageTranslate)) {
                        $languageTranslate = new LanguageTranslate();
                    }

                    $languageTranslate->attributes = $importedLanguageTranslation;

                    //overwrite the id because the $languageSource->id might be different from the $importedLanguageTranslation['id']
                    $languageTranslate->id = $languageSource->id;

                    if (count($languageTranslate->getDirtyAttributes())) {
                        $saveType = $languageTranslate->isNewRecord ? 'new' : 'updated';
                        if ($languageTranslate->save()) {
                            ++$result['languageTranslations'][$saveType];
                        } else {
                            $this->throwInvalidModelException($languageTranslate);
                        }
                    }
                }
            }
        }

        return $result;
    }
466105
public function importAction()
    {
        $success = null;
        $request = $this->getRequest();
        $data    = $request->getPost();
        $form    = $this->getServiceLocator()
                        ->get( 'Form' )
                        ->create( 'Grid\Paragraph\ChangeLayout\Import', array(
                            'returnUri' => $request->getQuery( 'returnUri' ),
                        ) );

        $form->setAttribute(
            'action',
            $this->url()
                 ->fromRoute( 'Grid\Paragraph\ChangeLayout\Import', array(
                     'locale'       => (string) $this->locale(),
                     'paragraphId'  => $this->paragraphId,
                     'returnUri'    => $request->getQuery( 'returnUri' ),
                 ) )
        );

        if ( $request->isPost() )
        {
            $form->setData( $data );

            if ( $form->isValid() )
            {
                $data       = $form->getData();
                $beforeId   = $this->getLayoutId();
                $clonedId   = $this->getParagraphModel()
                                   ->cloneFrom( $data['importId'], '_central' );
                $success    = $this->saveLayout( $clonedId );

                $this->getServiceLocator()
                     ->get( 'Grid\Menu\Model\Menu\Model' )
                     ->interleaveParagraphs( $clonedId, $beforeId );
            }
            else
            {
                $success    = false;
            }
        }

        if ( true === $success )
        {
            $this->messenger()
                 ->add( 'paragraph.action.importLayout.success',
                        'paragraph', Message::LEVEL_INFO );
        }

        if ( false === $success )
        {
            $this->messenger()
                 ->add( 'paragraph.action.importLayout.failed',
                        'paragraph', Message::LEVEL_ERROR );
        }

        if ( null !== $success )
        {
            return $this->redirect()
                        ->toUrl( $form->get( 'returnUri' )
                                      ->getValue() );
        }

        $view = new ViewModel( array(
            'form' => $form,
        ) );
        return $view->setTerminal( true );
    }
29886
public function getModel()
    {
        if (!isset($this->_pk))
            throw new InvalidCallException('Invalid usage. You must call this method only if load method succeeds');

        if (isset($this->_model))
            return $this->_model;

        $class = $this->modelClass;
        return $this->_model = $class::findOne($this->_pk);
    }
564533
public function pivotModel()
    {
        if (!isset($this->pivotModel)) {
            $this->pivotModel = $this->modelFactory()->get(Join::class);
        }

        return $this->pivotModel;
    }
1297712
public function getModel()
    {
        if (!$this->isLoggedIn()) {
            throw new NotLoggedInException();
        }

        if (isset($this->loggedInUserIdentifier)) {
            try {
                return SolutionSchema::getModel($this->modelClassName, $this->loggedInUserIdentifier);
            } catch (RecordNotFoundException $er) {
                throw new NotLoggedInException();
            }
        }

        throw new NotLoggedInException();
    }
536551
public function get()
    {
        $result = $this->getRaw();

        $result = new PlasticResult($result);

        if ($this->model) {
            $this->getModelFiller()->fill($this->model, $result);
        }

        return $result;
    }
895342
public function get()
    {
        $hasFrom = ! is_null($this->query->from);

        // When no from has been specified at this point the developer
        // tries to do a basic where query. In this case we want to use the index
        // path, since the index path is supposed to return collections.
        if (! $hasFrom) {
            $path = $this->model->getPath('index');
            $this->query->from($path);
        }

        $models = $this->getModels();

        return $this->model->newCollection($models);
    }
1190856
public function getSaldoModel()
    {
        $model = null;
        $saldoSec = $this->findSegment(static::SEG_ACCOUNT_INFORMATION);

        if (is_string($saldoSec)) {
            $saldoSec = $this->splitSegment($saldoSec);
            array_shift($saldoSec); // get rid of header
            $model = $this->createModelFromArray($saldoSec);
        }

        return $model;
    }
441183
public function getDataStorageModel()
    {
        if (! $this->_dataModel instanceof FieldDataModel) {
            $this->_dataModel = $this->tracker->createTrackClass('Model\\FieldDataModel');
        }

        return $this->_dataModel;
    }
676830
protected function createModel()
    {
        $model = $this->loader->getTracker()->getTrackModel();
        $model->applyFormatting(true, true);

        return $model;
    }
1355195
public function getModel()
    {
        if (is_null($this->_model)) {
            $modelClass = $this->modelClass;
            $this->_model = new $modelClass();
        }

        return $this->_model;
    }
94717
public function findModel($new = false)
    {
        $class = \Yii::$app->user->identityClass;
        return $new === true ? new $class() : Yii::$app->user->identity;
    }
293671
private function newPrimaryKey()
    {
        if ($this->newPrimaryKey === null) {
            $model = $this->targetModel();

            if ($this->isPrimaryKeyDifferent()) {
                $newKey = $model->key();
            } else {
                $newKey = sprintf('%s_new', $model->key());
            }

            $this->newPrimaryKey = $newKey;
        }

        return $this->newPrimaryKey;
    }
742771
public function getImporter()
    {
        return $this->loader->getImportLoader()->getImporter(
                $this->getRequest()->getControllerName(),
                $this->getModel()
                );
    }