Home > PHP | フレームワーク > CakePHPではまったこと21(DB初期化のテンプレート)

CakePHPではまったこと21(DB初期化のテンプレート)

ひさびさにCakePHPをいじると、やっぱり疲れる。
簡単なところではsaveですでにはまる。
ループの中にsaveを入れると、最後のsaveしか実行されない。前にそんなことを書いた気がするけど書いてなかった。そういう時はcreateしとけば良いっぽいけど、なんか違う気もする。


foreach ($names as $name) {
	$data = array('User' => array());
	$data['User']['name'] = $name;
	
	$this->User->save($data);
	$this->User->create();
}

まあ、今回は新しくDBを作成(初期化)するときのテンプレートをメモっておく。

・MySQL(4.x?)の場合


DROP TABLE IF EXISTS users;
CREATE TABLE users (
	id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	name CHAR(100) NOT NULL UNIQUE,
	password CHAR(100) NOT NULL,
	nickname CHAR(32),
	profile BLOB,
	confirm INTEGER DEFAULT 0,
	created CHAR(40) DEFAULT NULL,
	modified CHAR(40) DEFAULT NULL
);

テーブルがある場合は最初に削除してる。
idは自動でインクリメントされるように「AUTO_INCREMENT PRIMARY KEY」を指定、createdとmodifiedはCHAR(40)くらいにしとく。

・SQLite(2)の場合


DROP TABLE users;
CREATE TABLE users (
	id INTEGER PRIMARY KEY,
	name CHAR(100) NOT NULL UNIQUE,
	password CHAR(100) NOT NULL,
	nickname CHAR(32) NOT NULL,
	profile BLOB,
	confirm INTEGER DEFAULT 0,
	created DEFAULT NULL,
	modified DEFAULT NULL
);

DROP文にIFが使えない。これを実行したときにusersテーブルがないとエラーが出るけど、まあ気にしない。
idは自動でインクリメントされるように「INTEGER PRIMARY KEY」を指定、createdとmodifiedにCHARとか指定するとうまくいかなかったような・・・。まあ、SQLiteなんで型は適当でいいはず。

以上

Comments:5

ray ban mirrored aviators worn 17-11-12 (日) 17:39

You could certainly see your expertise in the work you write. The world hopes for more passionate writers like you who aren’t afraid to say how they believe. Always follow your heart.

restart energy 18-03-15 (木) 20:39

you are truly a excellent webmaster. The web site loading velocity is amazing.
It kind of feels that you are doing any unique trick. Moreover,
The contents are masterwork. you have performed a magnificent activity
on this subject!

china 18-04-18 (水) 20:31

Hi mates, its fantastic article about tutoringand completely defined,
keep it up all the time.

lưới chắn rác 18-08-15 (水) 11:58

Hi to all, for the reason that I am really eager of reading this weblog’s post to be updated on a regular basis.
It carries good material.

Cora 19-08-14 (水) 5:53

However, preparing any other part, it could be a smart idea
to, yourself would try to determine the type of look you need to placed on the perfect day so that once the makeup artist
is there it will likely be feasible for the right results on you to make the style
that you would like to achieve. Some professionals even believe that enough time they are getting their face done
is time for reflection and relaxation before
becoming started in their performance. Eyelashes are the way to enhance your vision,
yes even if you have long eyelashes, and an easy,natural approach to make your makeup unique of
another day.

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://red-treasure.com/report/wp-trackback.php?p=213
Listed below are links to weblogs that reference
CakePHPではまったこと21(DB初期化のテンプレート) from コスミー報告書[社外秘]

Home > PHP | フレームワーク > CakePHPではまったこと21(DB初期化のテンプレート)

Search
Feeds
Meta
 合計:011636
 今日:0107 昨日:0165

Return to page top