Created
February 6, 2011 01:47
-
-
Save nissuk/813020 to your computer and use it in GitHub Desktop.
CakePHP datasources plugin の ArraySource を使用する例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// {app}/config/database.php | |
class DATABASE_CONFIG | |
{ | |
var $default = array( | |
// デフォルトのDB設定... | |
); | |
// https://github.com/cakephp/datasources を導入してArraySource用のDB設定を追加します。 | |
var $array = array( | |
'datasource' => 'Datasources.ArraySource' | |
); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// {app}/models/gender.php | |
class Gender extends AppModel | |
{ | |
var $name = 'Gender'; | |
// DB設定をArraySourceのものにします。 | |
var $useDbConfig = 'array'; | |
var $displayField = 'name'; | |
var $order = 'Gender.display_index'; | |
// レコードをarrayで設定します。 | |
var $records = array( | |
array('id' => 1, 'name' => '男性', 'display_index' => 1), | |
array('id' => 2, 'name' => '女性', 'display_index' => 2), | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// {app}/models/user.php | |
class User extends AppModel | |
{ | |
var $name = 'User'; | |
var $displayField = 'username'; | |
// MySQL等のデータベースを使うときと同じように、関連を設定します。 | |
var $belongsTo = array( | |
'Gender' => array( | |
'className' => 'Gender', | |
'foreignKey' => 'gender_id' | |
) | |
); | |
} | |
/* | |
CREATE TABLE users ( | |
id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, | |
username varchar(255) NOT NULL DEFAULT '', | |
gender int unsigned NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// {app}/controllers/user_controller.php | |
class UsersController extends AppController | |
{ | |
var $name = 'Users'; | |
var $helpers = array('Html', 'Form'); | |
function add() | |
{ | |
if (!empty($this->data)) { | |
$this->User->create(); | |
if ($this->User->save($this->data)) { | |
$this->Session->setFlash(__('The user has been saved', true)); | |
$this->redirect(array('action' => 'index')); | |
} else { | |
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true)); | |
} | |
} | |
// ArraySourceで定義したGenderモデルのリストをviewに渡します。 | |
$this->set('genders', $this->User->Gender->find('list')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // {app}/views/users/add.ctp ?> | |
<div class="users form"> | |
<?php echo $this->Form->create('User');?> | |
<fieldset> | |
<legend><?php __('Add User'); ?></legend> | |
<?php | |
echo $this->Form->input('username'); | |
// 上手くいっていれば、ここで性別のセレクトボックスが表示されます。 | |
echo $this->Form->input('gender'); | |
?> | |
</fieldset> | |
<?php echo $this->Form->end(__('Submit', true));?> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment