開発メモ

開発用のメモです。

Cakephp で scaffold で コンボボックスを条件絞る

リレーション項目名 user_id の場合 users のように _ids に変更したものを Filterで処理をする。

<?php
App::uses('UsersAppController', 'Users.Controller');
/**
 * Users Controller
 *
 */
class UsersController extends UsersAppController {

    public $scaffold;

    public $uses = array(
        "Users.User",
        "Address.Prefecture",
        "Address.City",
        "Address.Town",
        "Address.Block"
    );

    public function beforeFilter() {
        if (strcmp($this -> action, "index") == 0 || strcmp($this -> action, "view") == 0) {
            $this -> User -> belongsTo = array(
                'Prefecture' => array(
                    'className' => 'Address.Prefecture',
                    'foreignKey' => 'prefecture_id',
                ),
                'City' => array(
                    'className' => 'Address.City',
                    'foreignKey' => 'city_id'
                ),
                'Town' => array(
                    'className' => 'Address.Town',
                    'foreignKey' => 'town_id'
                ),
                'Block' => array(
                    'className' => 'Block.Town',
                    'foreignKey' => 'address_id'
                ),
            );
        } else {
            $this -> set("prefectures", $this -> Prefecture -> find("list"));
            $pid = 0;
            $cid = 0;
            $tid = 0;

            if (isset($this -> request -> params["pass"][0])) {
                $user = $this -> User -> findById($this -> request -> params["pass"][0]);
                if (isset($user["User"]["prefecture_id"])) {
                    $pid = $user["User"]["prefecture_id"];
                }
                if (isset($user["User"]["city_id"])) {
                    $cid = $user["User"]["city_id"];
                }
                if (isset($user["User"]["town_id"])) {
                    $tid = $user["User"]["town_id"];
                }
            }

            if (isset($this -> request -> data["User"]["prefecture_id"])) {
                $pid = $this -> request -> data["User"]["prefecture_id"];
            }
            if (isset($this -> request -> data["User"]["city_id"])) {
                $cid = $this -> request -> data["User"]["city_id"];
            }
            if (isset($this -> request -> data["User"]["town_id"])) {
                $tid = $this -> request -> data["User"]["town_id"];
            }
            $this -> set("cities", $this -> City -> find("list", array("conditions" => array("prefecture_id" => $pid))));
            $this -> set("towns", $this -> Town -> find("list", array("conditions" => array(
                    "prefecture_id" => $pid,
                    "city_id" => $cid
                ))));
            $this -> set("addresses", $this -> Block -> find("list", array("conditions" => array(
                    "prefecture_id" => $pid,
                    "city_id" => $cid,
                    "town_id" => $tid
                ))));
        }
    }

}
Twitter: @asahina_alice