1

我想知道是否有办法删除行或过滤记录集或集合。

例如,如果我有两张表:一张用于提问,一张用于选择答案。这些问题属于不同的形式。问题1-10属于形式a,11-20属于形式b。根据前面问题的答案,某些问题可能会出现也可能不会出现,而稍后的某些答案可能会出现也可能不会出现。我不想经常访问数据库,而是想将属于每个表单的记录集或问题集合缓存到内存中,并在每个会话中过滤掉内存中的问题集。

这样每个用户只会在会话开始时访问数据库一次,而不是每次单击下一步时。

4

3 回答 3

2

模型使用的Collection对象是从它扩展而来的lithium\util\Collection,它提供了一种过滤现有集合并根据用户提供的闭包返回新集合的方法。

$newQuestions = $oldQuestions->find(function($question) {
    if (your new criteria) { 
        return true;
    }
    return false;
});

只需确定您希望应用的标准并在闭包中执行过滤。一旦它运行,你应该有一个Collection只包含匹配记录的新对象。

于 2012-02-09T15:56:19.570 回答
2

从数据库中获取 Recordset 或 Collection 后,您可以对其执行几个过滤器。有关lithium\util\Collection更多信息,请参阅。

一个例子是

$questions = Questions::all();
$form_questions = $questions->find(function($question) {
  if($query->form == 'b') {
    return true;
  }
  return false;
}), true);

要在页面请求之间保持这些问题持续存在,请查看lithium\storage\Session.

于 2012-02-09T16:04:26.367 回答
1

除非您的流量非常大,否则简单的“每页一次”数据库调用不太可能对您的服务器造成很大压力,但如果您确实想这样做,最简单的方法是缓存此信息在用户登录时的 PHP$_SESSION超全局中。假设您已将 PHP 设置为使用文件系统存储(尽管即使您使用数据库会话存储,它对性能的影响也很小),您的问题将以超快的速度存储-to-access 已经预先构建为每个特定用户唯一的文件。加载脚本后,会话文件会自动读入内存,您可以从那里访问任何信息。

例子:

假设您的问题表有列question_number并且question_text您的答案表有列question_numberanswer_text

<?php
//on login:
//first get the answer array, so we can use it in the logic below:
$query = mysql_query('SELECT * FROM `questions` WHERE [criteria]',[connection identifier]) or die(mysql_error());
if (!mysql_num_rows($query)){
   die("No questions!");
}
$answer_array = array();
//create a keyed array that you can access by question number
while($row=mysql_fetch_array($query)){
   $answer_array[$row['question_number']] = $row['answer_text'];
}


//now get the questions and put everything into the session variable 
$query = mysql_query('SELECT * FROM `questions` WHERE [criteria]',[connection identifier]) or die(mysql_error());
if (!mysql_num_rows($query)){
   die("No questions!");
}
//loop through the results and generate session arrays we can work with later
while($row = mysql_fetch_array($query)){
   //assign the question to the correct form:
   if ($row['question_number']<=10){
      $session_key = 'form_a';
   } elseif($row['question_number']<=20){
      $session_key = 'form_b';
   } elseif($row['question_number']<=30){
      $session_key = 'form_c';
   } else {
      $session_key = 'form_d';
   }

   //if the session variable does exist yet, create it:
   if (!isset($_SESSION[$session_key])){
      $_SESSION[$session_key] = array();
   }
   //get the existing answer if it exists, otherwise leave the answer blank:
   $my_answer = "";
   if(isset($answer_array[$row['question_number']])){
      $my_answer = $answer_array[$row['question_number']];
   }
   //add this question array as a child array element in the session array, keyed by the question number
   $_SESSION[$session_key][$row['question_number']] = array(
      'question' => $row,
      'answer'   => $my_answer
   );
}

现在,如果我们正在加载表单 B,例如,我们可以从会话数组中读取它$_SESSION['form_b']并根据前面问题的答案执行我们想要的任何逻辑切换:

$html = "";
foreach($_SESSION['form_b'] as $question_number => $data){
  //perform any logic, for instance, if question 2 from form a is equal to '5', don't show question 3 on form B:
  switch($question_number){
      case '3': if ($_SESSION['form_a']['2']['answer']=='5'){ continue 2; }; break;
  }
  //add the question to the form, and populate the answer if they already answered it earlier:
  $html .= "<label>".$data['question']."<input type='text' value=\"".$data['answer']."\" name='question_".$question_number."' /></label>";
}

然后,当您提交每个表单时,除了更新 mysqlanswers表之外,您还需要更新 _SESSION 数组。例如,如果您通过 POST 提交表单 B:

$form = 'form_b';
foreach($_POST as $key=>$value){
    if (substr($key,0,strlen('question_')!='question_'){
        continue;                       
    }
    $number = str_replace('question_','',$key); //this will give us the question number
    $saved = add_answer($number,$value); //call the function to insert the new answer into the database (this is a dummy function, and please make sure to escape your variables
    if ($saved){//assuming it saved:
        $_SESSION[$form ][$number]['answer']=$value; //now we've updated the session array as well.
    }
}
于 2012-02-09T03:02:42.997 回答