TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
[3.3 ORM] Limiting Results
  • Is there a way to make the ORM only return the active records all the time. Add a sort of where('active','=',1) that would always work on the Model?

  • class ORM extends Kohana_ORM {
        public function find()
        {
            $this->where('active', '=', 1);
            return parent::find();
        }
        
        public function find_all()
        {
            $this->where('active', '=', 1);
            return parent::find_all();
        }
    }
    
  • To also take into account the possibility of table joins, would that be:

    $this->where($this->_object_name.'.active', '=', 1);
    

    ?

  • @samwilson object_name? maybe tablename?

  • No, in ORM these days the table name is aliased to object_name in _load_result():

    $this->_db_builder->from(array($this->_table_name, $this->_object_name));
    
  • looked carefully code:

    ->select(array(DB::expr('COUNT('.$this->_db->quote_column($this->_object_name.'.'.$this->_primary_key).')'), 'records_found'))

    best way:

    $this->_db->quote_column($this->_object_name.'.active')
  • (That's from ORM::count_all() isn't it?)

    But that's only needed if you're using it within DB::expr(), where it won't be quoted properly.

    In where(), it will be quoted correctly, so we can just pass in $this->_object_name.'.active', can't we?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion