CodeIgniter:set_value()或post() - 什么更快,什么是最好的做法来存储数据到数据库 [英] CodeIgniter: set_value() or post() - what is faster and what is the best practice to store data into database
问题描述
Codeigniter
与 form_helper
和 form_validation
来做一些表单处理。在控制器
中,表格已成功验证。 现在我们需要使用模型
类将这些数据放入数据库中。假设我们的表单假设我们的表单有几个输入元素(例如> 20) )。
问题
以下哪些代码片段效率更高? 这两个片段显然都在表单提交数据的控制器方法中。
代码片段1
if($ this-> form_validation-> run())
{
//验证成功,现在收集变量中的值以将其传递给模型。
$ form_data ['field1'] = $ this-> form_validation-> set_value('field1');
$ form_data ['field2'] = $ this-> form_validation-> set_value('field2');
//等于
$ form_data ['fieldN'] = $ this-> form_validation-> set_value('fieldN');
//现在将这些数据放到数据库中。
$ this->对应模型 - > write_to_db($ form_data);
}
代码段2
if($ this-> form_validation-> run())
{
//验证成功,现在收集将变量中的值传递给模型。
$ form_data ['field1'] = $ this-> input-> post('field1');
$ form_data ['field2'] = $ this-> input-> post('field2');
//等于
$ form_data ['fieldN'] = $ this-> input-> post('fieldN');
//现在将这些数据放到数据库中。
$ this->对应模型 - > write_to_db($ form_data);
}
所以基本上我问的是:最好是获取一些任意表单元素的发布数据? $ this-> input-> post
或 $ this-> form_validation-> set_value()
?
$ b
PS:如果我们看看 set_value()
和<$ c $ ($请参阅下面),显然 set_value()
会更快,因为 > post()
遍历整个 $ _ POST
。因此,从某种意义上说,它也是关于什么是最佳做法?
$ b
Form_validation.php,set_value()方法
public function set_value($ field ='',$ default ='')
{
if(!isset($ this - > _field_data [$ field]))
{
return $ default;
}
//如果数据是数组,则一次输出一个数据。
//例如:form_input('name []',set_value('name []');
if(is_array($ this-> _field_data [$ field] ['postdata']))
{
return array_shift($ this-> _field_data [$ field] ['postdata']);
}
return $ this-> _field_data [ $ field $ [$ field] ['postdata'];
}
Input.php, post()方法
function post($ index = NULL,$ xss_clean = FALSE)
{
//检查是否提供了字段
if($ index === NULL AND!empty($ _ POST))
{
$ post = array();
//遍历整个_POST数组并返回
foreach(array_keys($ _ POST),作为$ key)
{
$ post [$ key] = $ this-> _fetch_from_array($ _ POST,$ key,$ xss_clean);
}
return $ post;
}
return $ this-> _fetch_from_array( $ _POST,$ index,$ xss_clean);
}
如果规则已经在输入上运行,那么这两个函数都会返回修改后的值。你想从表单中读取一个post值, USE $ this-> input-> post()
。
set_value()
用于重新填充表单验证失败。
没有额外的过滤,所以它更快,但我更喜欢你应该使用 $ this-> input-> post()
作为安全。
Background
Using Codeigniter
with form_helper
and form_validation
to do some form processing. Form has been validated successfully in the controller
.
Now we need to put this data into the database using the model
class.
Assumptions
Lets assume our form has several input elements (e.g. >20).
Question
Which of the following code snippets will be more efficient? Both snippets are obviously inside the controller method to which the form submits data.
Code Snippet 1
if ($this->form_validation->run())
{
// validation successful, now collect the values in a variable to pass it to the model.
$form_data['field1'] = $this->form_validation->set_value('field1');
$form_data['field2'] = $this->form_validation->set_value('field2');
// AND SO ON
$form_data['fieldN'] = $this->form_validation->set_value('fieldN');
// Now put this data into database.
$this->corresponding_model->write_to_db($form_data);
}
Code Snippet 2
if ($this->form_validation->run())
{
// validation successful, now collect the values in a variable to pass it to the model.
$form_data['field1'] = $this->input->post('field1');
$form_data['field2'] = $this->input->post('field2');
// AND SO ON
$form_data['fieldN'] = $this->input->post('fieldN');
// Now put this data into database.
$this->corresponding_model->write_to_db($form_data);
}
So essentially what I am asking is: What is better to get the post data for some arbitrary form element? $this->input->post
or $this->form_validation->set_value()
?
PS: If we look the the set_value()
and post()
functions in the code (please see below), obviously set_value()
is going to be faster as post()
loops through the entire $_POST
. So in a sense it is also about what is the best practice?
Form_validation.php, set_value() method
public function set_value($field = '', $default = '')
{
if ( ! isset($this->_field_data[$field]))
{
return $default;
}
// If the data is an array output them one at a time.
// E.g: form_input('name[]', set_value('name[]');
if (is_array($this->_field_data[$field]['postdata']))
{
return array_shift($this->_field_data[$field]['postdata']);
}
return $this->_field_data[$field]['postdata'];
}
Input.php, post() method
function post($index = NULL, $xss_clean = FALSE)
{
// Check if a field has been provided
if ($index === NULL AND ! empty($_POST))
{
$post = array();
// Loop through the full _POST array and return it
foreach (array_keys($_POST) as $key)
{
$post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
}
return $post;
}
return $this->_fetch_from_array($_POST, $index, $xss_clean);
}
Both functions will return the modified value if rules have been run on the input.
When you want to read a post value from form, USE $this->input->post()
.
set_value()
is used to re-populate a form has failed validation.
There is no additional filtering on it, so it faster but I prefer you should use $this->input->post()
for the secure.
这篇关于CodeIgniter:set_value()或post() - 什么更快,什么是最好的做法来存储数据到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!