5月 27
04.schemaを作る
ここでついでなので schema を作っておきます。
自分は今 app フォルダーの中にいるので、そのまま
cake schema generate -f
と打ちます。
Welcome to CakePHP v1.2.3.8166 Console
—————————————————————
App : app
Path: /Users/bob/Sites/cake/app
—————————————————————
Cake Schema Shell
—————————————————————
Generating Schema…
Schema file: schema.php generated
とメッセージが出て終了。
/app/config/sql/schema.php というファイルが生成されます。
中味はこんな感じ。
class AppSchema extends CakeSchema {
var $name = 'App';
function before($event = array()) {
return true;
}
function after($event = array()) {
}
var $favorites = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
'name' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
);
var $members = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
'email' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100),
'password' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100),
'type_id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'index'),
'birthday' => array('type' => 'date', 'null' => true, 'default' => NULL),
'img1' => array('type' => 'string', 'null' => true, 'default' => NULL),
'img2' => array('type' => 'string', 'null' => true, 'default' => NULL),
'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'type_id' => array('column' => 'type_id', 'unique' => 0))
);
var $members_favorites = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
'member_id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'index'),
'favorite_id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'index'),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'member_id' => array('column' => 'member_id', 'unique' => 0), 'favorite_id' => array('column' => 'favorite_id', 'unique' => 0))
);
var $types = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
'name' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
);
var $users = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
'username' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100),
'password' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100),
'name' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 100),
'email' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 100),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
);
}
これを作っておけば、環境が変わって、データベースがMySQLからPostgreSQLに変わったりしても、すぐにデータベースを構築できます。
その場合は、
cake schema run create
と打ちます。
例えばこいつをPostgreSQLの環境で構築したデータベースはこうなりました。
cake_db=# ¥d
List of relations
Schema | Name | Type | Owner
--------+--------------------------+----------+----------
public | favorites | table | postgres
public | favorites_id_seq | sequence | postgres
public | members | table | postgres
public | members_favorites | table | postgres
public | members_favorites_id_seq | sequence | postgres
public | members_id_seq | sequence | postgres
public | types | table | postgres
public | types_id_seq | sequence | postgres
public | users | table | postgres
public | users_id_seq | sequence | postgres
(10 rows)
cake_db=# ¥d users
Table "public.users"
Column | Type | Modifiers
----------+------------------------+----------------------------------------------------
id | integer | not null default nextval('users_id_seq'::regclass)
username | character varying(100) | not null
password | character varying(100) | not null
name | character varying(100) | default NULL::character varying
email | character varying(100) | default NULL::character varying
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
cake_db=# ¥d members
Table "public.members"
Column | Type | Modifiers
----------+-----------------------------+------------------------------------------------------
id | integer | not null default nextval('members_id_seq'::regclass)
email | character varying(100) | not null
password | character varying(100) | not null
type_id | integer | not null
birthday | date |
img1 | character varying(255) | default NULL::character varying
img2 | character varying(255) | default NULL::character varying
created | timestamp without time zone | not null
modified | timestamp without time zone | not null
Indexes:
"members_pkey" PRIMARY KEY, btree (id)
"type_id" btree (type_id)
cake_db=# ¥d favorites
Table "public.favorites"
Column | Type | Modifiers
--------+------------------------+--------------------------------------------------------
id | integer | not null default nextval('favorites_id_seq'::regclass)
name | character varying(100) | not null
Indexes:
"favorites_pkey" PRIMARY KEY, btree (id)
cake_db=# ¥d types
Table "public.types"
Column | Type | Modifiers
--------+------------------------+----------------------------------------------------
id | integer | not null default nextval('types_id_seq'::regclass)
name | character varying(100) | not null
Indexes:
"types_pkey" PRIMARY KEY, btree (id)
cake_db=# ¥d members_favorites
Table "public.members_favorites"
Column | Type | Modifiers
-------------+---------+----------------------------------------------------------------
id | integer | not null default nextval('members_favorites_id_seq'::regclass)
member_id | integer | not null
favorite_id | integer | not null
Indexes:
"members_favorites_pkey" PRIMARY KEY, btree (id)
"favorite_id" btree (favorite_id)
"member_id" btree (member_id)
まあデータベースが変わるなんてことはないかもしれないし、後でやっても構わない訳ですが、作っておけば後々便利ということでせっかく今ターミナルを開いてる訳ですからそのままやってしまった方がよい、ということです。
