वीर

23 เมษายน 2008

CakeSchema

Filed under: Uncategorized — Tags: , , , , , , — वीर @ 07:08

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 ดังนี้:

< ?php
class UsersSchema extends CakeSchema {

        var $name = 'users';

        function before($event = array()) {
                return true;
        }

        function after($event = array()) {
        }

        var $users =
        array('id' => 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/

4 ความคิดเห็น »

  1. [...] ง่ายๆ ไว้ใช้ในทดลองก่อน. จากนั้นก็สร้าง user table ผ่านทาง CakeSchema. แล้วก็ต่อด้วยสร้าง table ของ acl (access control list) [...]

    Pingback โดย เตรียม model (+ db) สำหรับ AuthComponent ใน CakePHP 1.2.x « वीर — 23 เมษายน 2008 @ 08:46

  2. [...] ง่ายๆ ไว้ใช้ในทดลองก่อน. จากนั้นก็สร้าง user table ผ่านทาง CakeSchema. แล้วก็เตรียม model ของ users table พร้อมด้วย add [...]

    Pingback โดย ใช้ AuthComponent ของ CakePHP 1.2.x อย่างง่าย « वीर — 23 เมษายน 2008 @ 09:42

  3. ผมชอบ tag cakephp ของคุณจังครับ ;) migration เนี้ยไม่รู้ว่าเท่าของ rails ไหมแต่ของ rails เนี้ยเจ้าตัว rake มันสามารถย้อนการ migrate ได้ทำให้เวลาเราเปลี่ยนแปลง โครงสร้างของ database แล้วมีปัญหาเราก็ย้อนกลับไปใช้ของตัวเก่าได้อย่างทันที

    ประโยชน์ของ migrate ก็คงเป็นเรื่องการสร้าง / เปลี่ยนแปลง db table โดยไม่ต้องผ่าน sql ละมั่งครับ (แต่ผมว่าถ้ามันย้อนได้ด้วยเหมือน rails ก็คงดีเหมือนกัน)

    ความเห็น โดย DominixZ — 15 สิงหาคม 2008 @ 09:07

  4. อัพ แอนด์ ดาวน์ version ได้ครับ http://code.google.com/p/cakephp-migrations/wiki/HowToUse :-)

    ความเห็น โดย वीर — 15 สิงหาคม 2008 @ 10:34


RSS feed สำหรับความคิดเห็นในกระทู้นี้ TrackBack URI

เขียนความคิดเห็นของคุณ

บลอกที่ WordPress.com .