Skip to content

Instantly share code, notes, and snippets.

@KorsaR-ZN
Last active August 29, 2015 14:18
Show Gist options
  • Save KorsaR-ZN/f1d853fda1e70037ec01 to your computer and use it in GitHub Desktop.
Save KorsaR-ZN/f1d853fda1e70037ec01 to your computer and use it in GitHub Desktop.
Specifies which related objects should be eagerly loaded for Phalcon models
<?php
/**
* WithTrait.php
* ----------------------------------------------
*
*
* @author Stanislav Kiryukhin <[email protected]>
* @copyright Copyright (c) 2015, CKGroup.ru
*
* ----------------------------------------------
* All Rights Reserved.
* ----------------------------------------------
*/
namespace Phalcon\Ext\Mvc\Model;
trait WithTrait
{
/**
* Specifies which related objects should be eagerly loaded.
* This method takes as its first parameter of array in each item specifies the alias of a relation.
* The second parameter is similar to the method find() or findFirst()
*
* @param array $with
* @param null $parameters
* @return \Phalcon\Mvc\Model\ResultsetInterface
*
* @see \Phalcon\Mvc\Model::find()
* @see \Phalcon\Mvc\Model::findFirst()
*/
public static function with(Array $with, $parameters = null)
{
if (count($with) < 1) {
throw new \InvalidArgumentException();
}
$model = new static();
$modelClass = get_class($model);
/** @var $manager \Phalcon\Mvc\Model\ManagerInterface */
$manager = $model->getModelsManager();
if ($parameters && isset($parameters['alias'])) {
$modelAlias = $parameters['alias'];
} else {
$modelAlias = lcfirst(substr($modelClass, ((int)strrpos($modelClass, '\\')) + 1));
}
// fix model alias
if ($parameters) {
$parameters = array_map(function($value) use($modelAlias) {
return str_replace($modelAlias . '.', '[' . $modelAlias . '].', $value);
}, (array)$parameters);
}
$builder = $manager->createBuilder($parameters);
$builder->from([$modelAlias => $modelClass]);
/**
* Build relations
*/
foreach ($with as $alias) {
/** @var $relation \Phalcon\Mvc\Model\RelationInterface */
$relation = $manager->getRelationByAlias($modelClass, $alias);
$fields = (array)$relation->getFields();
$referencedFields = (array)$relation->getReferencedFields();
$conditions = [];
foreach ($referencedFields as $k => $field) {
$f1 = $alias . '.' . $field;
$f2 = '[' . $modelAlias . '].' . $fields[$k];
$conditions[] = $f1 . '=' . $f2;
}
$builder->leftJoin($relation->getReferencedModel(), implode(' AND ', $conditions), $alias);
}
$query = $builder->getQuery();
/**
* Pass the cache options to the query
*/
if (isset($parameters['cache'])) {
$query->cache($parameters['cache']);
}
$bind = null;
$bindTypes = null;
/**
* Check for bind parameters
*/
if (isset($parameters['bind'])) {
$bind = $parameters['bind'];
if (isset($parameters['bindTypes'])) {
$bindTypes = $parameters['bindTypes'];
}
}
/**
* Execute the query passing the bind-params and casting-types
*/
$resultset = $query->execute($bind, $bindTypes);
/**
* Define an hydration mode
*/
if (is_object($resultset) && isset($parameters['hydration'])) {
$resultset->setHydrateMode($parameters['hydration']);
}
return $resultset;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment