我正在編寫一個基於現有數據庫創建模型生成器的規範。我創建了下面的虛擬數據庫來說明規格:如何處理與我的PHP模型的n-m關係?
這裏是展示各種表應該如何操作的代碼片段:
<?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();
由於我不是非常技術性的我真的不知道以上是否可行,如果是的話,生成相應模型的工作量是多少。因此,我想問一下以下問題:
問題1:從編碼風格看,您是否看到任何不能(合理)實現的東西?
Q2:它會增加任何價值有對portfolio_has_customer
表中的一個額外的步驟或者是通過customer
對象像上面確定訪問amount_of_contract
財產?
Q3:是否有用於處理的portfolio_has_customer
的n-m
關係,這將是比上述一個實施較不復雜的方法嗎?
類名不以$開頭(除非您有變量,稱爲'$ Companies'和'$ Portofolio')。 –