Ejemplo n.º 1
0
 /**
  * 获得 BelongTo Bean
  *
  * @param $name
  * @throws \Exception
  * @return Bean|null
  */
 public function getBelongTo($name)
 {
     $foreignKey = Relation::getForeignKey($name);
     if (!isset($this->data[$foreignKey])) {
         return null;
     }
     $foreignTable = $this->table->getBelongToTable($name);
     $m = $this->getManagerFactory()->getManager($foreignTable);
     return $m->get($this->data[$foreignKey]);
 }
Ejemplo n.º 2
0
 public static function create($name, Table $table)
 {
     $config = $table->getConfig()['fields'][$name];
     switch ($config['type']) {
         case FieldInterface::TYPE_ARRAY:
             $class = '\\ArrayField';
             break;
         case FieldInterface::TYPE_BOOLEAN:
             $class = '\\BooleanField';
             break;
         case FieldInterface::TYPE_DATE:
             $class = '\\DateField';
             break;
         case FieldInterface::TYPE_DATETIME:
             $class = '\\DatetimeField';
             break;
         case FieldInterface::TYPE_FLOAT:
             $class = '\\FloatField';
             break;
         case FieldInterface::TYPE_INTEGER:
             $class = '\\IntegerField';
             break;
         case FieldInterface::TYPE_STRING:
             $class = '\\StringField';
             break;
         case FieldInterface::TYPE_TEXT:
             $class = '\\TextField';
             break;
         case FieldInterface::TYPE_TIME:
             $class = '\\TimeField';
             break;
         default:
             return null;
     }
     $class = __NAMESPACE__ . $class;
     return new $class($name, $table, $config['required'], $config['index'], $config['unique']);
 }
Ejemplo n.º 3
0
 /**
  * @param               $id
  * @param               $relationTableName
  * @param  array        $where
  * @param  array        $options
  * @return array|Bean[]
  */
 public function getMany($id, $relationTableName, array $where = [], array $options = [])
 {
     if ($this->table->hasOneToManyTable($relationTableName)) {
         $where[Relation::getForeignKey($this->getTableName())] = $id;
         if ($options) {
             $where = array_merge(['AND' => $where], $options);
         }
         return $this->getManagerFactory()->getManager($relationTableName)->select([], $where);
     } elseif ($this->table->hasManyToManyTable($relationTableName)) {
         $mm = Relation::getManyToManyTableName($this->getTableName(), $relationTableName);
         $manager = $this->getManagerFactory()->getManager($relationTableName);
         $foreignKey = $mm . '.' . Relation::getForeignKey($relationTableName);
         $thisForeignKey = $mm . '.' . Relation::getForeignKey($this->getTableName());
         $where[$thisForeignKey] = $id;
         return $manager->select(['[<]' . $mm => [$relationTableName . '.id' => $foreignKey]], ['AND' => $where]);
     } else {
         return [];
     }
 }
Ejemplo n.º 4
0
 protected function addTable(Table $table)
 {
     $this->tables[$table->getName()] = $table;
     return $this;
 }
Ejemplo n.º 5
0
 /**
  * @param  Table|string $table
  * @return Manager
  * @throws \Exception
  */
 public function getManager($table)
 {
     $structure = $this->getStructure();
     if ($table instanceof Table) {
         $tableName = $table->getName();
     } else {
         $tableName = $table;
     }
     if (!$structure->hasTable($tableName)) {
         throw new \Exception("数据表 %{$tableName} 不存在");
     }
     if (!isset($this->managers[$tableName])) {
         $this->managers[$tableName] = new Manager($this, $this->getStructure()->getTable($tableName));
     }
     return $this->managers[$tableName];
 }
Ejemplo n.º 6
0
 public function extendTable(Table $table)
 {
     $table->addField(new DatetimeField(static::FIELD_CREATED_AT, $table));
     $table->addField(new DatetimeField(static::FIELD_UPDATED_AT, $table));
 }
Ejemplo n.º 7
0
 public static function getManyToManyTableName(Table $table1, Table $table2)
 {
     $relation = [$table1->getName(), $table2->getName()];
     sort($relation);
     return $relation[0] . '_' . $relation[1];
 }
Ejemplo n.º 8
0
 public function getConfig()
 {
     return $this->table->getConfig()['fields'][$this->name];
 }