codeigniter-base-model icon indicating copy to clipboard operation
codeigniter-base-model copied to clipboard

Select Only One Field

Open hendrasan opened this issue 12 years ago • 3 comments
trafficstars

Is it possible to return only one field so that something like this works?

$this->tagline = $this->setting->get_by(array('name' => 'tagline'))->select('value');

hendrasan avatar Jun 11 '13 06:06 hendrasan

$this->tagline = $this->setting->get_by(array('name' => 'tagline'))->select('value')->value; ?

michail1982 avatar Jun 11 '13 07:06 michail1982

you can't do that, there will be an error.

Fatal error: Call to undefined method stdClass::select() 

I end up just creating one custom function to get the value.

public function get_value($name) {
  if (empty($name)) {
    return;
  }

  $r = $this->db->select('value')
            ->where('name', $name)
            ->get($this->_table);

  if (empty($r->row()->value)) {
    return;
  }

  return $r->row()->value;
}

This only works in a key-value pair table though. Although this won't be reusable in all cases, it would be nice if this is somehow built-in in the library though.

hendrasan avatar Jun 11 '13 07:06 hendrasan

Yeap, sorry, my mistake =) select() is not nessesary

michail1982 avatar Jun 11 '13 07:06 michail1982