CakeSchema จริงๆ แล้วผมก็ไม่ค่อยเข้าใจเท่าไหร่ว่ามันเอาไว้ทำอะไรบ้าง. เข้าไปดูในเว็บต่างๆ[1] เขาบอกว่าเอาไปใช้ทำ Migration ได้แบบใน Ruby on Rails แต่ผมก็งงๆ อยู่ดีว่า migration เอาไว้ทำอะไรบ้าง. เท่าที่เห็นได้คร่าวๆ คือไม่ต้องเขียน SQL ตรงๆ แต่มาเขียน PHP แล้ว CakePHP จะไป generate SQL ให้แทน เวลาสร้าง database. แล้วก็เวลาที่จะเปลี่ยนแปลงโครงสร้างของ table ก็ทำผ่าน CakePHP ได้ด้วย … แบบนี้เขาเรียก migration หรือเปล่า?
ผมจะลองใช้ CakeSchema มาสร้าง table สำหรับเก็บ username และ password ดู. เริ่มแรกก็สร้างไฟล์ users.php ใน app/config/sql ดังนี้:
array('type'=>'integer',
'null' => false,
'key' => 'primary',
'extra' => 'auto_increment'),
'username' => array('type'=>'string',
'null' => false,
'length' => 255),
'password' => array('type'=>'string',
'null' => false,
'length' => 255));
}
?>
จากนั้นเวลาจะสรั้าง database ก็ cd เข้าไปใน folder: cake/console แล้วก็สั่ง
./cake schema run create users เสร็จแล้วก็ตอบ y ไปเรื่อยๆ ก็เป็นอันใช้ได้. หน้าจะเป็นประมาณด้านล่างนี้
Hello vee,
Welcome to CakePHP v1.2.0.6311 beta Console
---------------------------------------------------------------
Cake Schema Shell
---------------------------------------------------------------
The following tables will drop.
users
Are you sure you want to drop the tables? (y/n)
[n] > y
Dropping tables.
users updated.
The following tables will create.
users
Are you sure you want to create the tables? (y/n)
[y] > y
Creating tables.
users updated.
End create.
ส่วนเรื่อง migration หรืออื่นๆ ไว้ให้ผมทำเป็นก่อน คงจะได้เขียนต่อไป.
อ้างอิง
[1] http://cakebaker.42dh.com/2008/04/13/migrations-the-cakephp-way/