$value) { if(property_exists(get_called_class(), $field)) { $model->$field = $value; } } $me = new User(); $me->loadMe(); if(!is_numeric($model->create_by) && !$model->create_by) { $model->create_by = $me->id; } if(!is_numeric($model->edit_by) && !$model->edit_by) { $model->edit_by = $me->id; } return $model; } public static function getFirst($filter = null) { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $res = $db->select("AddressLink", "*", "$where ORDER BY address_id,`create`,origin_address_id LIMIT 1"); if($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new AddressLink($data); if($item->id) { return $item; } else { return null; } } return null; } public static function getAll() { $items = []; $db = FronkDB::singleton(); $res = $db->select("AddressLink", "*", "1=1 ORDER BY address_id,`create`,origin_address_id"); if($db->num_rows($res)) { while($data = $db->fetch_object($res)) { $items[] = new AddressLink($data); } } return $items; } public static function count($filter) { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $res = $db->select("AddressLink", "COUNT(*) as cnt", "$where"); //$res = $db->query($sql); if($db->num_rows($res)) { $data = $db->fetch_object($res); return $data->cnt; } return 0; } public static function search($filter, $limit = false) { $items = []; $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $sql = "SELECT * FROM AddressLink WHERE $where ORDER BY address_id,`create`,origin_address_id"; if(is_array($limit) && count($limit)) { if(is_numeric($limit['start']) && is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; } elseif(is_numeric($count)) { $sql .= " LIMIT ".$limit['count']; } } mfLoghandler::singleton()->debug($sql); $res = $db->query($sql); if($db->num_rows($res)) { while($data = $db->fetch_object($res)) { $items[] = new AddressLink($data); } } return $items; } private static function getSqlFilter($filter) { $where = "1=1 "; //var_dump($filter);exit; if(array_key_exists("address_id", $filter)) { $address_id = $filter["address_id"]; if(is_numeric($address_id)) { $where .= " AND address_id=$address_id"; } } if(array_key_exists("origin_address_id", $filter)) { $origin_address_id = $filter["origin_address_id"]; if(is_numeric($origin_address_id)) { $where .= " AND origin_address_id=$origin_address_id"; } } if(array_key_exists("type", $filter)) { switch($filter['type']) { case "employee": $where .= " AND type='employee'"; break; case "billing": $where .= " AND type='billing'"; break; case "contact": $where .= " AND type='contact'"; break; case "techcontact": $where .= " AND type='techcontact'"; break; } } return $where; } }