我有一个多表的体育赛事数据库,其中一些表保存着比赛结果记录,还有一个用户资料表和几个杂项表。
我的模型如下:
我想要弄清楚的是,是否建议使用单个CodeIgniter模型,包含所有结果表格。结果表格的示例:男性30至39岁21KM赛事前十名和男性40至49岁21KM赛事前十名。我目前使用相同的模型方法来处理所有的结果表格:
我的模型如下:
class Results extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function displayrecords($table) {
$query=$this->db->query("select * from " . $table);
return $query->result();
}
public function getdisplayname($table) {
$query=$this->db->query("select * from " . $table);
return $query->result();
}
public function updatetable() {
}
public function deleterecords($table, $id) {
$this->db->query("delete * from " . $table . " where id='" . $id . "'");
}
}
索引视图的控制器:
public function index() {
// Load the models
$this->load->model('Results');
$this->load->model('Info');
$this->load->model('TableNames');
// Create the array to store the data for the index page
$data['results'] = array();
// Get a list of all the tables in the event database
$tables = $this->db->list_tables();
$data['tables'] = array();
// Loop through the list of table names and extract the results tables
foreach($tables as $table) {
if (strpos($table, "rs_") === 0) {
array_push($data['tables'], $table);
$temp = $this->Results->displayrecords($table);
array_push($data['results'], $temp);
$data['headnames'] = $this->db->list_fields($table);
}
}
// Get the event info data and the list of Human Readable Table Names
$data['tablenames'] = $this->TableNames->getnames();
$data['eventinfo'] = $this->Info->getinfo();
// Render the templates
$this->parser->parse('templates/index_header', $data);
$this->parser->parse('pages/index', $data);
$this->parser->parse('templates/footer', $data);
}
我想要弄清楚的是,是否建议使用单个CodeIgniter模型,包含所有结果表格。结果表格的示例:男性30至39岁21KM赛事前十名和男性40至49岁21KM赛事前十名。我目前使用相同的模型方法来处理所有的结果表格:
public function displayrecords($table) {
$query=$this->db->query("select * from " . $table);
return $query->result();
}
该方法需要一张结果表作为参数,并显示结果。我会为每个结果表创建模型,但我不知道表的名称、列等信息。每个结果表都由管理员通过网页创建,并根据需要添加到数据库中。所有表都将执行相同的操作,如删除行、更新行。这就是为什么我认为最好有一个模型可以处理所有表。
但我不确定我的做法是否正确,因为我似乎已经遇到了一些问题。例如,要删除表中的记录时,我可以获取需要删除的行的ID,但我不知道如何获取数据库表名,因为deleterecords方法需要两个参数:$id和$tablename才能正确删除记录。该模型方法如下:
public function deleterecords($tablename, $id) {
$this->db->query("delete from " . $tablename . " where id='" . $id . "'");
}
然后在视图中,我会显示每个结果表:
foreach ($table as $row) {
echo "<tr id='result_" . $row->id . "'>";
echo "<td>" . $row->Place . "</td>";
echo "<td class='bibcell'>" . $row->BibNumber . "<a class='editbut' href=''><img class='bibnum' src='" . base_url() . "assets/images/editicon.png' width='80' style='padding-left: 20px;'></a><a class='delbut' href='deletedata?id=" . $row->id . "'><img src='" . base_url() . "assets/images/deleteicon.png' width='75' style='padding-left: 5px;'></a></td>";
echo "<td>" . $row->Name . "</td>";
echo "<td>" . $row->Age . "</td>";
echo "<td>" . $row->Club . "</td>";
echo "<td>" . $row->Time . "</td>";
echo "<td><select class='user-options'><option value='pending' style='background: red;'>Pending</option><option value='checked' style='background: green;'>Checked</option></select></td>";
echo "</tr>";
}
使用隐藏输入框是获取表名的方法吗?还是CodeIgniter中可能有处理单个模型使用多个表的功能?
编辑:
我已经设法弄清楚了:
echo "<td class='bibcell'>" . $row->BibNumber . "<a class='editbut' href=''><img class='bibnum' src='" . base_url() . "assets/images/editicon.png' width='80' style='padding-left: 20px;'></a><a class='delbut' href='deletedata?id=" . $row->id . "&tablename=" . $tables[$i] . "'><img src='" . base_url() . "assets/images/deleteicon.png' width='75' style='padding-left: 5px;'></a></td>";
我希望将id和tablename传递给Model方法以进行删除,但我得到了未定义的变量tablename。不过我可以看到url中有tablename变量。
http://localhost/results/pages/deletedata?id=1&tablename=rs_toptenfemale