【问题标题】:cakephp find('list') - problem usingcakephp find('list') - 使用问题
【发布时间】:2010-06-09 11:58:40
【问题描述】:

我正在尝试获取一个数组来填充 Counties 选择。如果我使用 find('all') 我可以获取数据,但是这个数组需要展平才能在 $form->input($counties) 的视图中使用。

如果我使用 find('list'),我似乎无法获得正确的数组 - 这是简单的县名数组。

我试过的是这样的:

        $ops=array(
        'conditions' => array(
            'display' => '!=0',
            'TO_DAYS(event_finish) >= TO_DAYS(NOW())'
        ),
        'fields' => 'DISTINCT venue_county',
        'order' => 'venue_county DESC'
    );
    $this->set('counties', $this->Event->find('list',$ops));

但它生成的 SQL 是:

SELECT Event.id, DISTINCT Event.venue_county FROM events AS Event WHERE display = 1 AND TO_DAYS(event_finish) >= TO_DAYS(NOW()) ORDER BY @ 987654329@DESC

这会产生错误,因为它首先在查询中插入 Event.id 字段 - 这是不想要的并导致错误。

在我的数据库中,我有一个包含场地地址的事件表,我真的不想为地址创建另一个表。

对于 find('list') 调用,我应该使用哪些选项?

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    “列表”需要 2 个字段 - 一个用于 ID,另一个用于文本标签。如果您只使用一个,它将获取 IF 字段的当前模型 ID。它应该是这样的:

    $ops=array(
        'conditions' => array(
            'display !=' => 0,
            'TO_DAYS(event_finish) >= TO_DAYS(NOW())'
        ),
        'fields' => array('venue_county', 'venue_county'),
        'order' => 'venue_county DESC',
        'group'=>'venue_county'
    
    );
    $this->set('counties', $this->Event->find('list',$ops));
    

    请记住,像 ">"、"!="、"LIKE"、"IN" 等条件是从左侧放置的(如我的示例中的 display 字段)。

    基本上您可以更改字段,但第一个字段需要是 ID 字段,下一个字段需要是 TEXT 字段

    【讨论】:

    • 如果我想放一个不是来自数据库的选项,例如“请选择”作为默认选项怎么办?
    【解决方案2】:

    尝试指定列表的字段

    <?php
    $this->set('counties', $this->Event->find('list',array(
        'conditions' => array(
            'NOT' => array('display' => 0),
            'TO_DAYS(event_finish) >= TO_DAYS(NOW())'
        ),
        'fields' => array('Event.venue_county','Event.venue_county'),
        'order' => 'venue_county DESC'
    )));
    ?>
    

    虽然我可能会建议您在本地执行日期条件(假设 [event_finish] 字段是数据或日期时间字段)。当您使用它时,您不妨将显示字段条件简化为 IS vs. 和 ISN'T(我建议将该字段设为 tinyint(1) 类型,因此它基本上是真/假)。

    <?php
    $this->set('counties', $this->Event->find('list',array(
        'conditions' => array(
            'display' => 1,
            'Event.event_finish >' => date('Y-m-d 00:00:00'),
        ),
        'fields' => array('Event.venue_county','Event.venue_county'),
        'order' => 'venue_county DESC'
    )));
    ?>
    

    【讨论】:

    • 感谢您的示例-感谢您提供的额外提示-我会尽我所能:-)
    • 我注意到 SQL 自动包含 GROUP BY 而无需指定此条件(我在原始代码中添加了 DISTINCT)。这很好,但有点可怕,因为我不知道它在什么情况下添加它。你知道它使用了什么规则或添加了哪些其他情况吗?
    • 评论:book.cakephp.org/view/1017/Retrieving-Your-Data 至于group by 自动-我没注意到,可能是因为这两个字段是一样的,所以它不必返回更多的行它需要,但我没有真正研究它,也没有发现 find(list) 有任何问题......对我来说效果很好。您可以将 "recursive' =&gt; -1 添加到查找选项中,以防您要确保排除其他链接模型。
    • 感谢递归提示 - 是的,我对两个字段的想法相同。
    【解决方案3】:

    希望这会奏效。

    $ops=array(
        'conditions' => array(
            'display' => '!=0',
            'TO_DAYS(event_finish) >= TO_DAYS(NOW())'
        ),
        'fields' => array('id','venue_county'),
        'order' => 'venue_county DESC'
    );
    $this->set('counties', $this->Event->find('list',$ops));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多