Welcome Guest, Not a member yet? Register   Sign In
possible to rename db-table in active record ?
#1

[eluser]Jan_1[/eluser]
We can write
Code:
$this->db->select('table.colum as something');
but,
can we also rename a database table?
e.g. when we do something very long or complicated with JOIN
#2

[eluser]Jason Stanley[/eluser]
Have you tried it? I thought you just did this. I usually write joins using the query function instead of Active Record so I am not really sure.

Code:
$this->db->from('content AS c');
#3

[eluser]Jan_1[/eluser]
Yes, that works. But how could I rename to use it in JOIN.
$this->db->join('table as t',...
does not work
#4

[eluser]meigwilym[/eluser]
Try the user guide first

http://ellislab.com/codeigniter/user-gui...name_table

Mei
#5

[eluser]Jan_1[/eluser]
Thanks, but that would rename the table in the database. I just want to use another name in the request with different JOIN-lines. I dont't want to rename it in the database.
#6

[eluser]louisl[/eluser]
To be honest I've never tried it, I've never been a fan of shortening all the table names. Active record makes the code layout pretty nice and readable so mine typically look like this:-

Code:
public function getUsersInGroup($group_id) {

  $this->db->select('users.username');
  $this->db->select('user_profiles.FirstName');
  $this->db->select('user_profiles.Surname');
  $this->db->from('users');
  $this->db->join('user_profiles', 'users.id = user_profiles.user_id', 'left');
  $this->db->join('user_groups_lookup', 'users.id = user_groups_lookup.user_id', 'left');
  $this->db->where('user_groups_lookup.group_id', $group_id);
    
  $query = $this->db->get();
  
  return $query;

}
#7

[eluser]CroNiX[/eluser]
Not everything needs to go in Active Record (It can't handle EVERY situation, obviously). I have lots of complex queries that I just execute using db::query(). You just have to be sure to manually escape input, like you would anywhere else. I'd rather spend time quickly writing real queries than try to figure out how to make them work just for Active Record. I'd say I use AR about 90% of the time for the quick, easy stuff that doesn't take fudging around.

Active Record really is also for portability. So you can run the same (AR) queries on MSSQL as you do MySQL or Oracle, even though SQL varies quite a bit between each one. Yes, its possible your application might need to run on a different platform, but in 20+ years of coding, I've yet to have that happen. 99.9999% I usually know up front which db is needed, and the application won't be moved to something else.

@Jan The terminology is "table alias", not "rename a database table". They are really different things. One is temporary and only exists in memory, the other is a permanent change to the table structure.




Theme © iAndrew 2016 - Forum software by © MyBB