Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP的正确方法(从设置的表中获取值)_Php_Mysql_Cakephp - Fatal编程技术网

CakePHP的正确方法(从设置的表中获取值)

CakePHP的正确方法(从设置的表中获取值),php,mysql,cakephp,Php,Mysql,Cakephp,我有一个名为Settings的表,其中包含字段key,value,在视图中,我必须使用CakePHP显示此表中的5项,在Controller的操作中,我将以下内容放在下面: $this->set('config', array( 'key1' => $this->Setting->getValue('key1'), 'key2' => $this->Setting->getValue('key2'), 'k

我有一个名为
Settings
的表,其中包含字段
key
value
,在视图中,我必须使用CakePHP显示此表中的5项,在
Controller
的操作中,我将以下内容放在下面:

$this->set('config', array(
        'key1' => $this->Setting->getValue('key1'),
        'key2' => $this->Setting->getValue('key2'),
        'key3' => $this->Setting->getValue('key3'),
        'key4' => $this->Setting->getValue('key4'),
        'key5' => $this->Setting->getValue('key5')

    ));
Setting->getValue()中我有这个:

function getValue($strKey){
        $value = $this->find('first', array(
            'conditions' => array(
                'key' => $strKey
             ),
            'fields' => array(
                'Setting.value'
            )
        ));
        return $value['Setting']['value'];
    }
还有一种更“简单的方法”可以做到这一点吗


谢谢。

我可能会这样做:

$this->find(
   'list', 
   array(
      'conditions' => array(
          'OR' => array(
                array('key' => 'key1'),
               /*etc*/
           ),
       ),
       'fields' => array('key', 'value)
    )
);
您应该得到一个如下所示的数组:

 array('key1' => 'val1', 'key2' => 'val2', /*etc*/);

你正在一个接一个地检索值,这不是正确的方法

通过生成值数组在单个查询中执行此操作:

$inArray = array('val1','val2','val3','val4','val5');

function getValue($inArray){
     $returnData = $this->find('list',array(
                       'conditions' => array('key'=>$inArray),
                       'fields' => array('key', 'value))
             );
     return $returnData;
      }

@kai给出的答案包含的或语句不是进行此类查询的最佳方式,仅供参考:

另一个问题,应该在模型中,还是应该创建行为?kai建议的代码应该放在控制器中,即:$config=$this->find(…,然后$this->set(compact('config'));我认为
$this->find..
应该放在模型中,控制器调用模型的函数。