Commit 78348d78 by Qiang Xue

removed unused files.

parent 1b151818
<?php
namespace yii\db\ar;
use yii\db\Exception;
use yii\db\dao\TableSchema;
/**
* ActiveMetaData represents the meta-data for an Active Record class.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveMetaData
{
/**
* @var ActiveMetaData[] list of ActiveMetaData instances indexed by the model class names
*/
public static $instances;
/**
* @var TableSchema the table schema information
*/
public $table;
/**
* @var string the model class name
*/
public $modelClass;
/**
* @var ActiveRecord the model instance that can be used to access non-static methods
*/
public $model;
/**
* Returns an instance of ActiveMetaData for the specified model class.
* Note that each model class only has a single ActiveMetaData instance.
* This method will only create the ActiveMetaData instance if it is not previously
* done so for the specified model class.
* @param string $modelClass the model class name. Make sure the class name do NOT have a leading backslash "\".
* @param boolean $refresh whether to recreate the ActiveMetaData instance. Defaults to false.
* @return ActiveMetaData the ActiveMetaData instance for the specified model class.
*/
public static function getInstance($modelClass, $refresh = false)
{
if (isset(self::$instances[$modelClass]) && !$refresh) {
return self::$instances[$modelClass];
} else {
return self::$instances[$modelClass] = new self($modelClass);
}
}
/**
* Constructor.
* @param string $modelClass the model class name
*/
public function __construct($modelClass)
{
$this->modelClass = $modelClass;
$this->model = new $modelClass;
$tableName = $this->model->tableName();
$this->table = $this->model->getDbConnection()->getDriver()->getTableSchema($tableName);
if ($this->table === null) {
throw new Exception("Unable to find table '$tableName' for ActiveRecord class '$modelClass'.");
}
$primaryKey = $this->model->primaryKey();
if ($primaryKey !== $this->table->primaryKey) {
$this->table->fixPrimaryKey($primaryKey);
} elseif ($primaryKey === null) {
throw new Exception("The table '$tableName' for ActiveRecord class '$modelClass' does not have a primary key.");
}
}
/**
* Adds a relation.
*
* $config is an array with three elements:
* relation type, the related active record class and the foreign key.
*
* @throws Exception
* @param string $name $name Name of the relation.
* @param array $config $config Relation parameters.
* @return void
*/
public function addRelation($name, $config)
{
if (preg_match('/^(\w+)\s*:\s*\\\\?([\w\\\\]+)(\[\])?$/', $name, $matches)) {
if (is_string($config)) {
$config = array('on' => $config);
}
$relation = new ActiveRelation($config);
$relation->name = $matches[1];
$modelClass = $matches[2];
if (strpos($modelClass, '\\') !== false) {
$relation->modelClass = ltrim($modelClass, '\\');
} else {
$relation->modelClass = dirname($this->modelClass) . '\\' . $modelClass;
}
$relation->hasMany = isset($matches[3]);
$this->relations[$relation->name] = $relation;
} else {
throw new Exception("{$this->modelClass} has an invalid relation: $name");
}
}
}
\ No newline at end of file
<?php
/**
* ActiveFinder class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\ar;
use yii\db\dao\BaseQuery;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class BaseActiveQuery extends BaseQuery
{
/**
* @var string the name of the ActiveRecord class.
*/
public $modelClass;
/**
* @var array list of relations that this query should be performed with
*/
public $with;
/**
* @var string the table alias to be used for query
*/
public $tableAlias;
/**
* @var string the name of the column that the result should be indexed by.
* This is only useful when the query result is returned as an array.
*/
public $index;
/**
* @var boolean whether to return each record as an array. If false (default), an object
* of [[modelClass]] will be created to represent each record.
*/
public $asArray;
/**
* @var array list of scopes that should be applied to this query
*/
public $scopes;
public function asArray($value = true)
{
$this->asArray = $value;
return $this;
}
public function with()
{
$this->with = func_get_args();
if (isset($this->with[0]) && is_array($this->with[0])) {
// the parameter is given as an array
$this->with = $this->with[0];
}
return $this;
}
public function index($column)
{
$this->index = $column;
return $this;
}
public function tableAlias($value)
{
$this->tableAlias = $value;
return $this;
}
public function scopes($names)
{
$this->scopes = $names;
return $this;
}
protected function createModels($rows)
{
$models = array();
if ($this->asArray) {
if ($this->index === null) {
return $rows;
}
foreach ($rows as $row) {
$models[$row[$this->index]] = $row;
}
} else {
/** @var $class ActiveRecord */
$class = $this->modelClass;
if ($this->index === null) {
foreach ($rows as $row) {
$models[] = $class::create($row);
}
} else {
foreach ($rows as $row) {
$models[$row[$this->index]] = $class::create($row);
}
}
}
return $models;
}
}
<?php
namespace yii\db\ar;
class HasManyRelation extends Relation
{
public $link;
}
<?php
namespace yii\db\ar;
class HasOneRelation extends Relation
{
public $link;
}
<?php
/**
* JoinElement class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\ar;
use yii\base\VectorIterator;
use yii\db\dao\Query;
use yii\db\Exception;
class JoinElement
{
/**
* @var integer ID of this join element
*/
public $id;
/**
* @var BaseActiveQuery
*/
public $query;
/**
* @var JoinElement the parent element that this element needs to join with
*/
public $parent;
public $container;
/**
* @var JoinElement[] the child elements that need to join with this element
*/
public $children = array();
/**
* @var JoinElement[] the child elements that have corresponding relations declared in the AR class of this element
*/
public $relations = array();
/**
* @var array column aliases (alias => original name)
*/
public $columnAliases = array();
/**
* @var array primary key column aliases (original name => alias)
*/
public $pkAlias = array();
/**
* @var string|array the column(s) used for index the query results
*/
public $key;
/**
* @var array query results for this element (PK value => AR instance or data array)
*/
public $records = array();
public $related = array();
/**
* @param integer $id
* @param ActiveRelation|ActiveQuery $query
* @param null|JoinElement $parent
* @param null|JoinElement $container
*/
public function __construct($id, $query, $parent, $container)
{
$this->id = $id;
$this->query = $query;
if ($parent !== null) {
$this->parent = $parent;
$this->container = $container;
$parent->children[$query->name] = $this;
if ($query->select !== false) {
$container->relations[$query->name] = $this;
}
}
}
public function populateData($rows)
{
if ($this->container === null) {
foreach ($rows as $row) {
if (($key = $this->getKeyValue($row)) !== null && !isset($this->records[$key])) {
$this->records[$key] = $this->createRecord($row);
}
}
} else {
foreach ($rows as $row) {
$key = $this->getKeyValue($row);
$containerKey = $this->container->getKeyValue($row);
if ($key === null || $containerKey === null || isset($this->related[$containerKey][$key])) {
continue;
}
$this->related[$containerKey][$key] = true;
if ($this->query->asArray) {
if (isset($this->records[$key])) {
if ($this->query->hasMany) {
if ($this->query->index !== null) {
$this->container->records[$containerKey][$this->query->name][$key] =& $this->records[$key];
} else {
$this->container->records[$containerKey][$this->query->name][] =& $this->records[$key];
}
} else {
$this->container->records[$containerKey][$this->query->name] =& $this->records[$key];
}
} else {
$record = $this->createRecord($row);
if ($this->query->hasMany) {
if ($this->query->index !== null) {
$this->container->records[$containerKey][$this->query->name][$key] = $record;
$this->records[$key] =& $this->container->records[$containerKey][$this->query->name][$key];
} else {
$count = count($this->container->records[$containerKey][$this->query->name]);
$this->container->records[$containerKey][$this->query->name][] = $record;
$this->records[$key] =& $this->container->records[$containerKey][$this->query->name][$count];
}
} else {
$this->container->records[$containerKey][$this->query->name] = $record;
$this->records[$key] =& $this->container->records[$containerKey][$this->query->name];
}
}
} else {
if (isset($this->records[$key])) {
$record = $this->records[$key];
} else {
$this->records[$key] = $record = $this->createRecord($row);
}
$this->container->records[$containerKey]->addRelatedRecord($this->query, $record);
}
}
}
foreach ($this->relations as $child) {
$child->populateData($rows);
}
}
protected function getKeyValue($row)
{
if (is_array($this->key)) {
$key = array();
foreach ($this->key as $alias) {
if (!isset($row[$alias])) {
return null;
}
$key[] = $row[$alias];
}
return serialize($key);
} else {
return $row[$this->key];
}
}
protected function createRecord($row)
{
$record = array();
foreach ($this->columnAliases as $alias => $name) {
$record[$name] = $row[$alias];
}
if ($this->query->asArray) {
foreach ($this->relations as $child) {
$record[$child->query->name] = $child->query->hasMany ? array() : null;
}
} else {
$modelClass = $this->query->modelClass;
$record = $modelClass::create($record);
foreach ($this->relations as $child) {
$record->{$child->query->name} = $child->query->hasMany ? array() : null;
}
}
return $record;
}
}
\ No newline at end of file
<?php
namespace yii\db\ar;
class ManyManyRelation extends Relation
{
public $joinTable;
public $leftLink;
public $rightLink;
}
<?php
namespace yii\db\ar;
class Relation extends ActiveQuery
{
protected $multiple = false;
public $parentClass;
/**
* @var array
*/
public $link;
/**
* @var ActiveQuery
*/
public $via;
public function findWith($name, &$parentRecords)
{
if (empty($this->link) || !is_array($this->link)) {
throw new \yii\base\Exception('invalid link');
}
$this->addLinkCondition($parentRecords);
$records = $this->find();
/** @var array $map mapping key(s) to index of $parentRecords */
$index = $this->buildRecordIndex($parentRecords, array_values($this->link));
$this->initRecordRelation($parentRecords, $name);
foreach ($records as $record) {
$key = $this->getRecordKey($record, array_keys($this->link));
if (isset($index[$key])) {
$parentRecords[$map[$key]][$name] = $record;
}
}
}
protected function getRecordKey($record, $attributes)
{
if (isset($attributes[1])) {
$key = array();
foreach ($attributes as $attribute) {
$key[] = is_array($record) ? $record[$attribute] : $record->$attribute;
}
return serialize($key);
} else {
$attribute = $attributes[0];
return is_array($record) ? $record[$attribute] : $record->$attribute;
}
}
protected function buildRecordIndex($records, $attributes)
{
$map = array();
foreach ($records as $i => $record) {
$map[$this->getRecordKey($record, $attributes)] = $i;
}
return $map;
}
protected function addLinkCondition($parentRecords)
{
$attributes = array_keys($this->link);
$values = array();
if (isset($links[1])) {
// composite keys
foreach ($parentRecords as $record) {
$v = array();
foreach ($this->link as $attribute => $link) {
$v[$attribute] = is_array($record) ? $record[$link] : $record->$link;
}
$values[] = $v;
}
} else {
// single key
$attribute = $this->link[$links[0]];
foreach ($parentRecords as $record) {
$values[] = is_array($record) ? $record[$attribute] : $record->$attribute;
}
}
$this->andWhere(array('in', $attributes, $values));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment