在简要回顾了 phalcon 文档后,我想您正在寻找类似的东西 => 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,
)
)
)
)
);
如何设置迁移:Source
默认情况下,Phalcon 开发者工具使用 app/migrations 目录
转储迁移文件。您可以通过设置一个来更改位置
生成脚本上的参数。数据库中的每个表
将其各自的类生成在一个单独的文件中
引用其版本的目录:
每个文件都包含一个独特的类,该类扩展了
Phalcon\Mvc\Model\Migration 这些类通常有两种方法:
上和下()。 Up() 执行迁移,而 down() 滚动它
返回。
Up() 还包含魔法方法 morphTable()。魔法什么时候来
它识别同步实际表所需的更改
数据库到给定的描述。
<?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")
);
}
}
将生成的迁移上传到目标服务器后,您
可以很容易地运行它们,如下例所示: