Welcome Guest, Not a member yet? Register   Sign In
Is OK to put chaining Query Builder methods inside Controller?
#1

Hi.
I was wondering if this is recommended, chain a lot of query builder methods inside Controllers, instead Model.
Example. I have this model method that returns some rows based in that conditions:

PHP Code:
public function getTotais()
    {

        $this->selectSum('valor');
        $this->join('categorias''categorias.id = lancamentos.categorias_id');
        $this->where(
            [
                'lancamentos.usuarios_id' => session()->id_usuario,
                'lancamentos.consolidado'  => true
            
]
        );
        $result $this->first();       

        
return !is_null($result['valor']) ? $result['valor'] : 0.00;
    

But, for some controllers methods that call that model method, I need to pass more parameters to filter more precisely.
So, I do like this:

PHP Code:
$receitasMesAtual $this->lancamentoModel->where('tipo''r')->where('YEAR(data)'$ano)->where('MONTH(data)'$mes)->getTotais(); 

So, I have been concerned about the fact that I am putting too much "query builder methods" inside controller, instead it, create a specific method inside model.

But, at same time, if I create a specific method inside model, I will have to manage too much parameters inside that method, that's why I am chaining query builder methods calling from inside controller.

Is ok to chaining query builder methods inside controllers? Is this won't hurt MVC pattern?

Thank you
Reply
#2

It's breaking the MVC pattern. You should put your queries-functions inside the model and pass the rest with function parameters or using set functions.

e.g. setYear() and setDate()
and use getYearMonthData()

or getYearMonthData($year, $month)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB