$value) { if(property_exists(get_called_class(), $field)) { $model ->$field = $value; } } $me = new User(); $me->loadMe(); if($model->create_by === null) { $model->create_by = $me->id; } if($model->edit_by === null) { $model->edit_by = $me->id; } return $model; } public static function getOne($id) { if(!is_numeric($id) || !$id) { throw new Exception("Invalid number", 400); } $item = []; $db = FronkDB::singleton(); $res = $db->select("Product", "*", "id=$id LIMIT 1"); if($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new Product($data); } return $item; } public static function getAll() { $items = []; $db = FronkDB::singleton(); $res = $db->select("Product", "*", "1 = 1 ORDER BY name,producttech_id"); if($db->num_rows($res)) { while($data = $db->fetch_object($res)) { $items[] = new Product($data); } } return $items; } public static function getFirst() { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $res = $db->select("Product", "*", "$where ORDER BY producttech_id, name LIMIT 1"); if($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new Product($data); if($item->id) { return $item; } else { return null; } } return null; } public static function search($filter) { $items = []; $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $sql = "SELECT Product.* FROM `Product` LEFT JOIN ProductAttribute ON (ProductAttribute.product_id = Product.id) LEFT JOIN Producttech ON (Product.producttech_id = Producttech.id) LEFT JOIN ProducttechAttribute ON (ProducttechAttribute.producttech_id = Producttech.id) WHERE $where GROUP BY Product.id"; $res = $db->query($sql); //$res = $db->select("Product", "*", "$where ORDER BY name, producttech_id"); if($db->num_rows($res)) { while($data = $db->fetch_object($res)) { $items[] = new Product($data); } } return $items; } private static function getSqlFilter($filter) { $where = "1=1 "; $db = FronkDB::singleton(); //var_dump($filter);exit; if(array_key_exists("id", $filter)) { $id = $db->escape($filter['id']); if($id) { $where .= " AND Product.`id` like '%$id%'"; } } if(array_key_exists("productgroup_id", $filter)) { $productgroup_id = $filter['productgroup_id']; if(is_numeric($productgroup_id)) { $where .= " AND productgroup_id=$productgroup_id"; } } if(array_key_exists("producttech_id", $filter)) { $producttech_id = $filter['producttech_id']; if(is_numeric($producttech_id)) { $where .= " AND producttech_id=$producttech_id"; } } if(array_key_exists("name", $filter)) { $name = $db->escape($filter['name']); if($name) { $where .= " AND Product.`name` like '$name'"; } } if(array_key_exists("nameLike", $filter)) { $name = $db->escape($filter['nameLike']); if($name) { $where .= " AND Product.`name` like '%$name%'"; } } if(array_key_exists("attributename", $filter)) { $attributename = $db->escape($filter['attributename']); if($attributename) { $where .= " AND ProducttechAttribute.name = '$attributename'"; } } if(array_key_exists("attributevalue", $filter)) { $attributevalue = $db->escape($filter['attributevalue']); if($attributevalue) { $where .= " AND ProductAttribute.value = '$attributevalue'"; } } //var_dump($filter, $where);exit; return $where; } }