Welcome Guest, Not a member yet? Register   Sign In
Useful get method for MY_Model Class
#1

[eluser]Nezar[/eluser]
1. SQL :

Code:
CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `body` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ;

--
-- Dumping data for table `posts`
--

INSERT INTO `posts` (`id`, `title`, `body`) VALUES
(1, 'Post1', 'Body1'),
(2, 'Post2', 'Body2'),
(3, 'Post3', 'Body3'),
(4, 'Post4', 'Body4'),
(5, 'Post5', 'Body5'),
(6, 'Post6', 'Body6'),
(7, 'Post7', 'Body7'),
(8, 'Post8', 'Body8'),
(9, 'Post9', 'Body9'),
(10, 'Post10', 'Body10');

2. Create a base Model class in ( application/core/my_model.php ) :

Code:
<?php

class MY_Model extends CI_Model
{
    
    public $table ;
    public $id;
    
    function __construct() {
        parent::__construct();
        require_once(APPPATH . 'config/database.php');
    }
    
    
    function get($ids = null, $fields = null, $values = null)
    {
        
        global $db;
        
        if($ids != null)
        {
            $this->db->or_where_in($this->primary_key, $ids);  
        }
        
        if($fields != null and $values != null){
            

            $index = 0 ;
            
            if(!is_array($fields))
            {
                if(is_array($values))
                {
                    
                    $values_list = explode(",", $values[0]);
                    
                    if(is_array($values_list))
                    {
                        foreach($values_list as $value)
                        {
                            $this->db->or_where_in($fields, $value);
                        }
                    }
                    
                }else{
                    $this->db->or_where_in($fields, $values);
                }
                
                
            }else{
                
                foreach($fields as $field)
                {
                    $values_list = explode(",", $values[$index]);
                    
                    if(is_array($values_list))
                    {
                        foreach($values_list as $value)
                        {
                            $this->db->or_where_in($field, $value);
                        }
                    }
                    
                    $index++;
                }
                
            }
            
            
        }

        return $this->db->get($db['default']['dbprefix'] . $this->table)->result();
        
    }
    
}

3. Create a Post model ( application/models/post.php ) :

Code:
<?php

class Post extends MY_Model
{
    
    function __construct() {
        parent::__construct();
        $this->primary_key = 'id';
        $this->table = 'posts';
    }
    
}

4. Create Posts controller ( application/controllers/posts.php ) :

Code:
<?php

class Posts extends CI_Controller
{
    
    function index()
    {
        $this->load->database();
        $this->load->model('Post');
        echo "<pre>";
        
        
        // all data
        //print_r($this->Post->get());        
        
        // id = 1
        //print_r($this->Post->get(1));        
        
        // id = 1,2,3
        //print_r($this->Post->get(array(1,2,3)));        
        
        // id = 1,2
        // title = Post3
        // body = Body5
        //print_r($this->Post->get(array(1,2), array('title','body'), array('Post3','Body5')));
        
        //id = 1,2
        //title = Post3,Post4,Post5
        // body = Body6,Body7,Body10
        //print_r($this->Post->get(array(1,2), array('title','body'), array('Post3,Post4,Post5','Body6,Body7,Body10')));
        
        
        //title = Post3,Post4,Post5
        // body = Body6,Body7,Body10
        print_r($this->Post->get(null, array('title','body'), array('Post3,Post4,Post5','Body6,Body7,Body10')));

        $this->debug($this->db->last_query());
    }
    
    function debug($str)
    {
        echo "<br><h1>$str</h1>";
    }
}

:lol:
#2

[eluser]Jan_1[/eluser]
Could you specify your question?
#3

[eluser]Nezar[/eluser]
I don't have a question !!!

it's just some thing like this :

Creating all the get methods




Theme © iAndrew 2016 - Forum software by © MyBB