開発メモ

開発用のメモです。

Cakephp 住所検索用 コントローラーとモデル

コントローラー

PrefecturesController | 都道府県

<?php
class PrefecturesController extends AddressAppController {
    // public $uses = array('Prefecture');
    public $scaffold;
}

CitiesController | 市区町村

<?php
class CitiesController extends AddressAppController {
    // public $uses = array('City','Prefecture');
    public $scaffold;
}

TownsController | 町域

<?php
class TownsController extends AddressAppController {
    // public $uses = array('Town','City','Prefecture');
    public $scaffold;

}

BlocksController | 字・丁目

<?php
class BlocksController extends AddressAppController {
    // public $uses = array('Block','Town','City','Prefecture');
    public $scaffold;
}

モデル

Prefecture | 都道府県

<?php
class Prefecture extends AddressAppModel {
    var $name = "prefectures";

    public $validate = array(
        'prefecture_id' => array('numeric' => array('rule' => array('numeric'), ), ),
        'prefecture_name' => array('notEmpty' => array('rule' => array('notEmpty'), ), ),
        'prefecture_ruby' => array('notEmpty' => array('rule' => array('notEmpty'), ), ),
    );

    public $belongsTo = array();

    public $hasMany = array(
        'Block' => array(
            'className' => 'Block',
            'foreignKey' => 'prefecture_id',
            'dependent' => false,
        ),
        'City' => array(
            'className' => 'City',
            'foreignKey' => 'prefecture_id',
            'dependent' => false,
        ),
        'Town' => array(
            'className' => 'Town',
            'foreignKey' => 'prefecture_id',
            'dependent' => false,
        ),
    );

    public function __construct($id = false, $table = null, $ds = null) {
        parent::__construct($id, $table, $ds);
        $this -> diplayField = sprintf('%s.prefecture_name', $this -> alias);
    }

}

City | 市区町村

<?php
class City extends AddressAppModel {
    var $name = "cities";
    var $displayField = 'city_name';
    public $validate = array(
        'prefecture_id' => array('numeric' => array('rule' => array('numeric'), ), ),
        'city_id' => array('numeric' => array('rule' => array('numeric'), ), ),
        'city_name' => array('notEmpty' => array('rule' => array('notEmpty'), ), ),
        'city_ruby' => array('notEmpty' => array('rule' => array('notEmpty'), ), ),
    );

    public $belongsTo = array('Prefecture' => array(
            'className' => 'Address.Prefecture',
            'foreignKey' => 'prefecture_id',
            'displayField' => 'prefecture_name',
        ), );

    public $hasMany = array(
        'Block' => array(
            'className' => 'Block',
            'foreignKey' => 'city_id',
            'dependent' => false
        ),
        'Town' => array(
            'className' => 'Town',
            'foreignKey' => 'city_id',
            'dependent' => false
        ),
    );

}

Towns | 町域

<?php
class Town extends AddressAppModel {
    public $name = 'towns';
    public $displayField = 'town_name';
    public $validate = array(
        'prefecture_id' => array('numeric' => array('rule' => array('numeric'), ), ),
        'city_id' => array('numeric' => array('rule' => array('numeric'), ), ),
        'town_id' => array('numeric' => array('rule' => array('numeric'), ), ),
        'town_name' => array('notEmpty' => array('rule' => array('notEmpty'), ), ),
        'town_ruby' => array('notEmpty' => array('rule' => array('notEmpty'), ), ),
    );

    public $belongsTo = array(
        'Prefecture' => array(
            'className' => 'Prefecture',
            'foreignKey' => 'prefecture_id',
        ),
        'City' => array(
            'className' => 'City',
            'foreignKey' => 'city_id',
        ),
    );

    public $hasMany = array('Block' => array(
            'className' => 'Block',
            'foreignKey' => 'town_id',
            'dependent' => false
        ), );

}

Block | 字・丁目

<?php
class Block extends AddressAppModel {
    public $name = 'blocks';
    public $displayField = 'block_name';

    public $validate = array(
        'address_id' => array('numeric' => array('rule' => array('numeric'), ), ),
        'prefecture_id' => array('numeric' => array('rule' => array('numeric'), ), ),
        'city_id' => array('numeric' => array('rule' => array('numeric'), ), ),
        'town_id' => array('numeric' => array('rule' => array('numeric'), ), ),
        'zip' => array('notEmpty' => array('rule' => array('notEmpty'), ), ),
        'disabled_flag' => array('notEmpty' => array('rule' => array('notEmpty'), ), ),
        'office_flag' => array('notEmpty' => array('rule' => array('notEmpty'), ), ),
    );

    public $belongsTo = array(
        'Prefecture' => array(
            'className' => 'Prefecture',
            'foreignKey' => 'prefecture_id',
        ),
        'City' => array(
            'className' => 'City',
            'foreignKey' => 'city_id',
        ),
        'Town' => array(
            'className' => 'Town',
            'foreignKey' => 'town_id',
        )
    );
}
Twitter: @asahina_alice