依赖下拉框CakePHP 3

依赖下拉框CakePHP 3,php,mysql,cakephp,cakephp-3.0,Php,Mysql,Cakephp,Cakephp 3.0,我已经创建了一个国家、城市和客户表,我试图确保当我从下拉列表中添加新客户时,我可以选择一个国家,然后选择与该国家相关的城市。目前我无法从下拉列表中选择任何城市和国家组合。 这是我的数据库 CREATE TABLE IF NOT EXISTS `southpac_team`.`customers` ( `id` INT NOT NULL, `name` VARCHAR(100) NULL, `country_id` INT NULL, `city_id` INT NULL, `

我已经创建了一个国家、城市和客户表,我试图确保当我从下拉列表中添加新客户时,我可以选择一个国家,然后选择与该国家相关的城市。目前我无法从下拉列表中选择任何城市和国家组合。 这是我的数据库

CREATE TABLE IF NOT EXISTS `southpac_team`.`customers` (
  `id` INT NOT NULL,
  `name` VARCHAR(100) NULL,
  `country_id` INT NULL,
  `city_id` INT NULL,
  `address` VARCHAR(255) NULL,
  `postal_address` VARCHAR(255) NULL,
  `phone` VARCHAR(45) NULL,
  `email` VARCHAR(100) NULL,
  `payment_terms_id` INT NULL,
  `stop_credit` TINYINT(1) NULL,
  `gst_percentage` INT NULL,
  `currency` VARCHAR(45) NULL,
  `account_closed` TINYINT(1) NULL,
  `invoice_email` VARCHAR(100) NULL,
  `customer_notes` VARCHAR(255) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `southpac_team`.`countries` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `southpac_team`.`cities` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `country_id` INT NOT NULL,
  `name` VARCHAR(100) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;
我用蛋糕烘焙来创建表之间的关系,这是我的客户控制器

<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * Customers Controller
 *
 * @property \App\Model\Table\CustomersTable $Customers
 */
class CustomersController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['Countries', 'Cities', 'PaymentTerms']
        ];
        $customers = $this->paginate($this->Customers);

        $this->set(compact('customers'));
        $this->set('_serialize', ['customers']);
    }

    /**
     * View method
     *
     * @param string|null $id Customer id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $customer = $this->Customers->get($id, [
            'contain' => ['Countries', 'Cities', 'PaymentTerms']
        ]);

        $this->set('customer', $customer);
        $this->set('_serialize', ['customer']);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $customer = $this->Customers->newEntity();
        if ($this->request->is('post')) {
            $customer = $this->Customers->patchEntity($customer, $this->request->data);
            if ($this->Customers->save($customer)) {
                $this->Flash->success(__('The customer has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The customer could not be saved. Please, try again.'));
        }
        $countries = $this->Customers->Countries->find('list', ['limit' => 200]);
        $cities = $this->Customers->Cities->find('list', ['limit' => 200]);
        $paymentTerms = $this->Customers->PaymentTerms->find('list', ['limit' => 200]);
        $this->set(compact('customer', 'countries', 'cities', 'paymentTerms'));
        $this->set('_serialize', ['customer']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Customer id.
     * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $customer = $this->Customers->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $customer = $this->Customers->patchEntity($customer, $this->request->data);
            if ($this->Customers->save($customer)) {
                $this->Flash->success(__('The customer has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The customer could not be saved. Please, try again.'));
        }
        $countries = $this->Customers->Countries->find('list', ['limit' => 200]);
        $cities = $this->Customers->Cities->find('list', ['limit' => 200]);
        $paymentTerms = $this->Customers->PaymentTerms->find('list', ['limit' => 200]);
        $this->set(compact('customer', 'countries', 'cities', 'paymentTerms'));
        $this->set('_serialize', ['customer']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Customer id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $customer = $this->Customers->get($id);
        if ($this->Customers->delete($customer)) {
            $this->Flash->success(__('The customer has been deleted.'));
        } else {
            $this->Flash->error(__('The customer could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}

我可以创建新客户,选择国家和城市。我只想限制投递箱,这样我就只能为正确的国家选择一个城市。我曾试图对此进行研究,但刚刚遇到了使用jshelper的示例,CakePHP3.0不包括这些示例。
谢谢。

你可以试试jQuery的链接选择插件

所需的源文件:

ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
jquery.chained.min.js

例如:

$(document).ready(function() { 
    $("#cityId").chained("#countryId");
});

您可以使用jquery链接库,但我不知道如何将其安装到我的web服务器上。谢谢Mark!你可能想在沙箱里看看。Sry,链接被破坏了,现在已经修复。我尝试了这个,但是我无法让链接插件工作,但是我可以使用JQuery获得弹出消息。我也试着遵循这个例子,但它似乎不起作用。
$(document).ready(function() { 
    $("#cityId").chained("#countryId");
});