我正在为某人编写基于现有数据库创建模型生成器的规范。我创建了以下虚拟数据库来说明规范:
这是一个代码片段,显示了应该如何操作各种表:
<?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();
由于我不是很技术,我真的不知道上述是否可行,如果可以,生成相应模型所需的工作量是多少。因此,我想问以下问题:
Q1:从编码风格来看,您是否看到任何(合理)无法实现的东西?
portfolio_has_customer
Q2:它会增加任何价值来为表格增加一个额外的步骤,还是通过上面的对象访问amount_of_contract
属性?customer
Q3:有没有一种替代方法来处理这种n-m
关系,portfolio_has_customer
实现起来比上面描述的更简单?