Welcome Guest, Not a member yet? Register   Sign In
Query Builder: new ways of use the select function
#1

This thread is related to the closed issue #3764 on Github.

Right now, CI_DB_query_builder select() has the following documentation:

Quote:Parameters:
  • $select (string) – The SELECT portion of a query
  • $escape (bool) – Whether to escape values and identifiers

Returns:
  • CI_DB_query_builder instance (method chaining)

Return type:
  • CI_DB_query_builder

Adds a SELECT clause to a query.

The function accepts a string in the first parameter, however, you can also pass an array:

PHP Code:
// Connected to a MySQL database

$this->db->select('field1, field2');
echo 
$this->db->get_compiled_select();
// SELECT `field1`, `field2`

$this->db->select(array('field1''field2'));
echo 
$this->db->get_compiled_select();
// SELECT `field1`, `field2` 

My proposal is to all the following calls to the select function described bellow produces the same result (inspired by the behaviors of the where function):

PHP Code:
// One string parameter
$this->db->select('long_field_name AS foo, another_long_field_name AS bar');

// One array parameter
$this->db->select(array(
 
 'long_field_name AS foo',
 
 'another_long_field_name AS bar'
));

// An associative array to define alias
$this->db->select(array(
 
 'long_field_name'         => 'foo',
 
 'another_long_field_name' => 'bar',
 
 'field_with_no_alias'
));

// Method chaining
$this->db->select('long_field_name AS foo')
 
        ->select('another_long_field_name AS bar');

// Second parameter be the alias IF the first parameter is a string
$this->db->select('long_field_name''foo')
 
        ->select('another_long_field_name''bar');

// Escaping
$this->db->select('long_field_name AS foo, another_long_field_name AS bar'''FALSE);
$this->db->select('long_field_name''foo'FALSE);
$this->db->select(array(
 
 'long_field_name'         => 'foo',
 
 'another_long_field_name' => 'bar',
 
 'field_with_no_alias'
), ''FALSE); 
Reply
#2

(This post was last modified: 04-16-2015, 03:01 PM by Rufnex.)

Why use an array if you can do the same by strings and mulitple select methods?

Reply
#3

(This post was last modified: 04-16-2015, 01:20 AM by gadelat.)

Well one reason I can think of to implement this is clear distinction between tables and aliases, which could be easy work around the bugs #3719 and #2755 which would be otherwise maybe never fixed due to complications in current implementation. This one should be easy enough to implement.
Reply
#4

#3764 is rejected.

I told you to post on the forums in reference to any ideas that you might have in the future, not so you can repost this one - that's pointless.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB