codeigniter-base-model
codeigniter-base-model copied to clipboard
Proposal: add get_first() method?
First of all: thanks for your awesome model!!
If I need a first record of an ordered set I use:
$this->$model->order_by($order_bys)
->$model->get_by(array());
Because get() expects an primary key, and get_by expects a where clause.
Should there be an easier way? Or am I overlooking something?
Thanks!
Couldn't you just use $this->$model->limit(1)->get_all()?
I would expect an array of a single objects here, am I wrong?
You can use pseudo where $office = $this->office->get_by('officeCode >','0'); // officeCode is autoincrement
@sprijk Let it be just "first()", I've implemented this some time ago. https://github.com/ivantcholakov/codeigniter-base-model
An example:
$data = $this->pages
->select('id, name, description')
->where('id', $page_id)
->where('lang', $this->lang_id)
->where('published', 1)
// limit(1) clause is added automatically.
->first(); // This method returns the record directly.
In addition I've got find() (as an alias method):
$items = $this->news
->select('id, name, shortdescription, dateadd')
->where('lang', $this->lang_id)
->order_by('dateadd', 'desc')
->find(); // This method returns an array of records.