Codeigniter查询结果返回带有setter的自定义结果对象

时间:2014-10-06 22:36:35

标签: php codeigniter oop codeigniter-2

CI custome对象的工作原理是什么?

根据CI文档您还可以将字符串传递给result(),该字符串表示要为每个结果对象实例化的类(注意:必须加载此类)

$query = $this->db->query("SELECT * FROM users;");

foreach ($query->result('User') as $row)
{
   echo $row->name; // call attributes
   echo $row->reverse_name(); // or methods defined on the 'User' class
}
}

这是一个非常好的功能,但Ci所做的是它会返回一个User个对象数组,并从行设置属性。

我有一个问题,我希望能够更好地控制在设置/获取之前要公开访问的属性和要修改的内容。

我该怎么做到这一点?我可以告诉CI将所有属性传递给构造函数,以便类可以填充自己的数据吗?

示例类用户

class User{
    private $data=array();
    protected $CI;
    //public $id,$name,$dob,$gender,$role,$username,$password,$salt,$picture,$lastactive;
    function __construct($data=null)
    {
        $this->data = $data; // i want to save data to a private var and allow attr. throu getters only
    }
    function set_password($p){
      $this->generateSalt();
      $this->data->password = $p.$this->data->salt;
    }
}

简而言之::

我想使用custom_result_object,但我不希望codeigniter为我填充类属性,而是希望类接收那些attrs并以他自己的方式填充它自己。

2 个答案:

答案 0 :(得分:4)

我在为自己寻找解决方案时找到了你的问题。

在文档中挖掘了一下后,我设法弄明白了:

class user_item {
  // you can declare all the attributes you want as private
  private $id,$name,$dob,$gender,$role,$username,$password,$salt,$picture,$lastactive;

  function __construct(){
    // you can use the constructor to format data as needed
    $this->username = strtouppper($this->username);  
  }

  public function set_password($p){
    $this->generateSalt();
    $this->password = $p.$this->salt;
  }

  public function get_password(){
    return $this->password;
  }
}

设置完成后,您可以从$this->db->result()

实例化此类
class User_model extends CI_Model {
   public function get_user($id){
     return $this->db->get_where('users', array('id' => $id), 1)->result('user_item');
   }
}

根据需要调用该类的任何公共方法或属性

class Users extends CI_Controller {
  function __construct(){
     $this->load->model('user');
  }
  public function profile($user_id){
    var $myUser = $this->user->get_user($user_id);
    $myUser->set_password('myPassword');
    echo $myUser->get_password();
  }
}

我已经简化了代码以使其更清晰,但你明白了。

答案 1 :(得分:0)

此示例控制器使用结果数组和对象

if ($this->session->userdata('id_jurusan') ==1) {
            $where=array('id_jurusan'=>$this->session->userdata('id_jurusan'));
            $value = $this->session->userdata('id_jurusan');
            $value2 = $this->session->userdata('username');
            $data['rule']=$this->guru_mod->get_where($where,'forward_changing')->result();
            $data['fuzzy']=$this->guru_mod->get_data_all('fuzzy')->result();
            $data['artikel']=$this->guru_mod->get_data_all('artikel')->result();
            $data['kondisi']=$this->guru_mod->get_where($where,'kondisi')->result();
            $data['artikel2'] = $this->guru_mod->get_data_all2('artikel','id_jurusan',$value);
            $data['riwayat_rule'] = $this->guru_mod->get_data_all2('forward_changing','username',$value2);
            $data['kondisi_rule'] = $this->guru_mod->get_data_all2('kondisi','id_jurusan',$value);
            $this->load->view('guru/daftar_rule',$data);
        }