How to handle nm relationships with my PHP models?

I am writing a specification for someone to create a model generator based on an existing database. To illustrate the specification, I created the following dummy database:

enter image description here

And here is a code snippet showing how to manipulate various tables:

<?php // DISPLAYING VALUES $companies = new $Companies(); $companies->get_all(); foreach ($companies as $company) { echo "Company name: " . $company->name; // 1-n relationship FROM company table so company_type is 1 object echo "Type of company: " . $company->company_type->name; // 1-n relationship TO company table so portfolios is a collection of 0 // or more objects foreach ($company->portfolios as $portfolio) { foreach ($portfolio->customers as $customer) { echo $customer->amount_of_contract; echo $customer->name; } } } // ADDING A PORTFOLIO $companies = new $Companies; $company = $companies->get_by_name('mycompany'); $portfolio = new $Portofolio; $portfolio->company_id = $company->id; $portfolio->year = '2012'; $portfolio->create(); 

Since Iโ€™m not very technical, I really donโ€™t know if this is possible, and if so, how much work is involved in creating the appropriate models. Accordingly, I would like to ask the following questions:

Q1: From the coding style, do you see everything that would not be (reasonably) feasible?

Q2: will any value add an extra step for the portfolio_has_customer table or will it access the amount_of_contract property through the customer object as described above?

Q3: Is there an alternative for handling the nm portfolio_has_customer relationship that would be less difficult to implement than the one described above?

+4
source share
1 answer

Q1: The syntax resembles the ORM used by the kohana framework , for me it looks realistic

Q2: This use of the pivot table can be seen sometimes, the only thing that looks strange to me is how you access this property (I often saw additional relationships between portfolio and portfolio_has_customer portfolio_), although do not take it for granted

Q3: no, I do not know anything less complicated

0
source

Source: https://habr.com/ru/post/1413314/


All Articles