Data aggregation in PHP and MVC development

Asked

Viewed 224 times

1

Good morning!

I am developing a web system and I am having doubts about the organization as a MVC structure. There is a register of companies that already have class/model/control etc. My doubt refers to the addresses of a company (which is an aggregation of the register of companies). I will need to create a model for it... how to better allocate the loading of this model (I have an autoloading for the main models, but not for the aggregation).

  • Your doubt is based on the best load method for this aggregation or the construction of this aggregation?

  • Hello, Guilherme. It is based on better load method (since it does not "use" autoloading); the relationship/aggregation in DB is already ok. My question is related in the "coupling": best practices to include and coupling with the controller (in this case, from the company).

1 answer

0


Good as you said is a Aggregation that is they have the same hierarchical level, being one independent of the other, but still related. To define the best load method you will have to check your usage frequency criteria, I believe that if you constantly use data from this aggregation, it is better to keep it started already in the constructor:

require_once CONTROLLERS.'EnderecoController.php';

class Empresa {
    function __construct(){

        $this->endereco = new EnderecoController();
    }
}

Here the controller of Endereco is always active together with the company, ie it will always load the two files.

Now if you only use in some specific cases, be better perform a call download:

Class App{
    function __get($var){
        $controller = sprintf('%sController', ucword($var));
        if(!isset($this->{$var}) && file_exists(CONTROLLERS."{$controller}.php")){
            $this->{$var} = new $controller;
        }

        if(!isset($this->{$var})){
            echo "Variable {$var} not found!";
        }

        return $this->{$var};
    }
}

Class Empresa extends App{

    function AddEnderecoEmpresa(){
        $this->endereco->save($cdEmpresa);
    }

}

Here he will carry Endereco only when you need to use.

  • 1

    Opa, Guilherme! Aggregation yes! And your answer came against what you imagined! And, continuing, the first option will be the most appropriate because there will always be the link! Vlw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.