Welcome Guest, Not a member yet? Register   Sign In
Iterate a model's attributes within the model?
#11

[eluser]Dam1an[/eluser]
You can read about magic methods here
The idea behind the __get/__set methods, is it provides a nice way for manipulating private variables, so you could do something like
Code:
private $attributes = array();

public function __set($name, $value) {
    switch($name) {
        case 'jobref': $this->attributes['jobref'] = $value; break;
        case 'title': $this->attributes['title'] = $value; break;
        etc
    }
}

You could then just loop through the array as before in the get method
#12

[eluser]Colin Williams[/eluser]
Does get_object_vars($this) not do it for you? I've never tried it that way..
#13

[eluser]Michael McCabe[/eluser]
Well I still don't know which method to choose but I now have lots more to choose from! :ahhh:

I had heard of magic methods but didn't realise they were called that. Think I'm going to go with that method.

Thanks again. The high praise for this community is definitely deserved. :-)
#14

[eluser]Michael McCabe[/eluser]
[quote author="Colin Williams" date="1241915541"]Does get_object_vars($this) not do it for you? I've never tried it that way..[/quote]

I got all excited then and tried this but it results in exactly the same result as my first post. EVERYTHING codeigniter ends up in the variable as I presume this refers to not just the class your in but every class it is extended from. Or something like that, I'm only slightly proficient in php and a noob to frameworks and codeigniter.

I just tried this -
Code:
<?php

class Test_model extends Model {

    public $va1;
    public $var2;
    public $var3;

    function Test_model()
    {
        parent::Model();
    }
    
    function iterate() {
    
    $attr = get_object_vars($this);
    
    print_r($attr);
    
        foreach($attr as $key)
        
            print_r($key);    
    
        }

    
}

// Controller

<?php

class Test extends Controller {

    function Test()
    {
        parent::Controller();
        $this->load->model('test_model');
        
    }
    
    function index()
    {
            
        $this->test_model->iterate();
    
    }
    
    
    
}

It resulted in a HUGE page of objects being printed to the page and timed out before it had even finished.
#15

[eluser]Dam1an[/eluser]
I'm assuming you still get everything, because it gets all the vars for this class, and the class this class extends, and so on, till you get pretty much everything lol
#16

[eluser]Dam1an[/eluser]
I'm assuming you still get everything, because it gets all the vars for this class, and the class this class extends, and so on, till you get pretty much everything lol

Ooops, double post (It just reloaded the page the first time without having submitted, which happens a lot, but this is the first time its double posted)
#17

[eluser]peterphp[/eluser]
I found this code on another forum, it seems to solve the problem by excluding "_parent_name":

Code:
function get()
{
    foreach(get_class_vars(get_class($this)) as $key=>$value)
    {
        if($key != "_parent_name")
        {
            $val = $this->$key;
            if($val) $this->db->where($key, $val);
        }
    }
    // some more code here...
    }
}

Found this snipplet here.

Seems to me like the 'right' way to do it :-)




Theme © iAndrew 2016 - Forum software by © MyBB