@param AbstractModel $model
@param $param

@return AbstractModel
338113
public function findOneBy(AbstractModel $model, $param = [])
    {

        $result = $this->findBy($model, $param);
        $result->rewind();

        return $result->current();
    }
1043213
public function prepareCacheData(AbstractModel $model)
    {
        /**
         * @todo change MySQLTableData to a usecase based one
         */
        $tableData  = clone $this->tableData;
        $reflection = new \ReflectionClass($model);
        $tableData->setTableName($this->parseTableName($reflection));

        return [
            'tableData'    => $tableData,
            'reflection'   => new \ReflectionClass($model),
            'relationList' => $this->extractRelations($reflection),
        ];
    }
992605
protected function resolveArgumentValue(\ReflectionParameter $param)
    {
        $paramClass = null;

        try {
            $paramClass = $param->getClass();
        }
        catch (\ReflectionException $e) {
        }

        if (!($paramClass instanceof ReflectionClass) || $param->isDefaultValueAvailable()) {
            return $this->resolveArgumentDefaultValue($param);
        }

        if (!($this->manager instanceof Manager)) {
            throw new RuntimeException("Unable to retrieve descriptor for class '{$paramClass->getName(
                                       )}' " . "because the manager has not been set. Please use Descriptor::setManager to resolve this.");
        }

        return $this->manager->describe($paramClass->getName(), null, null, $paramClass);
    }
865288
private function getReflectionParamData(\ReflectionParameter $param)
    {
        $param_type = self::getPsalmTypeFromReflectionType($param->getType());
        $param_name = (string)$param->getName();

        $is_optional = (bool)$param->isOptional();

        return new FunctionLikeParameter(
            $param_name,
            (bool)$param->isPassedByReference(),
            $param_type,
            null,
            null,
            $is_optional,
            $param_type->isNullable(),
            $param->isVariadic()
        );
    }
440366
protected function insertModelAsLastRoot(Model $model)
    {
        /** @var \T4\Orm\Model $class */
        $class = get_class($model);
        $tableName = $class::getTableName();
        /** @var \T4\Dbal\Connection $connection */
        $connection = $class::getDbConnection();

        $query = (new Query())
            ->select('MAX(__rgt)')
            ->from($tableName);
        $maxRgt = (int)$connection->query($query)->fetchScalar();

        if ($model->isNew()) {

            $model->__lft = $maxRgt + 1;
            $model->__rgt = $model->__lft + 1;
            $model->__lvl = 0;
            $model->__prt = null;

        } else {

            $query = (new Query())
                ->update()
                ->table($tableName)
                ->values([
                    '__lft' => '__lft + :max - :lft + 1',
                    '__rgt' => '__rgt + :max - :lft + 1',
                    '__lvl' => '__lvl - :lvl',
                ])
                ->where('__lft>=:lft AND __rgt<=:rgt')
                ->params([':max' => $maxRgt, ':lft' => $model->__lft, ':rgt' => $model->__rgt, ':lvl' => $model->__lvl]);
            $connection->execute($query);

            $this->removeFromTreeByElement($model);

            // TODO: calculate new __lft, __rgt!
            $model->refreshTreeColumns();
            $model->__lvl = 0;
            $model->__prt = null;
        }
    }
1050242
public function persist(AbstractModel $model)
    {
        $collection = $this->getCollectionBy(
            $this->getModelConfiguration(get_class($model))
        );

        $this->setLastResult(
            $collection->insert($this->getModelDataAsArray($model))
        );

        return (bool) ($this->lastResult) ? $this->lastResult[self::RESULT_OK_INDEX] : false;
    }
766721
public function add(AbstractModel $model)
    {
        if (! $this->has($model)) {
            $this->addShallow($model);

            $modelLinks = $model->getRepo()->getLinkMap()->get($model);

            foreach ($modelLinks->getModels() as $linkedModel) {
                $this->add($linkedModel);
            }
        }

        return $this;
    }
1148177
public static function buildResult($model, $params = [])
    {
        $pk = $model->primaryKey;
        $id = $model->{$pk};
        $fields = array_get($params, ApiOptions::FIELDS, $pk);
        $related = array_get($params, ApiOptions::RELATED);

        if ($pk === $fields && empty($related)) {
            return [$pk => $id];
        }

        $fieldsArray = explode(',', $fields);

        $result = static::selectById($id, $params, $fieldsArray);

        return $result;
    }
1107807
protected function add(AbstractModel $model)
    {
        if (true === $this->has($model)) {
            return $this;
        }
        $this->validateAdd($model);
        if (true === $model->getState()->is('empty')) {
            $this->loaded = false;
        }

        $key = $model->getCompositeKey();
        $this->models[$key] = $model;
        $this->modelKeyMap[] = $key;

        if (false === $this->hasOriginal($model)) {
            $this->original[$key] = $model;
        }
        return $this;
    }
1081190
public static function findOrNew($param, $field = '*', $parameters = array()) {
	    if (empty($param)) {
	        return new static();
        }
	    $model = static::find($param, $field, $parameters);
	    if (empty($model)) {
	        return new static();
        }
        return $model;
    }
827133
public function get(AbstractModel $model)
    {
        if (! $this->repo->isModel($model)) {
            throw new InvalidArgumentException(
                sprintf('Model must be %s, was %s', $this->repo->getModelClass(), get_class($model))
            );
        }

        if ($this->map->contains($model)) {
            return $this->map[$model];
        } else {
            return $this->map[$model] = new Links($model);
        }
    }
796319
public function getModelDataAsArray(AbstractModel $model)
    {
        $reflectionClass = new \ReflectionClass($model);
        $set = [];

        foreach ($reflectionClass->getProperties() as $property) {
            /**
             * @todo move the check out into a validator
             */
            if (strpos($property->getDocComment(), 'ODM\\Property') !== false) {
                $property->setAccessible(true);
                $set[$property->getName()] = $property->getValue($model);
            }
        }

        return $set;
    }
1193229
public function setModel(Model $model)
    {
        $this->model = $model;
        $append = $this->getAppends();

        if ($append instanceof WithModelInterface) {
            $append->setModel($model);
        }

        return $this;
    }
552792
public function remove($param)
    {
        if ($param instanceof Model) {
            $param = $param->getId();
        }

        $item = $this->get($param);
        if ($item) {
            $id = (string) $item->getId();
            if ($this->_items[$id]) {
                unset($this->_items[$id]);
            }
        }

        return $this;

    }
919865
public function setModel(Model $model)
    {
        if (strpos($this->originalName, '.') !== false) {
            $parts = explode('.', $this->originalName);
            $name = array_pop($parts);

            while ($parts) {
                $part = array_shift($parts);
                $relation = $model->getAttribute($part);

                if ($relation instanceof Model) {
                    $model = $relation;
                    $this->setName($name);
                }
            }
        }

        return parent::setModel($model);
    }
801982
public function generateUpdateForModel(AbstractModel $model, $diff = null)
    {
        $cacheData = $this->fetchCacheData($model);
        $strategy  = new MySQLUpdateStrategy(
            $cacheData[self::TABLE_DATA_INDEX],
            $this->prepareModelData(
                $model,
                $cacheData[self::TABLE_DATA_INDEX]
            )
        );

        $strategy->setTransformer($this->paramTransformer);

        return [
            $strategy->generateSQLStatement(),
            $this->prepareModelDataForStatement($strategy->getModelData()),
            self::UPDATE_QUERY
        ];
    }
238025
public function setCurrentRecord(Mage_Core_Model_Abstract $model = null)
    {
        if ($model) {
            $expectedClass = get_class($this->getModel());
            if (!is_a($model, $expectedClass)) {
                throw new RuntimeException($this->__('Invalid model class. Expected:%1$s Passed:%2$s', $expectedClass, get_class($model)));
            }
        }

        Mage::unregister($this->getCurrentRecordKey());

        if ($model) {
            Mage::register($this->getCurrentRecordKey(), $model);
        }

        return $this;
    }