首页 > 解决方案 > 我可以对多个表使用单个 CodeIgniter 模型吗?

问题描述

我有一个包含多个表的体育赛事数据库。一些表格保存结果的记录,有一个用户配置文件表格,以及一些其他表格。

我的模型如下:

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 模型以及所有结果表。结果表的示例:21KM 赛事的 30 至 39 岁男性前十名和 21KM 赛事的 40 至 49 岁男性前十名。我目前拥有使用相同模型方法的所有结果表,例如:

public function displayrecords($table) {
    $query=$this->db->query("select * from " . $table);
    return $query->result();
}

它将结果表作为参数,并显示结果。我会为每个结果表创建模型,但我事先不知道该表将具有什么名称、它将具有的列等。每个结果表都是由管理员通过网页创建的,并根据需要添加到数据库中. 每个表都会对它们执行相同的操作,删除行,更新行。这就是为什么我认为最好有一个可以与所有表格一起使用的模型。

我不确定我的处理方式是否正确,因为我似乎已经遇到了一些问题。例如,当尝试从表中删除记录时,我可以获得需要删除的行的 id,但我不确定如何获取数据库表名,因为 deleterecords 方法需要 2 个参数,$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 变量在 url 中。

http://localhost/results/pages/deletedata?id=1&tablename=rs_toptenfemale

标签: phpcodeigniter

解决方案


I'm not sure about your case, but you can use multiple functions in your model and you can handle all the table in a single model (but I think it's not advisable and good practice )

You can use dynamic functions like this to avoid too much of functions in model

<?php 

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Common_model extends CI_Model {

  
public function insertContent($data,$table){
    $this->db->insert($table,$data);    
    if($this->db->affected_rows() > 0)
    {
        return TRUE; 
    }
    else{
        return FALSE;
    }
}

public function updateContent($data,$table,$whereField,$value){

    $this->db->where($whereField,$value);
    $update=$this->db->update($table, $data);
    if ($update > 0) {
        return TRUE;
    } else {
        return FALSE;
    }
}

public function login_check($data,$table)
{
    $this->db->select('*');
    $this->db->where($data);
    $query=$this->db->get($table);
    if($query->num_rows()>0)
    {
        return TRUE;         
    } else {
        return FALSE;
    }
}

public function retriveTableDetails($table)
{

    $this->db->select('*');
    $this->db->from($table);
    $query=$this->db->get();
    if($query->num_rows()>0)
    {
        return $query->result();
    } else {
        return FALSE;
    }
}

public function retriveTableDetailsById($table,$id)
{

    $this->db->select('*');
    $this->db->from($table);
    $this->db->where('id',$id);
    $query=$this->db->get();
    if($query->num_rows()>0)
    {
        return $query->result();
    } else {
        return FALSE;
    }
}


推荐阅读