Beispiel #1
0
 protected function handleData(array $config)
 {
     foreach ($config['tables'] as $name => $tbl) {
         $this->addTable(Table::createFromStructure($name, $this));
     }
     foreach ($this->tables as $table) {
         if ($extensions = $table->getConfig()['extensions']) {
             foreach ($extensions as $name) {
                 if (!isset($this->extensions[$name])) {
                     throw new \Exception("{$name} 扩展未注册");
                 }
                 $this->extensions[$name]->extendTable($table);
             }
         }
     }
     foreach ($this->tables as $table) {
         if ($belongToNames = $table->getConfig()['belong_to']) {
             foreach ($belongToNames as $name) {
                 $table->addBelongTo($this->getTables()[$name]);
             }
         }
     }
     foreach ($config['many_many'] as $relation) {
         $table = Relation::createManyToManyTable($this->tables[$relation[0]], $this->tables[$relation[1]]);
         $this->addTable($table);
     }
 }
Beispiel #2
0
 public function getName()
 {
     if (!$this->name) {
         $this->name = Relation::getForeignKey($this->relationTable->getName());
     }
     return $this->name;
 }
Beispiel #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 [];
     }
 }
Beispiel #4
0
 public function getBelongToTable($name)
 {
     $foreignKey = Relation::getForeignKey($name);
     $relationField = $this->getField($foreignKey);
     if (!$relationField instanceof RelationField) {
         throw new \Exception("{$name} 不是关联表");
     }
     return $relationField->getRelationTable();
 }
Beispiel #5
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]);
 }