【问题标题】:CakePHP Chained Plugin Dropdown not linking [duplicate]CakePHP 链式插件下拉菜单未链接 [重复]
【发布时间】:2026-01-27 21:40:02
【问题描述】:

我正在尝试使用链接到限制用户为错误国家/地区选择城市的插件。在我没有选择国家的那一刻,它让我从所有城市中进行选择。但是一旦我选择了一个国家,它就不会让我选择任何城市。我已经使用来自 Chained 网站的代码让这个插件正常工作。 我的破碎国家/城市版本可以在这里看到 登录:访客 密码:密码 http://team.southpacificavionics.com/customers/add 如果您将该网址上的“添加”更改为“测试”,则可以看到我的测试版本(对不起,我不能发布更多链接)。 我使用 ./cake bake 来创建我的客户、国家和城市控制器、模型、模板和关键关系。

You can see from this image that the cities are linked to countries

这是我给客户的 add.ctp

 <?php echo $this->Html->script('jquery.min'); ?>
 <?php echo $this->Html->script('jquery.chained'); ?>

<script type="text/javascript">
    $(document).ready(function () {
        $("#cities").chained("#countries");
    });
</script>

<script type="text/javascript">
    $(document).ready(function () {
        alert('java is working');
    });
</script>

<?php
/**
  * @var \App\View\AppView $this
  */
?>

<div class="customers form large-9 medium-8 columns content">
    <?= $this->Form->create($customer) ?>
    <fieldset>
        <legend><?= __('Add Customer') ?></legend>

        <?php
            echo $this->Form->input('country_id', ['options' => $countries, 'empty' => true,'id'=>'countries']);
            echo $this->Form->input('city_id', ['options' => $cities, 'empty' => true,'id'=>'cities']);

        ?>
    </fieldset>

    <?= $this->Form->end() ?>

这是我的mysql代码

CREATE TABLE IF NOT EXISTS `southpac_team`.`customers` (
  `id` INT NOT NULL,
  `country_id` INT NULL,
  `city_id` INT 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;

【问题讨论】:

  • 您在 thisthis 多次发布相同的内容。在问题解决之前你不应该接受答案

标签: php jquery mysql cakephp chained


【解决方案1】:

您没有在您的城市下拉选项中添加班级,但您应该在城市下拉列表中添加国家/地区 ID 作为班级

如何在 CakePHP Dropdown 中添加类

$items = $this->Cities->find('all')->all()->toArray();
        $cities = [];
        foreach ($items as $key => $value) {
              $cities[$key]['value'] = $value['id'];
              $cities[$key]['text'] = $value['indent_no'];
              $cities[$key]['class'] = $value['country_id'];
        }

更多请查看extra attribute in dorpdown

【讨论】:

  • 我是否需要创建一个 CitiesHelper 文件,因为我不确定要在帮助程序中放入什么代码。
  • 您可以从模型或控制器创建下拉列表,您从名为$cities 变量的控制器中设置
  • 我认为 $cities 是在城市控制器索引中声明的,我尝试创建一个初始化函数并将您的代码放入其中,但没有成功。抱歉,我不太确定我在做什么,我对此还是很陌生。
  • 我将它放入了城市模型中,似乎没有任何错误我只需要更改 $items = $this->Cities->find('all')->all()- >toArray(); to $items = $this->find('all')->all()->toArray();但我不确定如何测试我需要对我的 add.ctp 进行更改?
  • 我还发现在 CutomersController 的 add 函数中设置了 $cities //$cities = $this->Customers->Cities->find('list', ['limit' => 200]); $items = $this->Cities->find->('all')->all()->toArray(); $城市 = []; foreach ($items as $key => $value) { $cities[$key]['v​​alue'] = $value['id']; $cities[$key]['text'] = $value['name']; $cities[$key]['class'] = $value['country_id'];但是当我添加代码时,它说调用成员函数 find() on boolean