[英]How to add a fuction to Repair - update a cell in database table in specific id in model php file
I make a remake of an extension so I stuck in the last work that I must to do.我重新制作了一个扩展,所以我坚持了我必须做的最后一项工作。 I created a twig for admin which displays a list that take data from a table in the database.
我为管理员创建了一个 twig,它显示了一个从数据库中的表中获取数据的列表。 In each row of the list I created a button that I want to fix specific cell of the table.
在列表的每一行中,我创建了一个按钮,我想修复表格的特定单元格。 So each line has its own id.
所以每一行都有自己的id。
See the image from page list:查看页面列表中的图像:
I made the controller file but I stuck in model file that must do the rest work.我制作了 controller 文件,但我卡在了 model 文件中,该文件必须执行 rest 工作。
In the following code I have to change something so that by pressing the repair button it reads the specific id.在下面的代码中,我必须更改某些内容,以便通过按下修复按钮读取特定的 ID。
public function repairSaveCarts($link_id=" ") {
$this->db->query("SELECT * FROM " . DB_PREFIX . "save_cart WHERE link_id = '" . (int)$link_id . "'");
$db_id = $this->db->getLastId();
$end_url = base64_encode(serialize($db_id));
$url = $this->config->get('config_url').$end_url;
$this->db->query("UPDATE " . DB_PREFIX . "save_cart SET shorturl = '" . $this->db->escape($url) . "' WHERE link_id = '" . (int)$db_id . "'");
}
I thing that the change must be done in $db_id = $this->db->getLastId();
我认为必须在
$db_id = $this->db->getLastId();
中进行更改line but I'm not sure.行,但我不确定。 Someone that can help please.
有人可以帮忙请。
I make it work.我让它工作。 I had a small syntax error in
request->get | post
我在
request->get | post
中有一个小的语法错误。 request->get | post
at controller and for this reason the model didn't get the correct id from button.在 controller
request->get | post
,因此 model 没有从按钮获得正确的 ID。
public function repairSaveCarts($link_id) {
$end_url = base64_encode(serialize((int)$link_id));
$url = $this->config->get('config_url').$end_url;
$this->db->query("UPDATE " . DB_PREFIX . "save_cart SET shorturl = '" . $this->db->escape($url) . "', date_added = NOW() WHERE link_id = '" . (int)$link_id . "'");
return $query->row;
}
One thing I notice with serialize
is that when the model got the id from the controller it received it as a spring
value and not as an int
value.我注意到
serialize
的一件事是,当 model 从 controller 获得 id 时,它作为spring
值而不是int
值接收它。 The result was to give as example for结果是举个例子
id 10 ---> serialize value= s:2:"10";
To fix this i add in model the (int)
value after serialize
and now give me the correct results为了解决这个问题,我在 model 中添加
serialize
后的(int)
值,现在给我正确的结果
id 10 ---> serialize value= i:10;
. .
The only problem now is that when the module save to database from frond-end page give me the full URL site:现在唯一的问题是,当模块从前端页面保存到数据库时,请给我完整的 URL 站点:
https://www.example.com/aToxMDs=
but when in admin side the functionRepair save to database give me only:但是当在管理员方面,functionRepair 保存到数据库只给我:
aToxMDs=
The line $url = $this->config->get('config_url').$end_url;
行
$url = $this->config->get('config_url').$end_url;
seems that doesn't work.似乎这行不通。
What i must edit to add the store url before the $end_url
???我必须编辑什么才能在
$end_url
???
Here's a revised version of the function for your model class:这是 model class 的 function 的修订版:
public function repairSaveCarts($link_id, $base) {
$end_url = base64_encode(serialize($link_id));
$url = $base.$end_url;
$this->db->query("UPDATE " . DB_PREFIX . "save_cart SET shorturl = '" . $this->db->escape($url) . "' WHERE link_id = '" . (int)$link_id . "'");
}
You should also cater for HTTPS not just HTTP, so add the following to the controller to get the site Url and pass that value to the 'repair' function. You should also cater for HTTPS not just HTTP, so add the following to the controller to get the site Url and pass that value to the 'repair' function. This should be done just before the existing call to the model function:
这应该在对 model function 的现有调用之前完成:
// check if the request was made over HTTPS or not
if ($this->request->server['HTTPS']) {
$server = $this->config->get('config_ssl');
} else {
$server = $this->config->get('config_url');
}
$this->whateveryourmodelnameis->repairSaveCarts($link_id, $server);
And finally i make it.最后我成功了。 I replace this:
我替换这个:
public function repairSaveCarts($link_id, $base) {
$end_url = base64_encode(serialize($link_id));
$url = $base.$end_url;
$this->db->query("UPDATE " . DB_PREFIX . "save_cart SET shorturl = '" . $this->db->escape($url) . "' WHERE link_id = '" . (int)$link_id . "'");
}
with this on model file:在 model 文件上有这个:
public function repairSaveCarts($link_id) {
$end_url = base64_encode(serialize((int)$link_id));
if ($this->request->server['HTTPS']) {
$url = HTTPS_CATALOG . $end_url;
} else {
$url = HTTP_CATALOG . $end_url;
}
$this->db->query("UPDATE " . DB_PREFIX . "save_cart SET shorturl = '" . $this->db->escape($url) . "', date_added = NOW() WHERE link_id = '" . (int)$link_id . "'");
}
Thank you Daniel for your help.谢谢丹尼尔的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.