我可以为多个表使用单个CodeIgniter模型吗?

tyu7yeag  于 2023-08-01  发布在  其他
关注(0)|答案(2)|浏览(86)

我有一个体育赛事的数据库,它有多个表。其中一些表保存“结果”的记录,还有一个“用户配置文件”表和一些“其他”表。
我的模型如下:

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模型来处理所有的Results表。“结果”(Results)表格的示例:前十名男性30至39岁的21公里事件和前十名男性40至49岁也为21公里事件。我目前所有的结果表都使用相同的模型方法,例如:

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


它以结果表作为参数,并显示结果。我会为每个结果表创建模型,但我不知道之前手什么名字的表,它将有列等。每个结果表都由管理员通过网页创建,并根据需要添加到数据库中。每个表都将执行相同的操作,删除行,更新行。这就是为什么我认为最好有一个模型,可以与所有的表。
我不确定我这样做是否正确,因为我似乎已经遇到了一些问题。例如,当试图从表中删除一条记录时,我可以获得需要删除的行的id,但我不确定如何获得数据库表名,因为deletecorections方法需要两个参数,$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中是否有可能处理使用单个Model的多个表的特性?
编辑:
我设法弄明白了这一点:

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。我可以看到tablename变量是在url虽然。

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

z9smfwbn

z9smfwbn1#

我不确定你的情况,但是你可以在你的模型中使用多个函数,你可以在一个模型中处理所有的表(但是我认为这是不可取的,也是很好的实践)
您可以使用像这样的动态函数来避免模型中的函数过多

<?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;
    }
}

字符串

31moq8wy

31moq8wy2#

Please use below code for handle operations in common model.

个字符

相关问题