示例#1
0
 /**
  * Creates a view with name $viewID based on $refType bean type
  * and then left-joining the specified types in $types in the given
  * order.
  *
  * @param  string $viewID  desired name of the view
  * @param  string $refType first bean type to be used as base
  * @param  array  $types   array with types to be left-joined in view
  *
  * @return boolean $success whether we created a new view (false if already exists)
  */
 public function createView($viewID, $refType, $types)
 {
     if ($this->oodb->isFrozen()) {
         return false;
     }
     $history = array();
     $tables = array_flip($this->writer->getTables());
     $refTable = $refType;
     //$this->writer->safeTable($refType, true);
     $currentTable = $refTable;
     $history[$refType] = $refType;
     foreach ($types as $t) {
         if (!isset($history[$t])) {
             $history[$t] = $t;
             $connection = array($t, $currentTable);
             sort($connection);
             $connection = implode("_", $connection);
             $connectionTable = $this->writer->safeTable($connection, true);
             if (isset($tables[$connectionTable])) {
                 //this connection exists
                 $srcPoint = $this->writer->safeTable($connection) . "." . $this->writer->safeColumn($currentTable . "_id");
                 //i.e. partic_project.project_id
                 $dstPoint = $this->writer->safeTable($currentTable) . "." . $this->writer->safeColumn($this->writer->getIDField($currentTable));
                 //i.e. project.id
                 $joins[$connection] = array($srcPoint, $dstPoint);
                 //now join the type
                 $srcPoint = $this->writer->safeTable($connection) . "." . $this->writer->safeColumn($t . "_id");
                 $dstPoint = $this->writer->safeTable($t) . "." . $this->writer->safeColumn($this->writer->getIDField($t));
                 $joins[$t] = array($srcPoint, $dstPoint);
             } else {
                 //this connection does not exist
                 $srcPoint = $this->writer->safeTable($t) . "." . $this->writer->safeColumn($currentTable . "_id");
                 $dstPoint = $this->writer->safeTable($currentTable) . "." . $this->writer->safeColumn($this->writer->getIDField($currentTable));
                 $joins[$t] = array($srcPoint, $dstPoint);
             }
         }
         //now set the new refTable
         $currentTable = $t;
     }
     try {
         $rs = (bool) $this->writer->createView($refType, $joins, $viewID);
     } catch (Exception $e) {
         throw new RedBean_Exception_SQL('Could not create view, types does not seem related (yet)..');
     }
     return $rs;
 }
示例#2
0
 /**
  * Inspects the database schema. If you pass the type of a bean this
  * method will return the fields of its table in the database.
  * The keys of this array will be the field names and the values will be
  * the column types used to store their values.
  * If no type is passed, this method returns a list of all tables in the database.
  *
  * @param string $type Type of bean (i.e. table) you want to inspect
  *
  * @return array
  */
 public static function inspect($type = NULL)
 {
     return $type === NULL ? self::$writer->getTables() : self::$writer->getColumns($type);
 }
 /**
  * Inspects the database schema. If you pass the type of a bean this
  * method will return the fields of its table in the database.
  * The keys of this array will be the field names and the values will be
  * the column types used to store their values.
  * If no type is passed, this method returns a list of all tables in the database.
  *
  * @param string $type Type of bean (i.e. table) you want to inspect
  *
  * @return array
  */
 public function inspect($type = NULL)
 {
     return $type === NULL ? $this->writer->getTables() : $this->writer->getColumns($type);
 }