public static function get($ka = null) { if (is_string($ka)) { $keys = array($ka); } elseif (is_array($ka)) { $keys = $ka; } elseif (empty($ka)) { // return all key-values $ret = array(); $arr = Pdb::fetchAll('*', self::$table); foreach ($arr as $entry) { $ret[$entry['key']] = $entry['value']; } return $ret; } $ret = array(); foreach ($keys as $key) { $value = Pdb::fetchRow('`value`', self::$table, array('`key`=?' => $key)); if ($value === false) { throw new Exception("there is no key: {$key}"); } $ret[$key] = $value; } return count($ret) === 1 ? reset($ret) : $ret; }
protected function info() { $self = get_called_class(); $ret = Pdb::fetchRow('*', $self::$table, $this->selfCond()); if (empty($ret)) { throw new Exception(get_called_class() . " no id: {$this->id}"); } return $ret; }
public function defaultAddress() { $default_addr = Pdb::fetchRow('address', customer_address, array('customer=?' => $this->id, 'is_default=?' => 1)); if ($default_addr) { return new Address($default_addr); } else { $addresses = $this->addresses(); $addr = $addresses[0]; $addr->setDefault(); return $addr; } }
public function getByName($username) { $cond = array('name=?' => $username); return new self(Pdb::fetchRow('*', self::$table, $cond)); }
public function info() { return Pdb::fetchRow('*', self::$table, $this->selfCond()); }
public function remain() { return Pdb::fetchRow('remain', self::$table, $this->selfCond()); }
private static function typeId($name = '') { $id = Pdb::fetchRow('id', 'product_type', array('name = ?' => $name)); if (empty($id)) { throw new Exception("no type: {$name}"); } return $id; }
public static function get($type, $time) { return Pdb::fetchRow('price', self::$table, array_merge(self::typeCond($type), array('time >= ?' => $time)), 'ORDER BY time ASC'); }
$phone = '13711231212'; $email = '*****@*****.**'; $info = compact('username', 'password', 'realname', 'phone', 'email'); $customer = Customer::create($info); test(1, 1, array('name' => 'register Customer, db')); begin_test(); test(User::check($username, $password), true, array('name' => 'User::check($username, $password)')); begin_test(); $username = '******'; $password = '******'; $user = User::getByName('root'); $superadmin = $user->instance(); $admin = $superadmin->createAdmin(compact('username', 'password')); $ideal_arr = array('name' => $username, 'password' => md5($password), 'type' => 'Admin'); $id = Pdb::lastInsertId(); $real_arr = Pdb::fetchRow('name, password, type', User::$table, array('id=?' => $id)); test($real_arr, $ideal_arr, array('name' => 'Super Admin create Admin, db')); begin_test(); $prd_types = Product::types(); $info = array('name' => '唯爱心形群镶女戒_test', 'type' => reset(array_keys($prd_types)), 'material' => json_encode(array('PT950', '白18K金', '黄18K金', '红18K金')), 'rabbet_start' => '0.30', 'rabbet_end' => '0.60', 'weight' => 9, 'small_stone' => 3, 'st_weight' => 2.1, 'images' => array('400' => array('/test/static/img/i400-1.jpg', '/test/static/img/i400-2.jpg', '/test/static/img/i400-3.jpg'), 'thumb' => array('/test/static/img/i80-1.jpg', '/test/static/img/i80-2.jpg', '/test/static/img/i80-3.jpg'))); $product = Product::create($info); test(1, 1, array('name' => 'Admin post Product, db')); begin_test(); $address = $customer->defaultAddress(); $address->edit(array('name' => '小池', 'phone' => '14722320989', 'detail' => '深圳罗湖区田贝')); test(1, 1, array('name' => 'edit Address')); begin_test(); $cart = $customer->cart(); test(+$cart->count(), 2, array('name' => 'Cart count()')); begin_test(); $cart->submit();
$info = array('name' => '嘉黛_test', 'contact' => '吴小牛', 'phone' => '13526523659', 'qq' => '5833652584', 'city' => '湖北武汉'); $factory = $admin->addFactory($info); test(count(Factory::names()), 1, array('name' => 'Admin add Factory')); // case 8 Admin post Product, db begin_test(); $prd_types = Product::types(); $info = array('name' => '唯爱心形群镶女戒_test', 'type' => reset(array_keys($prd_types)), 'material' => json_encode(array('PT950', '白18K金', '黄18K金', '红18K金')), 'rabbet_start' => '0.30', 'rabbet_end' => '0.60', 'small_stone' => 3, 'st_weight' => 2.1, 'image1_400' => '/test/static/img/i400-1.jpg', 'image2_400' => '/test/static/img/i400-2.jpg', 'image3_400' => '/test/static/img/i400-3.jpg', 'image1_thumb' => '/test/static/img/i80-1.jpg', 'image2_thumb' => '/test/static/img/i80-2.jpg', 'image3_thumb' => '/test/static/img/i80-3.jpg'); $product = $admin->postProduct($info); // add more // for ($i=0; $i < 50; $i++) { // $info2 = $info; // $info2['name'] = $info['name'] . $i; // $admin->postProduct($info2); // } // but what if we count? test(Pdb::fetchRow('*', Product::$table, array('id=?' => $product->id)), $info, array('name' => 'Admin post Product, db', 'compare' => 'in')); // case 9 Admin del Product begin_test(); $info = array_merge($info, array('name' => 'product test to del')); $product_to_del1 = $admin->postProduct($info); $product_to_del2 = $admin->postProduct($info); $old_num = Product::count(); $admin->delProduct($product_to_del1); $admin->delProduct($product_to_del2->id); $new_num = Product::count(); test($old_num - 2, $new_num, array('name' => 'Admin del Product')); // case 10 Customer edit Address begin_test(); $address = $customer->defaultAddress(); $address->edit(array('name' => '小池', 'phone' => '14722320989', 'detail' => '深圳罗湖区田贝')); test(1, 1, array('name' => 'Customer edit Address'));