我正在尝试学习CakePHP,我有一个简单的问题,
我有
$this->set('settings', $this->Setting->find('all'));
在控制器中。我想将其集中到:
find all
设置where
类型等于General
我该怎么做?
您可以使用 findAllBy 函数。
findAllBy(string $value, array $fields, array $order, int $limit, int $page, int $recursive)
您可以在 CakePHP Cookbook 中阅读此内容
所以你的示例将如下所示:
$this->Setting->findAllByType('General');
$this->Setting->findAllByType($variable); //If you want to use a variable
或
$this->Setting->find('all', array('conditions' => array('type' => $type)));
或(不推荐)
$this->Setting->query("SELECT * FROM settings WHERE type = 'General';");
简单地说find
您仍然可以通过为模型对象提供参数来使用它的方法。
$settings = $this->Setting->find('all', array('conditions' => array('type' => 'General')));
$this->set('settings', $settings);
对于初学者来说,关于这个问题,最重要的是查看此链接。