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

5
我有一个多表的体育赛事数据库,其中一些表保存着比赛结果记录,还有一个用户资料表和几个杂项表。
我的模型如下:
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

你在将近一年前找到解决方案了吗? - relo80
2个回答

0

我不确定你的情况,但是你可以在你的模型中使用多个函数,并且你可以在单个模型中处理所有的表格(但我认为这不是一个明智和良好的做法)

你可以像这样使用动态函数来避免在模型中使用过多的函数

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


0
Please use below code for handle operations in common model.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users_model extends CI_Model 
{
  function __construct()
  {
    parent::__construct();
    $this->load->database();
  }

  //get data with whare condition OR without where condition EX: get_where() OR get()
  function select_where($table,$where='',$limit='',$offset='')
  {
    if ($where!=="")
    {
      return $this->db->get_where($table,$where,$limit,$offset)->result();
    }
    else
    {
      return $this->db->get($table,$limit,$offset)->result(); 
    }
  }    

  //count data with whare condition OR without where condition  
  function select_count($table,$where='')
  {
    if ($where!=="")
    {
      $this->db->select('*')->from($table)->where($where); 
      $q = $this->db->get(); 
      return $q->num_rows();
    }
    else
    {
      $this->db->select('*')->from($table); 
      $q = $this->db->get(); 
      return $q->num_rows();
    }
  }

  //Insert Data
  public function insert_data($table,$data)
  {
    return $this->db->insert($table,$data);
  }
  
  //update data with whare condition OR without where condition
  public function update_data($table,$datak,$edit)
  {
    $field=$this->db->field_data($table);
    $tid=$field[0]->name;
    $data=array(
      $tid=>$edit); 
    $this->db->where($data); 
    $res=$this->db->update($table,$datak);
    if($res)
    {
      return true;
    }
    else
    {
      return false;
    }
  }  
  public function delete_where($table,$data)
  {
    if($this->db->delete($table,$data))
    {
      return true;
    }
    else
    {
      return false;
    }
  } 



}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接