Select with alias in model in CI 4 - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: Select with alias in model in CI 4 (/showthread.php?tid=78741) |
Select with alias in model in CI 4 - damiano - 03-04-2021 Hello, I have this strange problem with a query on my CI4 project. There is a page that is really slow, so I've decided to print the sql query to see what's wrong with that and I've noticed this: Code: SELECT `mov`.* The table name was repeated and this causes a huge slowing of the table. The code I've used in my model is: Code: $this->select('mov.*'); If I don't use alias query works good, but it's a little bit annoying to repeat the entire table name every time in the query: Code: $this->select('Movements.*'); What am I missing? Thank you RE: Select with alias in model in CI 4 - iRedds - 03-05-2021 When you get an instance of the BaseBuilder class, the database table specified in the model is added to it. This is how the BaseBuilder class is designed. That is, before you start using QueryBuilder, already called PHP Code: ->from($this->table); There is nothing you can do about it. But you can reset the "from" section before using QueryBuilder. PHP Code: ->from([], true); RE: Select with alias in model in CI 4 - damiano - 03-05-2021 Ok I understand. So there is no way to use aliases in this case? I think will be great to have a property in model for alias. Something like: protected $table = 'Movements'; protected $tableAlias = 'mov'; protected $primaryKey = 'id'; RE: Select with alias in model in CI 4 - iRedds - 03-05-2021 PHP Code: ->from('table as alias', true) But does it make sense? You will still be manually specifying the alias in QB methods. |