From 06a49f37d5555f384c3a98f660f3db5d650dc60e Mon Sep 17 00:00:00 2001 From: Thomas Recouvreux Date: Wed, 11 Sep 2013 22:18:50 -0400 Subject: [PATCH] Remove price when product is removed --- src/Payutc/Bom/Product.php | 8 ++++++-- tests/ProductRwdbTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Payutc/Bom/Product.php b/src/Payutc/Bom/Product.php index 7708899..2c5e524 100644 --- a/src/Payutc/Bom/Product.php +++ b/src/Payutc/Bom/Product.php @@ -91,7 +91,6 @@ public static function getOne($obj_id, $fun_id=null, $removed=0) { 'obj_type' => "product", 'obj_id' => $obj_id, )); - if($fun_id !== null) { $qb->andWhere('obj.fun_id = :fun_id') ->setParameter('fun_id', $fun_id); @@ -267,7 +266,12 @@ public static function delete($id, $fun_id) { $db->query("UPDATE t_object_obj SET `obj_removed` = '1' WHERE `obj_id` = '%u';",array($id)); // 3. DELETE PRICE - // TODO !! + $qb = Dbal::createQueryBuilder(); + $qb->update('t_price_pri', 'pri') + ->where('obj_id = :id') + ->set('pri_removed', 1) + ->setParameter('id', $id); + $qb->execute(); return array("success"=>"ok"); } diff --git a/tests/ProductRwdbTest.php b/tests/ProductRwdbTest.php index dc5edde..b184a2e 100644 --- a/tests/ProductRwdbTest.php +++ b/tests/ProductRwdbTest.php @@ -3,6 +3,7 @@ require_once 'bootstrap.php'; use \Payutc\Bom\Product; +use \Payutc\Db\Dbal; class ProductRwdbTest extends DatabaseTest { @@ -72,6 +73,33 @@ public function testAdd() { $this->assertEquals(1001, $r['categorie_id']); $this->assertEquals(1, $r['fundation_id']); } + + /** + * @depends testAdd + */ + public function testDelete() { + // create object + $a = Product::add("Chouffe", 1000, 180, 10, 1, null, 1); + $id = $a["success"]; + // delete object + Product::delete($id, 1); + // assert we cant get the object now + $r = Product::getOne($id, 1); + $this->assertNull($r); + // but we can get it if we allow deleted objects + $r = Product::getOne($id, 1, 1); + $this->assertNotNull($r); + // check the price has been removed too + $qb = Dbal::createQueryBuilder(); + $q = $qb->select('pri_removed') + ->from('t_price_pri', 'pri') + ->where('obj_id = :id') + ->setParameter('id', $id); + $prices = $q->execute()->fetchAll(); + foreach($prices as $p) { + $this->assertEquals(1, $p['pri_removed']); + } + } }