我正在尝试学习 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);
对于初学者来说,关于这个问题,最重要的是查看这个链接。