PHP: Instantiating new instances of a model based on database information for controller/view use
I'm wondering whether the following code sample is good form, or it's better to work directly with the query results from the active record class. I just started using CodeIgniter and was planning on using the model's public properties in the view (sent through the controller).


    <?php
    class Classified_model extends CI_Model {
        public $year;
        public $make;
        public $model;
        public $car_type;
        public $price;
        public $description;
        public $picture;
        public $mileage;
    
        function __construct()
        {
    	// Call the Model constructor
    	parent::__construct();
        }
    
        public function getByCount($count)
        {
    	$this->db->select('*')->from('classified')->join('car','car.id=classified.id_car')->join('car_model', 'car_model.id=car.id_model')->join('car_make', 'car_model.id_make=car_make.id');
    	$query = $this->db->get();
    	return $query->result();
        }
    
        public function getClassifiedSet($query) 
        {
    	$objectSet = array();
    	foreach($query as $object) {
    	    $classified = new $this;
    	    foreach($object as $attribute=>$value){
    		if (self::has_attribute($attribute, $object)) 
    		    $classified->$attribute = $value;
    	    }
    	    array_push($objectSet, $classified);
    	}                              
    	//var_dump($objectSet);
    	return $objectSet;
        }
    
        private function has_attribute($attribute, $object) 
        {
    	$object_vars = get_object_vars($object);
    	return array_key_exists($attribute, $object_vars);
        }
    }
    //End of file


Controller would probably use this:

	$this->load->model('Classified_model','',TRUE);
	$classifiedsQuery = $this->Classified_model->getByCount(0);
	$classifieds = $this->Classified_model->getClassifiedSet($classifiedsQuery);
        $data['classifieds'] = $classifieds;

And I can then work with the objects in the view. I'm new to this, so I'm wondering whether this is ideal, or if I should be doing something else.
PHP: Instantiating new instances of a model based on database information for controller/view use
===

I'm wondering whether the following code sample is good form, or it's better to work directly with the query results from the active record class. I just started using CodeIgniter and was planning on using the model's public properties in the view (sent through the controller).


    <?php
    class Classified_model extends CI_Model {
        public $year;
        public $make;
        public $model;
        public $car_type;
        public $price;
        public $description;
        public $picture;
        public $mileage;
    
        function __construct()
        {
    	// Call the Model constructor
    	parent::__construct();
        }
    
        public function getByCount($count)
        {
    	$this->db->select('*')->from('classified')->join('car','car.id=classified.id_car')->join('car_model', 'car_model.id=car.id_model')->join('car_make', 'car_model.id_make=car_make.id');
    	$query = $this->db->get();
    	return $query->result();
        }
    
        public function getClassifiedSet($query) 
        {
    	$objectSet = array();
    	foreach($query as $object) {
    	    $classified = new $this;
    	    foreach($object as $attribute=>$value){
    		if (self::has_attribute($attribute, $object)) 
    		    $classified->$attribute = $value;
    	    }
    	    array_push($objectSet, $classified);
    	}                              
    	//var_dump($objectSet);
    	return $objectSet;
        }
    
        private function has_attribute($attribute, $object) 
        {
    	$object_vars = get_object_vars($object);
    	return array_key_exists($attribute, $object_vars);
        }
    }
    //End of file


Controller would probably use this:

	$this->load->model('Classified_model','',TRUE);
	$classifiedsQuery = $this->Classified_model->getByCount(0);
	$classifieds = $this->Classified_model->getClassifiedSet($classifiedsQuery);
        $data['classifieds'] = $classifieds;

And I can then work with the objects in the view. I'm new to this, so I'm wondering whether this is ideal, or if I should be doing something else.