コード例 #1
0
 public function resetAction()
 {
     $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($this->config->database->toArray());
     $tables = $connection->listTables();
     foreach ($tables as $table) {
         $tablename = \Phalcon\Text::camelize($table);
         $fd = fopen("{$this->config->application->formsDir}/{$tablename}Form.php", "w");
         fwrite($fd, "<?php" . self::NL . self::NL);
         // Begin class
         fwrite($fd, "class {$tablename}Form {" . self::NL);
         $columns = $connection->describeColumns($table);
         foreach ($columns as $column) {
             if ($column instanceof \Phalcon\Db\Column) {
                 // Escape if column is primary
                 if ($column->isPrimary()) {
                     continue;
                 }
                 // Begin method
                 $columnname = \Phalcon\Text::camelize($column->getName());
                 fwrite($fd, self::TAB . "private function _{$columnname}() {" . self::NL);
                 // Write element
                 $columntype_base = $this->_getBaseType($column->getType());
                 $columntype = $this->_getType($columntype_base, $column);
                 fwrite($fd, self::TAB . self::TAB . "\$element = new \\Phalcon\\Forms\\Element\\{$columntype}(\"{$columnname}\");" . self::NL);
                 fwrite($fd, self::TAB . self::TAB . "\$element->setLabel(\"{$columnname}\");" . self::NL);
                 // Add empty selection for select fields
                 if ($columntype == "Select") {
                     fwrite($fd, self::TAB . self::TAB . "\$element->setOptions([]);" . self::NL);
                 }
                 // Add validator on text fields
                 if ($columntype == "Text" && $column->getSize() > 0) {
                     fwrite($fd, self::TAB . self::TAB . "\$element->addValidator(new \\Phalcon\\Validation\\Validator\\StringLength([" . self::NL);
                     fwrite($fd, self::TAB . self::TAB . self::TAB . "\"max\" => {$column->getSize()}" . self::NL);
                     fwrite($fd, self::TAB . self::TAB . "]));" . self::NL);
                 }
                 // End method
                 fwrite($fd, self::TAB . self::TAB . "return \$element;" . self::NL);
                 fwrite($fd, self::TAB . "}" . self::NL);
             }
         }
         // Final method : construction of the form
         fwrite($fd, self::TAB . "public function setFields() {" . self::NL);
         foreach ($columns as $column) {
             if ($column instanceof \Phalcon\Db\Column) {
                 if ($column->isPrimary()) {
                     continue;
                 }
                 $columnname = \Phalcon\Text::camelize($column->getName());
                 fwrite($fd, self::TAB . self::TAB . "\$this->add(\$this->_{$columnname}());" . self::NL);
             }
         }
         fwrite($fd, self::TAB . "}" . self::NL);
         // End class
         fwrite($fd, "}" . self::NL . self::NL);
         fclose($fd);
     }
     $this->view->disable();
     echo "done!";
     return FALSE;
 }
コード例 #2
0
ファイル: DbDescribeTest.php プロジェクト: lisong/cphalcon
 public function testDbMysql()
 {
     require 'unit-tests/config.db.php';
     if (empty($configMysql)) {
         $this->markTestSkipped("Skipped");
         return;
     }
     $connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
     //List tables
     $expectedTables = array('albums', 'artists', 'customers', 'issue_11036', 'issue_1534', 'issue_2019', 'm2m_parts', 'm2m_robots', 'm2m_robots_parts', 'parts', 'personas', 'personnes', 'prueba', 'robots', 'robots_parts', 'songs', 'subscriptores', 'tipo_documento');
     $tables = $connection->listTables();
     $this->assertEquals($tables, $expectedTables);
     $tables = $connection->listTables('phalcon_test');
     $this->assertEquals($tables, $expectedTables);
     //Table exist
     $this->assertEquals($connection->tableExists('personas'), 1);
     $this->assertEquals($connection->tableExists('noexist'), 0);
     $this->assertEquals($connection->tableExists('personas', 'phalcon_test'), 1);
     $this->assertEquals($connection->tableExists('personas', 'test'), 0);
     $expectedDescribe = $this->getExpectedColumnsMysql();
     $describe = $connection->describeColumns('personas');
     $this->assertEquals($describe, $expectedDescribe);
     $describe = $connection->describeColumns('personas', 'phalcon_test');
     $this->assertEquals($describe, $expectedDescribe);
     //Table Options
     $expectedOptions = array('table_type' => 'BASE TABLE', 'auto_increment' => NULL, 'engine' => 'InnoDB', 'table_collation' => 'utf8_unicode_ci');
     $options = $connection->tableOptions('personas', 'phalcon_test');
     $this->assertEquals($options, $expectedOptions);
     //Indexes
     $expectedIndexes = array('PRIMARY' => Phalcon\Db\Index::__set_state(array('_name' => 'PRIMARY', '_columns' => array('id'), '_type' => 'PRIMARY')), 'robots_id' => Phalcon\Db\Index::__set_state(array('_name' => 'robots_id', '_columns' => array('robots_id'))), 'parts_id' => Phalcon\Db\Index::__set_state(array('_name' => 'parts_id', '_columns' => array('parts_id'))));
     $describeIndexes = $connection->describeIndexes('robots_parts');
     $this->assertEquals($describeIndexes, $expectedIndexes);
     $describeIndexes = $connection->describeIndexes('robots_parts', 'phalcon_test');
     $this->assertEquals($describeIndexes, $expectedIndexes);
     //Indexes
     $expectedIndexes = array('PRIMARY' => Phalcon\Db\Index::__set_state(array('_name' => 'PRIMARY', '_columns' => array('id'), '_type' => 'PRIMARY')), 'issue_11036_token_UNIQUE' => Phalcon\Db\Index::__set_state(array('_name' => 'issue_11036_token_UNIQUE', '_columns' => array('token'), '_type' => 'UNIQUE')));
     $describeIndexes = $connection->describeIndexes('issue_11036');
     $this->assertEquals($describeIndexes, $expectedIndexes);
     $describeIndexes = $connection->describeIndexes('issue_11036', 'phalcon_test');
     $this->assertEquals($describeIndexes, $expectedIndexes);
     //References
     $expectedReferences = array('robots_parts_ibfk_1' => Phalcon\Db\Reference::__set_state(array('_referenceName' => 'robots_parts_ibfk_1', '_referencedTable' => 'robots', '_columns' => array('robots_id'), '_referencedColumns' => array('id'), '_referencedSchema' => 'phalcon_test')), 'robots_parts_ibfk_2' => Phalcon\Db\Reference::__set_state(array('_referenceName' => 'robots_parts_ibfk_2', '_referencedTable' => 'parts', '_columns' => array('parts_id'), '_referencedColumns' => array('id'), '_referencedSchema' => 'phalcon_test')));
     $describeReferences = $connection->describeReferences('robots_parts');
     $this->assertEquals($describeReferences, $expectedReferences);
     $describeReferences = $connection->describeReferences('robots_parts', 'phalcon_test');
     $this->assertEquals($describeReferences, $expectedReferences);
 }