後,爾康文檔的簡要回顧我假設你正在尋找這樣的事情=>Phalcon PHP creating tables
樣品用法:
<?php
use \Phalcon\Db\Column as Column;
$connection->createTable(
"robots",
null,
array(
"columns" => array(
new Column("id",
array(
"type" => Column::TYPE_INTEGER,
"size" => 10,
"notNull" => true,
"autoIncrement" => true,
)
),
new Column("name",
array(
"type" => Column::TYPE_VARCHAR,
"size" => 70,
"notNull" => true,
)
),
new Column("year",
array(
"type" => Column::TYPE_INTEGER,
"size" => 11,
"notNull" => true,
)
)
)
)
);
HOWTO設置遷移:Source
![enter image description here](https://i.stack.imgur.com/lGdsd.png)
By default Phalcon Developer Tools use the app/migrations directory to dump the migration files. You can change the location by setting one of the parameters on the generation script. Each table in the database has its respective class generated in a separated file under a directory referring its version:
![enter image description here](https://i.stack.imgur.com/oXyHC.png)
Each file contains a unique class that extends the Phalcon\Mvc\Model\Migration These classes normally have two methods: up() and down(). Up() performs the migration, while down() rolls it back.
Up() also contains the magic method morphTable(). The magic comes when it recognizes the changes needed to synchronize the actual table in the database to the description given.
<?php
use Phalcon\Db\Column as Column;
use Phalcon\Db\Index as Index;
use Phalcon\Db\Reference as Reference;
class ProductsMigration_100 extends \Phalcon\Mvc\Model\Migration
{
public function up()
{
$this->morphTable(
"products",
array(
"columns" => array(
new Column(
"id",
array(
"type" => Column::TYPE_INTEGER,
"size" => 10,
"unsigned" => true,
"notNull" => true,
"autoIncrement" => true,
"first" => true,
)
),
new Column(
"product_types_id",
array(
"type" => Column::TYPE_INTEGER,
"size" => 10,
"unsigned" => true,
"notNull" => true,
"after" => "id",
)
),
new Column(
"name",
array(
"type" => Column::TYPE_VARCHAR,
"size" => 70,
"notNull" => true,
"after" => "product_types_id",
)
),
new Column(
"price",
array(
"type" => Column::TYPE_DECIMAL,
"size" => 16,
"scale" => 2,
"notNull" => true,
"after" => "name",
)
),
),
"indexes" => array(
new Index(
"PRIMARY",
array("id")
),
new Index(
"product_types_id",
array("product_types_id")
)
),
"references" => array(
new Reference(
"products_ibfk_1",
array(
"referencedSchema" => "invo",
"referencedTable" => "product_types",
"columns" => array("product_types_id"),
"referencedColumns" => array("id"),
)
)
),
"options" => array(
"TABLE_TYPE" => "BASE TABLE",
"ENGINE" => "InnoDB",
"TABLE_COLLATION" => "utf8_general_ci",
)
)
);
// insert some products
self::$_connection->insert(
"products",
array("Malabar spinach", 14.50),
array("name", "price")
);
}
}
Once the generated migrations are uploaded on the target server, you can easily run them as shown in the following example:
![enter image description here](https://i.stack.imgur.com/uiOtB.png)
太謝謝你了!哪裏更好地找到這個代碼來自動創建表? – bvitaliyg
您只需運行一次該代碼,準備一個僅用於初始設置的文件 – markcial