Welcome Guest, Not a member yet? Register   Sign In
Retrieving database data in a Model
#1

[eluser]Unknown[/eluser]
Hi codeigniters,

I am working on a project where i am for the first time using models with CI. I try to make the models as dynamic as i can so i can re-use them.

In MyModel the saveObject function is working great. I just send the whole object to my db connection and it saves/updates perfectly. But retrieving is not working for me.

If i use the getEntrie() method i get al clean object of stdClass with only the data from the DB table. What i want is the whole constructed model class with this data without having to declare al the fields hardcoded e.g. ( $this->field1 = $dbobject['fied1'] ).

Could someone please help me?


Code:
<?php
    

    // Model class
    class MyModel extends Model {
    
        ## Table name of corresponding model ##
        private $db_table = "tablename";
        
        ## Model vars ######
            var $id;
            var $field1;
                                                                                                
            var $field2;
            var $field3;

        ####################


        // Constructor method
        function MyModel(){

            // Construcct Controller
            parent::Model();    
            
        }    
    

        // Count entries
        function countAll(){
        
            return $this->db->count_all($this->db_table);
        
        }
        
        
        // Get one object
        function getEntrie( $id ){
        
            $object = $this->db->get_where( $this->db_table, array('id'=> $id ) );
            $object = $object->result();
            
             return $object[0];
        }

        // Get ammount of entries
        static function getEntries( $ammount = 1) {
            
            $objects = $this->db->get($this->db_table, $ammount);
            return $objects->result();
        
        }
        
        // Get all entries
        function getAll(){
        
            $objects = $this->db->get($this->db_table);
            return $objects->result();
        
        }
        
        
        //Save Object
        function saveObject(){
        
            // Check if object is new or not
            if ( isset( $this->id ) ) {
            
                //Update existing
                $this->update();
            }else{
                //Add new
                $this->insert();
            }
        
        }
        
                
        //Insert object in DB
        private function insert(){
        
            $this->db->insert( $this->db_table, $this);
        
        }
        
        //Update object in DB
        private function update(){

            $this->db->insert( $this->db_table, $this, array( 'id' => $this->id) );        
        }

        // Delete object from DB
        function delete(){
        
            $this->db->delete( $this->db_table, array ( 'id' => $this->id ) );
        }        
                
        
    }
?>
#2

[eluser]JuanitoDelCielo[/eluser]
Code:
function getEntrie( $id ){
        
            $object = $this->db->get_where( $this->db_table, array('id'=> $id ) );
            $object = $object->result();
            
             return $object[0];

             // Better

             $object = $this->db->get_where( $this->db_table, array('id'=> $id ) );
             return $object->row();

}
And check this

$obj = getEntrie(1);

foreach ( $obj as $key => $value ) {
    $this->$key = $value;
}


but is worth because you already have it :S
#3

[eluser]Unknown[/eluser]
Sometimes in development you think. "How did i not come up with that". Thanks for the reminder!
Code:
<?php

function getEntrie( $id ){
        
            $object = $this->db->get_where( $this->db_table, array('id'=> $id ) );
            $object = $object->result();
            
            foreach ( $object[0] as $key => $value ){
            
            
                $this->$key = $value;
            }
            
        }


?>




Theme © iAndrew 2016 - Forum software by © MyBB