如何删除行或筛选记录集或集合

时间:2012-02-09 02:48:34

标签: php lithium

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

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

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

3 个答案:

答案 0 :(得分:2)

模型使用的Collection对象是从lithium\util\Collection扩展而来的,$newQuestions = $oldQuestions->find(function($question) { if (your new criteria) { return true; } return false; }); 提供了一种过滤现有集合的方法,并根据用户提供的闭包返回一个新集合。

Collection

只需确定您要应用的条件并在闭包中执行过滤。一旦运行,您应该有一个新的{{1}}对象,只有匹配的记录。

答案 1 :(得分: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

答案 2 :(得分:1)

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

实施例

假设您的问题表包含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>";
}

然后,当您提交每个表单时,除了更新mysql answers表之外,您还需要更新_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.
    }
}