/** * 获取单条数据 * @param Vo $vo 实体对象 * @param int $id 要查询的条件id * @throws UnexpectedValueException * @throws DbException * @return boolean 未找到数据返回false,找到一条数据返回true ,多条数据会产生异常 */ public function get(Vo $vo, $id) { if (!$vo instanceof PDOVo) { throw new UnexpectedValueException('vo参数必须为MySQLVo对象。'); } $vo->createTb($this->_link); $priKey = $vo->getPrimaryKey(); $where = " `{$priKey}`=:{$priKey} "; $tbName = $vo->getTableName(); $fieldsArr = $vo->getFields(); $fields = implode(",", $fieldsArr); $sql = "SELECT {$fields} FROM `" . $tbName . "` WHERE " . $where; Debug::trace('查询数据:' . $sql, 'mysql'); $sth = $this->_link->prepare($sql); $sth->bindValue(":{$priKey}", $id); Debug::trace(":{$priKey}={$id}", 'mysql'); $ret = $sth->execute(); if ($ret === false) { throw new DbException($sth->errorInfo(), $sth->errorCode()); } $result = $sth->fetchAll(); if ($result === false) { throw new DbException($sth->errorInfo(), $sth->errorCode()); } if (!is_array($result) || !$result) { return false; } if (count($result) > 1) { throw new DbException('查询到' . count($result) . '数据'); } $vo->fromArray($result[0]); //填充对象 return true; }
/** * 获取单条数据 * @param Vo $vo 实体对象 * @param int $id 此字段,目前无效 * @return Vo * @throws UnexpectedValueException */ public function get(Vo $vo, $id = 0) { if (!$vo instanceof NoSQLVo) { throw new UnexpectedValueException('vo对象必须为NoSQLVo'); } $key = $vo->getKey(); $data = $this->_link->get($key, $vo->getCas()); if (!$data) { return false; } $vo->fromArray($data); return true; }