वीर

23 เมษายน 2008

เตรียม model (+ db) สำหรับ AuthComponent ใน CakePHP 1.2.x

ก่อนที่จะใช้ AuthComponent ได้ก็ต้องมีการเตรียมการสร้าง database และ model กันนิดๆ หน่อยๆ หลังจากติดตั้ง CakePHP เรียบร้อยแล้ว. นอกจากนั้นเพื่อให้การทดลองเป็นได้โดยสะดวกผมคิดว่าควรจะสร้าง view กับ controller ง่ายๆ ไว้ใช้ในทดลองก่อน. จากนั้นก็สร้าง user table ผ่านทาง CakeSchema. แล้วก็ต่อด้วยสร้าง table ของ acl (access control list) โดยเข้าไปใน folder: cake/console แล้วสั่ง ./cake schema run create DbAcl แล้วตามด้วยการกด y ไปเรื่อยๆ เพื่อลบ table เก่าถ้ามี แล้วสร้างใหม่. ต่อด้วยการสร้าง model ของ user ใน app/models ในไฟล์ user.php:

< ?php
class User extends AppModel {
}
?>

ต่อจากนั้นก็ add user เข้าไปสักคนเพื่อเป็นการทดลอง เพื่อความสะดวกในการ add user ผมก็สร้าง CakeShell ไว้ใน folder: app/vendors/shells ชื่อไฟล์ว่า user.php:

< ?php
uses ('controller'.DS.'components'.DS.'auth');

class UserShell extends Shell {
    var $uses = array("User");
    function add() {
        $auth = new AuthComponent();
        $username = $this->args[0];
        $password = $auth->password($this->args[1]);       
        $data = array("User" =>
                      array("username" => $username,
                            "password" => $password));
        if($this->User->save($data)) {
            print "Success: $username was added\n";
        } else {
            print "Fail\n";
        }
    }
}

จากนั้นก็เข้าไปใน folder: cake/console แล้วก็เรียก ./cake user add myusername mypassword

อาจจะสั้นๆ ไปหน่อย นอกจากนั้นก็ปล่อยให้ CakePHP จัดการ.

CakeSchema

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

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/

2 เมษายน 2008

ubuntuhcl.org: เว็บ share ประสบการณ์ ด้าน Hardware ของชาว Ubuntu

Filed under: Uncategorized — वीर @ 4:12 am
Tags: , , , , , , ,

เว็บ share ประสบการณ์ ด้าน Hardware ของชาว Ubuntu. ประสบการณ์นี้ก็ประมาณว่า ใช้เครื่องรุ่นไหนบ้าง. รุ่นไหนใช้กับ Ubuntu รุ่นไหนได้ ถ้าไม่ได้มีวิธีแก้ไขอย่างไร ฯลฯ.

ผมไปสมัครไปละ … น่าผิดหวังนิดๆ ที่ใช้ OpenID ไม่ได้. แต่ก็ไม่ได้กรอกอะไรมากมาย.    นอกจาก UbuntuHCL มีลักษณะรวมศูนย์ซึ่งน่าจะทำให้ ค้นหาข้อมูลง่ายแล้ว. เวลา post review ก็ยังมี list ให้เลือกเลยว่าจะ review hardware แบบไหน ใช้ ubuntu version ไหน. ทำแบบนี้ในเรื่องเฉพาะทาง ข้อมูลก็น่าจะสะอาดครบถ้วน มากกว่าหวังพึ่ง tag มั้ง. เท่าที่เห็นตอนนี้เวลา post ใช้ BBCode หละ ใช้ HTML ไม่ได้.ที่สำคัญอีกอย่างไม่รู้ว่า Post เป็นภาษาอะไรได้บ้าง.

http://www.ubuntuhcl.org/   ข่าวมาจาก http://lwn.net/Articles/276023/rss

หน้าต่อไป »

บลอกที่ WordPress.com .