Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kohana ORM question

Tags:

select

orm

kohana

i am using kohana ORM in order to get some results from the database. My problem is: even though i have consulted the documentation, i can't find a way to select only the column i am interested in. To be more explicit, i have:

$sale_stock = Model::factory('product_type')
->where('product_type_id','=', $id )
-> find_all();

var dumping it, it selects me all the "SELECT product_type.* from product_type where etc". But i want to select only the 'stock' field from the salestock table. doing find('stock') instead find_all() returns a weired object... Where am i wrong, and how can i actually select only the column 'stock' using kohana orm?

thank you!

like image 457
dana Avatar asked Feb 20 '11 09:02

dana


1 Answers

ORM methods find() and find_all() always select all table columns, so there is two ways to get specified fields:

  • Load full table rows and get columns from it:
$sale_stock = Model::factory('product_type')
   ->where('product_type_id','=', $id )
   -> find_all();
// get array of id=>stock values
$columns = $sale_stock->as_array('id', 'stock');
  • Create special method in model using Query Builder:
// model Model_Product_Type 
public function get_stocks($product_type_id) 
{    
   return DB::select(array('stock'))
      ->from($this->_table_name)
      ->where('product_type_id', '=', $product_type_id)
      ->execute($this->_db); 
}
like image 121
biakaveron Avatar answered Oct 20 '22 18:10

biakaveron