• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterate a model's attributes within the model?

#1
[eluser]Michael McCabe[/eluser]
Good morning/afternoon to everyone.

I just wrote a big explanation of why I wanted to do this but decided it would be better to just ask if it's possible so...

I would like to iterate through my models attributes in a function of that object. For example —
Code:
class Jobs_model extends Model {

    public $jobref;
    public $clientid;
    public $title;
    
    function Jobs_model()
    {
        parent::Model();
    }

    function get() {
        
        foreach($this as $key => $val)
            $this->db->where($key, $val);    
        }
        
}

This works, if I were to print each iteration's value I get the first three variables, however I then (perhaps obviously to you) get every class which codeigniter has.

Is there any way to iterate through an objects attributes from within the object itself?

Thanks for any help you can provide.

#2
[eluser]Dam1an[/eluser]
One way to do it, would be to get the fields for that given table, and then loop through that, but using the class variable for the where condition
Something like
Code:
function get() {
    $fields = $this->db->list_fields('jobs');
    
    foreach($fields as $field) {
        $this->db->where($field, $this->$field);
    }
    
    return $this->db->get('jobs');
}

Is that the sort of thing you're after, or am I going down the wrong route completey?

#3
[eluser]Michael McCabe[/eluser]
I initially thought you'd nailed it but what happens if I want all jobs for a particular client?

If I have set the $clientid to '32' but haven't set the $jobref or $title what would the sql be?

"SELECT * FROM jobs WHERE clientid = '32' AND jobref = NULL AND title = NULL"

I expect this wouldn't work. I suppose I could have an if in the foreach –

Code:
public $jobref = FALSE;
public $clientid = FALSE;
public $title = FALSE;

function get() {
    $fields = $this->db->list_fields('jobs');
    
    foreach($fields as $field) {
        if($this->$field) {
             $this->db->where($field, $this->$field);
       }
    }
    
    return $this->db->get('jobs');
}

That seems crazy enough to work d'ya think?

#4
[eluser]Dam1an[/eluser]
I knew there was something I'd missed, yeah that if should do the trick
(I just quickly done that without bothering to look in my old code where I'd done that)

#5
[eluser]depthcharge[/eluser]
Would putting the models attributes in an array help, looping the attributes array rather than the object or fields?

Code:
$attrib['jobref'] = 'ref4354';
$attrib['clientid'] = 'microsoft';
$attrib['title'] = 'uninstall';

Code:
class Jobs_model extends Model {

    public $attrib;
    
    function Jobs_model()
    {
        parent::Model();
    }

    function get() {
        
        foreach($this->attrib as $key => $val)
            if ($val) $this->db->where($key, $val);    
        }
        
}

#6
[eluser]Dam1an[/eluser]
That would work as well (and save a DB call to list the table fields), but I personally don't like it, as it makes the syntax 'ugly'
I'm assuming the fields are set to public so he can do
Code:
$this->job_model->jobref = 'my_ref';
instead of
$this->job_model->props['jobref'] = 'my_ref';

But thats just personal preference

(you could do the array method, but use the syntax of the first method if you use the magic set method)

#7
[eluser]depthcharge[/eluser]
Yes, maybe using setters/getters would make it less ugly

Code:
function set_jobref($val)
{
     $this->attrib('jobref') = $val;
}

function set_client_id($val)
...
....
....


$this->set_jobref('ref4545');

#8
[eluser]Michael McCabe[/eluser]
Thanks for the timely and invaluable advice so far!

[quote author="lee74" date="1241903872"]Would putting the models attributes in an array help, looping the attributes array rather than the object or fields?

Code:
$attrib['jobref'] = 'ref4354';
$attrib['clientid'] = 'microsoft';
$attrib['title'] = 'uninstall';

Code:
class Jobs_model extends Model {

    public $attrib;
    
    function Jobs_model()
    {
        parent::Model();
    }

    function get() {
        
        foreach($this->attrib as $key => $val)
            if ($val) $this->db->where($key, $val);    
        }
        
}
[/quote]

That's pretty much how I had written the model originally but like Dam1an I found it a bit ugly. Worked perfectly well though.

[quote author="Dam1an" date="1241904652"]
(you could do the array method, but use the syntax of the first method if you use the magic set method)[/quote]

I've never heard of the magic set method what is that?.

#9
[eluser]Michael McCabe[/eluser]
[quote author="lee74" date="1241905005"]Yes, maybe using setters/getters would make it less ugly

Code:
function set_jobref($val)
{
     $this->attrib('jobref') = $val;
}

function set_client_id($val)
...
....
....


$this->set_jobref('ref4545');
[/quote]

I was writing my reply when you posted this. This may be a good way to do it. I think I'm getting too hung up on doing it the "right" way.

My app works I just want to make it as portable and easy to maintain as is possible. There is no time restraint so I'm not pressured into saying "That'll do" which may end up being a bad thing!!

#10
[eluser]depthcharge[/eluser]
Quote:I think I’m getting too hung up on doing it the “right” way.

Something I spend way too much time doing. :roll:


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.