Skip to content

Instantly share code, notes, and snippets.

@howtomakeaturn
Last active May 17, 2016 11:49
Show Gist options
  • Save howtomakeaturn/fdf9258c2b2b66280a71f7e857ef4270 to your computer and use it in GitHub Desktop.
Save howtomakeaturn/fdf9258c2b2b66280a71f7e857ef4270 to your computer and use it in GitHub Desktop.
manual modeling
<?php
namespace Dxchemistry\TraditionalChinese;
class Category
{
private $id;
private $name;
private $subcategories;
function __construct($id, $name, $subcategories)
{
$this->id = $id;
$this->name = $name;
$this->subcategories = $subcategories;
}
function getName()
{
return $this->name;
}
function getSubcategories()
{
return $this->subcategories;
}
}
<?php
namespace Dxchemistry\TraditionalChinese;
class CategoryRepository
{
private $map = [
1 => '前處理藥劑',
2 => '染色加工藥劑',
3 => '後處理加工'
];
function getAll()
{
$result = [];
$result[] = $this->get(1);
$result[] = $this->get(2);
$result[] = $this->get(3);
return $result;
}
function get($id)
{
$subcategories = [];
$sql = "select * from category2 where type = $id order by id";
$data = mysql_query($sql);
while ($row = mysql_fetch_assoc($data)){
$subcategories[] = $this->getSubcategory($row);
}
$category = new Category($id, $this->map[$id], $subcategories);
return $category;
}
private function getSubcategory($row)
{
$products = [];
$id = $row['id'];
$sql = "select * from menu2 where type = $id order by weight";
$data = mysql_query($sql);
while ($inner_row = mysql_fetch_assoc($data)){
$products[] = new Product($inner_row['id'], $inner_row['name'], $inner_row['brief'], $inner_row['weight']);
}
$subcategory = new Subcategory($row['id'], $row['name'], $products);
return $subcategory;
}
}
<?php
namespace Dxchemistry\TraditionalChinese;
class Product
{
private $id;
private $name;
private $description;
private $weight;
function __construct($id, $name, $description, $weight)
{
$this->id = $id;
$this->name = $name;
$this->description = $description;
$this->weight = $weight;
}
function getId()
{
return $this->id;
}
function getName()
{
return $this->name;
}
function getWeight()
{
return $this->weight;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment