Relationship between PHP Models [closed]

0

Hello

I am developing a miniframework, for my projects, and have to create more knowledge about object orientation in PHP

I have created a MODEL where it does a lot of everything, and my classes extend this MODEL .... So far everything is working, insert, update, delete, findAll, findAllByPk, FindAllByAttributes etc.

But what I wanted to do now are the relationships between classes. Ex: I have a Product and a Category class, where every product has a category.

At the time of showing this in the view, I wanted to call it like this:

  

$ Product-> Category-> attribute_category

But until now I'm unsuccessful, I thank anyone who can help me (give me a try).

    
asked by anonymous 06.02.2017 / 21:43

1 answer

1

What I would do if I were you would be this:

class Produto
{

  protected $objCategoria; //faça get's end seter's
  protected $idCategoria;  //faça get's end seter's
  ...

  protected function getCategoriaProduto($idCategoria){
     $this->$categoria = new Categoria($idCategoria);
  }

  public function getProduto($idProduto){
    //processamento para pegar produto
    $this->getCategoriaProduto($this->$idCategoria);  
  }
}

What I did was create an objCategory property, which stores an object of the category class (which has to be defined by you), this when you do a search on the product through a getProducts method, for example. This method creates an instance of the Category class, which feeds the objCategory property.

To best fit what you've already done, you can feed the $ objCategory with a findAllByPk of the Category class into your Product Class's findAllByPk method.

    
07.02.2017 / 00:16