CakePhp-下拉列表-为每个操作调用关联查找('list')?

CakePhp-下拉列表-为每个操作调用关联查找('list')?,php,cakephp,model,Php,Cakephp,Model,假设我有一个公司模型和公司控制器。公司表中的外键包括时区id、车站id、州id、城市id 我在CompanyController中执行了添加、编辑和查看公司的操作。为了添加和编辑,我需要所有外键关联(时区、州、城市、电台)的下拉列表。因此,在这些行动中,我发现自己为每一个行动都写了很多以下内容: $this->set('cities', $this->Station->City->find('list')); $this->set('states', $this-&

假设我有一个公司模型和公司控制器。公司表中的外键包括时区id、车站id、州id、城市id

我在CompanyController中执行了添加、编辑和查看公司的操作。为了添加和编辑,我需要所有外键关联(时区、州、城市、电台)的下拉列表。因此,在这些行动中,我发现自己为每一个行动都写了很多以下内容:

$this->set('cities', $this->Station->City->find('list'));
$this->set('states', $this->Station->State->find('list'));
等等


似乎有很多代码重复。有更好的方法吗?

向您的公司控制器添加预查看回调,如afterFilter:

function afterFilter() {
    // conditional ensures only actions that need the vars will receive them
    if (in_array($this->action, array('index', 'view', 'edit'))) {
        $this->set('cities', $this->Station->City->find('list'));
        $this->set('states', $this->Station->State->find('list'));
    }
}
private function _populate_dropdowns() {
    $this->set('cities', $this->Station->City->find('list'));
    $this->set('states', $this->Station->State->find('list'));
}

每次公司控制器操作后都将调用afterFilter。

向公司控制器添加预查看回调,如afterFilter:

function afterFilter() {
    // conditional ensures only actions that need the vars will receive them
    if (in_array($this->action, array('index', 'view', 'edit'))) {
        $this->set('cities', $this->Station->City->find('list'));
        $this->set('states', $this->Station->State->find('list'));
    }
}
private function _populate_dropdowns() {
    $this->set('cities', $this->Station->City->find('list'));
    $this->set('states', $this->Station->State->find('list'));
}

afterFilter将在每个公司控制器操作后调用。

过滤器很好,正如webbiedave的回答所示,但如果最终使用大量条件逻辑,则可能会变得复杂

另一个选择是,您可以在控制器中定义不作为操作/视图的方法:毕竟它是一个普通的PHP对象。更好的是,如果使用PHP5,您可以将这些方法声明为私有,以确保它们不会被误认为是操作

例如,您可以在控制器中定义以下内容:

function afterFilter() {
    // conditional ensures only actions that need the vars will receive them
    if (in_array($this->action, array('index', 'view', 'edit'))) {
        $this->set('cities', $this->Station->City->find('list'));
        $this->set('states', $this->Station->State->find('list'));
    }
}
private function _populate_dropdowns() {
    $this->set('cities', $this->Station->City->find('list'));
    $this->set('states', $this->Station->State->find('list'));
}
然后在需要它的任何操作开始时调用它:

public function add() {
    $this->_populate_dropdowns();
    // ... 
    // add code
}

public function edit() {
    $this->_populate_dropdowns();
    // ... 
    // edit code
}

正如webbiedave的回答一样,过滤器是好的,但如果你最终使用了大量的条件逻辑,它可能会变得复杂

另一个选择是,您可以在控制器中定义不作为操作/视图的方法:毕竟它是一个普通的PHP对象。更好的是,如果使用PHP5,您可以将这些方法声明为私有,以确保它们不会被误认为是操作

例如,您可以在控制器中定义以下内容:

function afterFilter() {
    // conditional ensures only actions that need the vars will receive them
    if (in_array($this->action, array('index', 'view', 'edit'))) {
        $this->set('cities', $this->Station->City->find('list'));
        $this->set('states', $this->Station->State->find('list'));
    }
}
private function _populate_dropdowns() {
    $this->set('cities', $this->Station->City->find('list'));
    $this->set('states', $this->Station->State->find('list'));
}
然后在需要它的任何操作开始时调用它:

public function add() {
    $this->_populate_dropdowns();
    // ... 
    // add code
}

public function edit() {
    $this->_populate_dropdowns();
    // ... 
    // edit code
}

事实上,我只是试了一下,没用。但它确实与beforeFilter一起工作。这就是你的意思,还是我遗漏了什么?我写了afterFilter,因为这是我当时想到的,但我真的想尝试任何预查看回调。我的主要目的是让您知道,有一些强大的回调可以帮助解决此类问题。我很高兴过滤器为您工作!afterFilter是在所有操作完成后调用的,基本上就在_destruct之前。beforeRender是您想要的@dogmatic69:据我所知,beforeRender是在加载视图类并导入变量后调用的,因此$this->set无法工作,尽管有其他方法可以访问它们。你错了。您可以在视图和元素中调用$this->set。实际上,您只是尝试了一下,但没有成功。但它确实与beforeFilter一起工作。这就是你的意思,还是我遗漏了什么?我写了afterFilter,因为这是我当时想到的,但我真的想尝试任何预查看回调。我的主要目的是让您知道,有一些强大的回调可以帮助解决此类问题。我很高兴过滤器为您工作!afterFilter是在所有操作完成后调用的,基本上就在_destruct之前。beforeRender是您想要的@dogmatic69:据我所知,beforeRender是在加载视图类并导入变量后调用的,因此$this->set无法工作,尽管有其他方法可以访问它们。你错了。您可以在视图和元素中调用$this->set是的,我考虑过这一点。然后考虑将这两个想法结合起来:创建私有方法,但从beforefilter调用它们,这也很有效:简单地说就是代码需要的频率。beforeFilter处理每个请求,所以如果您认为大多数请求都需要它,那么它似乎是合适的。然后考虑将这两个想法结合起来:创建私有方法,但从beforefilter调用它们,这也很有效:简单地说就是代码需要的频率。beforeFilter对每个请求都运行,所以如果您认为大多数请求都需要它,那么它似乎是合适的。