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

Proposal: add get_first() method?

Open sprijk opened this issue 12 years ago • 4 comments

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!

sprijk avatar Oct 11 '12 12:10 sprijk

Couldn't you just use $this->$model->limit(1)->get_all()?

andrewryno avatar Oct 11 '12 17:10 andrewryno

I would expect an array of a single objects here, am I wrong?

sprijk avatar Oct 12 '12 06:10 sprijk

You can use pseudo where $office = $this->office->get_by('officeCode >','0'); // officeCode is autoincrement

michail1982 avatar Nov 19 '12 15:11 michail1982

@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.

ivantcholakov avatar Jan 04 '14 07:01 ivantcholakov