CakePHP Behavior

+6

No comments posted yet

Comments

Slide 1

CakePHP Behavior の紹介

Slide 2

自己紹介

Slide 3

フリーのプログラマー   BLOG : Sun Limited Mt.  Twitter : syuhari Skype : syuharix

Slide 4

Behavior  使ってますか?

Slide 5

Behavior とは

Slide 6

振る舞い 行為

Slide 7

Controller : Component View : Helper Model : Behavior

Slide 8

モデルに 機能を 追加する

Slide 9

使い方

Slide 10

インストール app/models/behaviors

Slide 11

<?php class Model extends AppModel  {   var $name = 'Model';   var $actsAs =           array('Behavior'); } ?>

Slide 12

実際に 開発で使った

Slide 13

ユーザ情報は ソフトデリートで!

Slide 14

beforeDelete, afterFind とか 使えば出来そう

Slide 15

お店情報も ソフトデリートで!

Slide 16

Soft Deletable Behavior   http://bakery.cakephp.org/articles/view/soft-delete-behavior

Slide 17

CREATE TABLE `test`.`users` (   `id` TINYINT NOT NULL                   AUTO_INCREMENT ,   `name` VARCHAR( 20 ) NOT NULL ,   `created` DATETIME NOT NULL ,   `modified` DATETIME NOT NULL ,   PRIMARY KEY ( `id` ) );

Slide 18

mysql> select * from users; +----+----------+---------------------+---------------------+ | id | name     | created             | modified            | +----+----------+---------------------+---------------------+ |  1 | akahoshi | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  2 | sekimoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  3 | toritani | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  4 | kanemoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  5 | arai     | 2009-05-21 13:37:05 | 2009-05-21 13:37:05 | |  6 | mench    | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  7 | fujimoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  8 | kanou    | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  9 | ando     | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | +----+----------+---------------------+---------------------+ 9 rows in set (0.00 sec)

Slide 21

mysql> select * from users; +----+----------+---------------------+---------------------+ | id | name     | created             | modified            | +----+----------+---------------------+---------------------+ |  1 | akahoshi | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  2 | sekimoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  3 | toritani | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  4 | kanemoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  5 | arai     | 2009-05-21 13:37:05 | 2009-05-21 13:37:05 | |  7 | fujimoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  8 | kanou    | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | |  9 | ando     | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 | +----+----------+---------------------+---------------------+ 8 rows in set (0.00 sec)

Slide 22

SofeDeletable Behavior を使う <?php class Users extends AppModel  {   var $name = 'User';   var $actsAs =           array('SoftDeletable'); } ?>

Slide 23

DB にカラム追加 ALTER TABLE `users`    ADD `deleted`   TINYINT NOT NULL ; ALTER TABLE `users`    ADD `deleted_date`    DATETIME NULL ;

Slide 24

mysql> select * from users; +----+----------+---------------------+---------------------+---------+--------------+ | id | name     | created             | modified            | deleted | deleted_date | +----+----------+---------------------+---------------------+---------+--------------+ |  1 | akahoshi | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL         | |  2 | sekimoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL         | |  3 | toritani | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL         | |  4 | kanemoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL         | |  5 | arai     | 2009-05-21 13:37:05 | 2009-05-21 13:37:05 |       0 | NULL         | |  6 | mench    | 2009-05-21 13:36:55 | 2009-05-21 14:12:06 |       0 | NULL         | |  7 | fujimoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL         | |  8 | kanou    | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL         | |  9 | ando     | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL         | +----+----------+---------------------+---------------------+---------+--------------+ 9 rows in set (0.00 sec)

Slide 27

mysql> select * from users; +----+----------+---------------------+---------------------+---------+---------------------+ | id | name     | created             | modified            | deleted | deleted_date        | +----+----------+---------------------+---------------------+---------+---------------------+ |  1 | akahoshi | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL                | |  2 | sekimoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL                | |  3 | toritani | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL                | |  4 | kanemoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL                | |  5 | arai     | 2009-05-21 13:37:05 | 2009-05-21 13:37:05 |       0 | NULL                | |  6 | mench    | 2009-05-21 13:36:55 | 2009-05-21 14:16:41 |       1 | 2009-05-21 14:16:41 | |  7 | fujimoto | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL                | |  8 | kanou    | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL                | |  9 | ando     | 2009-05-21 13:36:55 | 2009-05-21 13:36:55 |       0 | NULL                | +----+----------+---------------------+---------------------+---------+---------------------+ 9 rows in set (0.00 sec)

Slide 28

コードを1行 追加するだけ!

Slide 29

その他の Behavior

Slide 30

Sluggable Behavior http://bakery.cakephp.org/articles/view/sluggable-behavior

Slide 31

SEO Friendly な URL を作れる ID ではなくスラグでモデルを扱う

Slide 32

http://bakery.cakephp.org/articles/view/1 とかではなく、   http://bakery.cakephp.org/articles/view/sluggable-behavior

Slide 33

PublishableBehavior   開始日時、終了日時で 記事の公開を コントロールできる

Slide 34

Bakery で探そう! http://bakery.cakephp.org/tags/view/behavior

Summary: 第4回 CakePHP@Tokyo LT

URL: