「cakephp」カテゴリーアーカイブ

CakePHP2系 の更新処理を Ajax

http://blog.xao.jp/blog/cakephp/one-of-the-simple-way-for-using-ajax-in-cakephp/
http://ustom.daa.jp/blog/2012/11/js%E3%83%98%E3%83%AB%E3%83%91%E3%83%BC%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6%E3%80%80%E3%81%9D%E3%81%AE%EF%BC%91%EF%BD%9Ccakephp2/

test_controller.php

[php]
class TestController extends AppController {

var $name = ‘Test’;
var $helpers = array(‘Form’, ‘Js’=>array(‘jquery’) );

function index() {
$this->Test->recursive = 0;
$this->set(‘tests’, $this->paginate());
}

function test() {
}
}[/php]
test.ctp
[php]
<?php
$this->Html->script(‘jquery’,array(‘inline’=>false));

$this->Js->get(‘#btn’);
$request = $this->Js->request(
array(
‘action’ => ‘index’, //indexを表示
‘controller’ => ‘test’ //textコントローラーの
),
array(
‘method’ => ‘get’,
‘sync’ => true,
‘update’ => ‘#data’, //id="data"に表示させるよ
‘success’ => "alert(‘get data!!!’);"
)
);
$this->Js->event(‘click’,$request);
echo $this->Js->writeBuffer(array(‘inline’=>false));
?>

<div>
<div id="data"></div> //ここにindexを表示させるよ
<input id="btn" type="button" value="取得" />

</div>
[/php]

hasOneの一括保存

http://cakephp.jp/modules/newbb/viewtopic.php?topic_id=796&forum=6
[php]
public function add() {
if ($this->request->is(‘post’)) {
$this->User->create();
if (!empty($this->data)) {
$user = $this->User->save($this->data);
//if ($this->User->save($this->request->data)) {

// User が保存されたら、User_detail データに User の情報を追加し
// 保存します。
if (!empty($user)) {
// 新しく生成した User の ID は、
// $this->User->id にセットされます。
$this->User->UserDetail->data[‘UserDetail’][‘user_id’] = $this->User->id;

// User は User_detail と hasOne のアソシエーションで関連付いているので
// User モデルを通して Profile モデルにアクセスできます:
$this->User->UserDetail->save($this->data);
}
$this->Session->setFlash(
__(‘保存しました’, __(‘user’)),
‘alert’,
array(
‘plugin’ => ‘TwitterBootstrap’,
‘class’ => ‘alert-success’
)
);

$this->redirect(array(‘action’ => ‘index’));
} else {
$this->Session->setFlash(
__(‘保存失敗’, __(‘user’)),
‘alert’,
array(
‘plugin’ => ‘TwitterBootstrap’,
‘class’ => ‘alert-error’
)
);
}
}
$lectures = $this->User->Lecture->find(‘list’);
$prefectures = $this->User->Prefecture->find(‘list’);
$userDetails = $this->User->UserDetail->find(‘list’);
$numbers = $this->User->Number->find(‘list’);
$this->set(compact(‘lectures’, ‘prefectures’, ‘userDetails’,’numbers’));
debug($userDetails);
}
[/php]
[php]
public $hasOne = array(
‘UserDetail’ => array(
‘className’ => ‘UserDetail’,
‘foreignKey’ => ‘user_id’,
‘conditions’ => ”,
‘fields’ => ”,
‘order’ => ”
)
);[/php]

idを取るリンク

Controller
[php]
public function view($id = null) {
$this->Post->id = $id; //idを取る
$this->set(‘post’, $this->Post->read()); //あるIDのデータをフィールド情報+データで受け取る
}
[/php]

View
[php]
<ul>
<?php foreach ($posts as $post) : ?>
<li>
<?php
// debug($post);
// echo h($post[‘Post’][‘title’]);
//
echo $this->Html->link($post[‘Post’][‘title’],’/posts/view/’.$post[‘Post’][‘id’]); //idにリンクをはる
?>
</li>
<?php endforeach; ?>
</ul>[/php]

変数

[php]
public function index() {
$params = array(   //params変数の中身を書く
‘order’ => ‘modified desc’,
‘limit’ => 2
);
$this->set(‘posts’, $this->Post->find(‘all’, $params)); //params変数を呼び出し
$this->set(‘title_for_layout’, ‘記事一覧’);
}
[/php]

routes.php

/app/Config/routes.php
[php] Router::connect(‘/’, array(‘controller’ => ‘posts’, ‘action’ => ‘index’));
//どのコントローラのページをルートに表示させるか。postsページのindexページを表示させる
[/php]

foreach

Controller
[php]
<?php

class PostsController extends AppController {
public $helpers = array(‘Html’, ‘Form’); //ヘルパー使いますよ
public function index() {
$this->set(‘posts’, $this->Post->find(‘all’)); // posts変数をviewで使うよ Postのデータを全部取得するよ
}

}
[/php]

Viwe
[php]
<ul>
<?php foreach ($posts as $post) : ?> // foreachでぐるぐると取得するよ 
<li>
<?php
// debug($post);
echo h($post[‘Post’][‘title’]);
?>
</li>
<?php endforeach; ?> // foreachでぐるぐると取得の締め
</ul>
[/php]

findメソッド :CakePHP2.1

http://w.builwing.info/2012/05/16/cakephp2-1%E3%81%AEfind%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89/

[php]
find(‘all’, array(
‘conditions’ => array(‘Article.id’ => 1), ////検索条件の配列
‘fields’ => array(‘"User2"."id"’), ////フィールド名の配列
‘order’ => ‘Article.created’ => ‘desc’, //並び順を文字列または配列で指定
‘recursive’ => 2,
‘group’ => ‘type’
));
[/php]

Form ヘルパー

フォーム開始タグ生成
[php] //$this->Form->create(保存先モデル名, フォームタグの属性とその値);
<?php echo $form->create(null,array(‘type’=>’post’,’action’=>’.’)); ?>[/php]
[php]//$this->Form->text(name 属性で指定される名前。「モデル名.フィールド名」)
<?php echo $form->text(‘HelloForm.text1’); ?>
[/php]
[php]//$this->Form->radio(name 属性で使用される名前, value と label の値の配列, Form ヘルパーの属性設定)
<?php echo $form->radio("HelloForm.radio1",
array(‘男性’=>’男性’,’女性’=>’女性’,’両性’=>’両性’),
array(‘legend’=>’性別を選択’)); ?>
[/php]
[php]//$this->Form->select(name 属性で使用される名前, value と画面に表示される値の配列, Form ヘルパーの属性設定)
<?php echo $form->select(‘HelloForm.select1’,
array(‘未成年’=>’未成年’,’社会人’=>’社会人’,’その他’=>’その他’),null,
array(‘size’=>4,’empty’=>’※項目を選択’),true); ?>
[/php]
[php]<?php echo $form->end("送信"); ?>[/php]

[php]<?php echo $form->create(null,array(‘type’=>’post’,’action’=>’.’));
echo "</pre>";
print_r($form->value(‘HelloForm.date’));
echo "</pre>";
echo "<br/>";
echo $form->dateTime(‘HelloForm.date’,’YMD’);
echo $form->end("送信");
?>[/php]
[php]<!–チェックボックス–>
<?=$form->create(null,array(‘type’=>’post’,’action’=>’.’)) ?>
<?=$form->checkbox("HelloForm.check1",array(‘checked’=>true)) ?>
<?=$form->label(‘HelloForm.check1’) ?>
<?=$form->end("送信") ?>
<!–チェックボックス_end–>[/php]
[php]<!–テキストエリア–>
<?=$form->create(null,array(‘type’=>’post’,’action’=>’.’)) ?>
<?=$form->textarea("HelloForm.check1",array(‘checked’=>true)) ?>
<?=$form->label(‘HelloForm.check1’) ?>
<?=$form->end("送信") ?>
<!–テキストエリ_end–>[/php]
[php] <!–パスワード–>
<?=$form->create(null,array(‘type’=>’post’,’action’=>’.’)) ?>
<?=$form->password("HelloForm.check1",array(‘checked’=>true)) ?>
<?=$form->label(‘HelloForm.check1’) ?>
<?=$form->end("送信") ?>
<!–パスワード_end–>[/php]
[php]<!–フォームタグ–>
<?php echo $form->create(null,array(‘type’ => ‘post’, ‘action’ => ‘.’)); ?>
<?php echo $form->radio("HelloForm.radio1",array(‘ウインドウズ’=>’Windows’,’リナックス’=>’Linux’,’マックOS’=>’MacOS X’),
array(‘legend’=>’OSを選択’,’value’=>’リナックス’)); ?>
<?php echo $form->end("送信"); ?>
<!–フォームタグ_end–>[/php]
[php]<!–セレクトフォームタグ01–>
<?php echo $form->create(null,array(‘type’ => ‘post’, ‘action’ => ‘.’)); ?>
<?php echo $form->select("HelloForm.radio1",array(‘ウインドウズ’=>’Windows’,’リナックス’=>’Linux’,’マックOS’=>’MacOS X’),
array(‘legend’=>’OSを選択’,’value’=>’リナックス’)); ?>
<?php echo $form->end("送信"); ?>
<!–フォームタグ_end–> [/php]
[php]<!–セレクトフォームタグ02–>
<?php echo $form->create(null,array(‘type’ => ‘post’, ‘action’ => ‘.’)); ?>
<?php echo $form->select("HelloForm.radio1",array(‘ウインドウズ’=>’Windows’,’リナックス’=>’Linux’,’マックOS’=>’MacOS X’),’リナックス’,
array(‘size’=>4,’empty’=>’項目を選んでください’),true); ?>
<?php echo $form->end("送信"); ?>
<!–フォームタグ_end–>[/php]