Relationships between PHP Models

Asked

Viewed 73 times

0

Hello!

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

I created a MODEL where it is a big part of everything, and my classes extend this MODEL.... So far virtually everything is working, update, delete, update, findAll, findAllByPk, Findallbyattributes etc.

But what I wanted to do now is class relations. Ex:I have a class Product and a Category, where every product has a category.

At the time of showing this in the view, I wanted to call as follows:

$Product->Category->attribute_category

but so far I am unsuccessful, I thank anyone who can help me (gives me a north) of how to do this.

  • Bro I think you were not very happy to finalize the explanation of your text, post an example of what you already have, and what you want, is better to visualize your ideal and help in a solution

1 answer

1

What I would do if I were you would be the following:

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), when you do a product search through a getProduct method, for example. Such a method creates an instance of the Category class, which feeds the objCategory property.

to better fit in with what you have already done, you can feed $objCategoria with a findAllByPk of the Category class, within your findAllByPk method of the Product Class.

Browser other questions tagged

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