[英]Can CakePHP generate my database tables from the _schema attribute on the Model?
A common task for me when I'm writing CakePHP applications is to type out an SQL file and write it into the database before running bake to generate some scaffolding. 当我编写CakePHP应用程序时,我的一个常见任务是键入一个SQL文件并将其写入数据库,然后再运行bake以生成一些脚手架。 This is one of the very few gripes I have with CakePHP - doing this ties me into MySQL, and I'm wondering if there's a better way to do it through code.
这是我与CakePHP的极少数抱怨之一 - 这使我与MySQL联系起来,我想知道是否有更好的方法通过代码来完成它。 As an example, in some frameworks I can define the columns that my model uses along with the datatype and such, and then run a command through an admin interface to "build" the database based on what what's presented in the code.
例如,在某些框架中,我可以定义模型使用的列以及数据类型等,然后通过管理界面运行命令,根据代码中显示的内容“构建”数据库。 It will do this on whatever database is sitting behind the framework.
它将在框架后面的任何数据库上执行此操作。
Is there a way for CakePHP 2.x can do something like this? CakePHP 2.x有没有办法可以做到这样的事情? I want to write out the database schema in my Model code and run a command like bake to automatically generate the tables and columns that I need.
我想在我的Model代码中写出数据库模式,并运行像bake这样的命令来自动生成我需要的表和列。 After diving into the cookbook docs, the _schema attribute seems to do what I want to do:
在深入了解cookbook文档后, _schema属性似乎做了我想做的事情:
class Post{
public $_schema = array(
'title' => array('type'=>'text'),
'description' => array('type'=>'text'),
'author' => array('type'=>'text')
);
}
but there are no examples explaining what I would I do from there. 但没有例子说明我将从那里做什么。 Does the _schema attribute serve a different purpose?
_schema属性是否有不同的用途? Any help would be appreciated!
任何帮助,将不胜感激!
not from your $_schema array itself. 而不是你的$ _schema数组本身。 but creating and using a schema file
schema.php
in /APP/Config/Schema. 但在/ APP / Config / Schema中创建和使用模式文件
schema.php
。
you can then run the bake command "cake schema create" which will then "Drop and create tables based on the schema file." 然后,您可以运行bake命令“cake schema create”,然后“根据模式文件删除并创建表”。
I might then look sth like this: 我可能会看起来像这样:
class YourSchema extends CakeSchema {
public $addresses = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 10, 'key' => 'primary'),
'contact_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 10),
'type' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 2),
'status' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 2),
'email' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 50, 'collate' => 'utf8_unicode_ci', 'comment' => 'redundant', 'charset' => 'utf8'),
'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_unicode_ci', 'engine' => 'MyISAM')
)
// more tables...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.