This class manages a database table and is used by the Model class for reading and writing to its database table. There is one instance of Table for every table you have a model for.
Exemplo n.º 1
0
 public function __get($name)
 {
     if ($name === 'primary_key' && !isset($this->primary_key)) {
         $this->primary_key = [Table::load($this->class_name)->pk[0]];
     }
     return $this->{$name};
 }
Exemplo n.º 2
0
 public function setUp($connection_name = null)
 {
     require_once 'DatabaseLoader.php';
     Table::clearCache();
     $config = Config::instance();
     $this->original_default_connection = $config->getDefaultConnection();
     if ($connection_name) {
         $config->setDefaultConnection($connection_name);
     }
     /*
      if ($connection_name === 'sqlite' || $config->getDefaultConnection() === 'sqlite')
      {
      // need to create the db. the adapter specifically does not create it for us.
      //static::$db = \substr(Config::instance()->getConnection('sqlite'), 9);
      //new Sqlite(static::$db);
      //$file_db = new PDO('sqlite:messaging.sqlite3');
      if ($GLOBALS['OS'] !== 'WIN')
      {
      new \PDO('sqlite:../Fixtures/test.db');
      }
      else
      {
      new \PDO('sqlite:../Fixtures/test.db');
      }
      var_dump($config);
      var_dump($GLOBALS['OS']);
      exit();
      }
     */
     $this->connection_name = $connection_name;
     try {
         $this->conn = ConnectionManager::getConnection($connection_name);
     } catch (ExceptionDatabase $e) {
         $this->markTestSkipped($connection_name . ' failed to connect. ' . $e->getMessage());
     }
     $GLOBALS['Activerecord_LOG'] = false;
     $loader = new DatabaseLoader($this->conn);
     $loader->resetTableData();
     if (self::$log) {
         $GLOBALS['Activerecord_LOG'] = true;
     }
 }
Exemplo n.º 3
0
 /**
  * Returns the {@link Table} object for this model.
  *
  * Be sure to call in static scoping: static::table()
  *
  * @return Table
  */
 public static function table()
 {
     return Table::load(get_called_class());
 }
Exemplo n.º 4
0
 public function set_up($connection_name = null)
 {
     parent::set_up($connection_name);
     $this->sql = new SQLBuilder($this->conn, $this->table_name);
     $this->table = Table::load($this->class_name);
 }
Exemplo n.º 5
0
 /**
  * Creates INNER JOIN SQL for associations.
  *
  * @param Table $from_table the table used for the FROM SQL statement
  * @param bool $using_through is this a THROUGH relationship?
  * @param string $alias a table alias for when a table is being joined twice
  * @return string SQL INNER JOIN fragment
  */
 public function constructInnerJoinSql(Table $from_table, $using_through = false, $alias = null)
 {
     if ($using_through) {
         $join_table = $from_table;
         $join_table_name = $from_table->getFullyQualifiedTableName();
         $from_table_name = Table::load($this->class_name)->getFullyQualifiedTableName();
     } else {
         $join_table = Table::load($this->class_name);
         $join_table_name = $join_table->getFullyQualifiedTableName();
         $from_table_name = $from_table->getFullyQualifiedTableName();
     }
     // need to flip the logic when the key is on the other table
     if ($this instanceof HasMany || $this instanceof HasOne) {
         $this->setKeys($from_table->class->getName());
         if ($using_through) {
             $foreign_key = $this->primary_key[0];
             $join_primary_key = $this->foreign_key[0];
         } else {
             $join_primary_key = $this->foreign_key[0];
             $foreign_key = $this->primary_key[0];
         }
     } else {
         $foreign_key = $this->foreign_key[0];
         $join_primary_key = $this->primary_key[0];
     }
     if (!\is_null($alias)) {
         $aliased_join_table_name = $alias = $this->getTable()->conn->quoteName($alias);
         $alias .= ' ';
     } else {
         $aliased_join_table_name = $join_table_name;
     }
     return "INNER JOIN {$join_table_name} {$alias}ON({$from_table_name}.{$foreign_key} = {$aliased_join_table_name}.{$join_primary_key})";
 }
Exemplo n.º 6
0
 protected function setKeys($model_class_name, $override = false)
 {
     //infer from class_name
     if (!$this->foreign_key || $override) {
         $this->foreign_key = [Inflector::instance()->keyify($model_class_name)];
     }
     if (!$this->primary_key || $override) {
         $this->primary_key = Table::load($model_class_name)->pk;
     }
 }
Exemplo n.º 7
0
if (substr($targetNamespace, -1) != "\\") {
    $targetNamespace .= "\\";
}
$targetDirectory = $_SERVER['argv'][2];
$tablesDirectory = $targetDirectory . DIRECTORY_SEPARATOR . 'table';
if (file_exists($tablesDirectory) == false) {
    mkdir($tablesDirectory);
}
$config = new \Doctrine\DBAL\Configuration();
//..
$connectionParams = array('url' => 'mysql://*****:*****@localhost/teach');
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
$schemaManager = $conn->getSchemaManager();
foreach ($schemaManager->listTables() as $table) {
    $tableName = $table->getName();
    $tableDescriptor = new Table($table);
    $classDescription = $tableDescriptor->describe($targetNamespace . "Table");
    $class = new gossi\codegen\model\PhpClass($classDescription['identifier']);
    $class->setFinal(true);
    $escapedClassName = str_replace("\\", "\\\\", $class->getQualifiedName());
    $class->setProperty(PhpProperty::create("connection")->setType('\\PDO'));
    $constructor = PhpMethod::create("__construct");
    $constructor->addSimpleParameter("connection", '\\PDO');
    $constructor->setBody('$this->connection = $connection;');
    $class->setMethod($constructor);
    foreach ($classDescription['properties'] as $propertyIdentifier => $value) {
        $class->setProperty(PhpProperty::create($propertyIdentifier));
    }
    $querybuilder = $conn->createQueryBuilder();
    $foreignKeys = $table->getForeignKeys();
    foreach ($classDescription['methods'] as $methodIdentifier => $method) {