Skip to content

Latest commit

 

History

History
128 lines (116 loc) · 2.81 KB

simple-repository.md

File metadata and controls

128 lines (116 loc) · 2.81 KB

Simple repository

$pdo = new PDO('mysql:host=127.0.0.1;dbname=test;charset=utf8', 'root', null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

$db = new \Kir\MySQL\Databases\MySQL($pdo);
$db->getAliasRegistry()->add('shop', 'shop__');
class ProductPriceQuery {
	/** @var Database */
	private $db;

	/**
	 * @param Database $db
	 */
	public function __construct(Database $db) {
		$this->db = $db;
	}

	/**
	 * @param array $criteria
	 * @return RunnableSelect
	 */
	public function getAll(array $criteria) {
		$req = new RequiredDBFilterMap($criteria);
		$opt = new OptionalDBFilterMap($criteria);
		return $this->db->select()
		->field('sp.pricelist_id')
		->field('sp.product_id')
		->field('sp.price')
		->from('sp', 'shop#product_pricelists')
		->where($req('sp.pricelist_id=?', 'pricelist.id'))
		->where($opt('sp.price = ?', 'pricelist.price.is'))
		->where($opt('sp.price >= ?', 'pricelist.price.min'))
		->where($opt('sp.price <= ?', 'pricelist.price.max'));
	}
}
class ProductRepository {
	/** @var Database */
	private $db;
	/** @var ProductPriceQuery */
	private $priceQuery;

	/**
	 * @param Database $db
	 * @param ProductPriceQuery $priceQuery
	 */
	public function __construct(Database $db, ProductAmountQuery $priceQuery) {
		$this->db = $db;
		$this->priceQuery = $priceQuery;
	}

	/**
	 * @param array $criteria
	 * @return RunnableSelect
	 */
	public function find(array $criteria = []) {
		$req = new RequiredDBFilterMap($criteria);
		$opt = new OptionalDBFilterMap($criteria);
		return $this->db->select()
		->field('sp.id')
		->field('sp.reference')
		->field('p.price')
		->field('spl.name')
		->field('spl.description')
		->from('sp', 'shop#products')
		->joinInner('spl', 'shop#product_descriptions', 'spl.product_id = sp.id')
		->joinLeft('p', $this->priceQuery->getAll($criteria), 'sp.product_id = p.product_id')
		->where('sp.active=?', true)
		->where($req('spl.lang_id=?', 'language.id'))
		->where($opt('sp.reference=?', 'product.reference'))
		->where($opt('spl.name LIKE ?', 'product.name'))
		->where($opt('spl.description LIKE ?', 'product.description'));
	}

	/**
	 * @param array $data
	 * @return int
	 */
	public function store(array $data) {
		$id = $this->db->insert()
		->setKey('id')
		->into('shop#products')
		->addAll($data, ['id'])
		->addOrUpdateAll($data, ['reference'])
		->run();
		return $id;
	}

	/**
	 * @param int $id
	 * @return bool
	 */
	public function remove($id) {
		$count = $this->db->delete()
		->from('shop#products')
		->where('id=?', $id)
		->run();
		return $count === 1;
	}
}
$pr = new ProductRepository($db, new ProductPriceQuery($db));
$pr->find([
	'language' => [
		'id' => 'en',
	],
	'product' => [
		'name' => 'Canon%',
		'description' => '%W-Lan%'
	],
	'pricelist' => [
		'id' => 10,
		'price' => [
			'min' => 35
		],
	],
]);

Back