ction setLimit( $limit )
{
$this->limit = $limit;
}
/**
* 获取条数
*/
function getLimit()
{
return $this->limit;
}
/**
* 根据主键获取
*/
function getInfoByPk()
{
$sql = "select {$this->getFields()} "
."from {$this->getTable()} "
."where {$this->getPrimaryKeyField()}={$this->getPk()}";
return $this->query_one($sql);
}
/**
* 根据where条件获取一条信息
*/
function getOneByWhere()
{
$sql = "SELECT {$this->getFields()} "
. "FROM {$this->getTable()} "
. "WHERE {$this->getWhere()}";
return $this->query_one($sql);
}
/**
* 根据where条件获取数组列表
*/
function getListByWhere()
{
$sql = "SELECT ";
$sql .= "{$this->getFields()} "
. "FROM {$this->getTable()} ";
if ( $this->getWhere() != null ) {
$sql .= "WHERE {$this->getWhere()} ";
}
if ( $this->getGroupby() != null ) {
$sql .= "GROUP BY {$this->getGroupby()} ";
}
if ( $this->getOrderby() != null ) {
$sql .= "ORDER BY {$this->getOrderby()} ";
}
if ( $this->getLimit() != null ) {
$sql .= "LIMIT {$this->getLimit()}";
}
return $this->query_all($sql);
}
/**
* 根据where获取count
*/
function getCountByWhere()
{
$sql_count = "SELECT COUNT(*) AS total FROM {$this->getTable()} ";
if ( $this->getWhere() != null ) {
$sql_count .= "WHERE " . $this->getWhere();
}
return $this->query_scalar($sql_count);
}
/**
* 根据主键更新
*/
function updateByPk($entity)
{
$this->setFieldValues($entity);
$sql = "UPDATE {$this->getTable()} SET ";
foreach ($this->getFieldValues() as $key => $one){
if ($one != NULL){
$sql .= "$key='$one',";
}
}
$sql = rtrim($sql, ',');
$sql .= " WHERE {$this->getPrimaryKeyField()}='{$this->getPk()}'";
return $this->execute($sql);
}
/**
* 根据WHERE更新
*/
function updateByWhere($entity)
{
$this->setFieldValues($entity);
$sql = "UPDATE {$this->getTable()} SET ";
foreach ($this->getFieldValues() as $key => $one){
if ($one != NULL){
$sql .= "$key='$one',";
}
}
$sql = rtrim($sql, ',');
$sql .= " {$this->getWhere()}";
return $this->execute($sql);
}
/**
* 根据WHERE更新
*/
function insert_table($entity)
{
$this->setFieldValues($entity);
$sql_values = '';
$sql = "INSERT INTO {$this->getTable()} (";
foreach ($this->getFieldValues() as $key => $one){
if ($one != NULL){
$sql .= "$key,";
$sql_values .= "'$one',";
}
}
$sql = rtrim($sql, ',').") VALUES (".rtrim($sql_values, ',').")";
return $this->execute($sql);
}
//-------------------------Yii2 base----------------------
//-------------------------Yii2 base----------------------
//-------------------------Yii2 base----------------------
//* @author nike@youfumama.com
//* @date 2017-03-08
//* 可扩展
/**
* 只获取一行,如果该查询没有结果则返回 false
* @param type $sql
* @return mix
*/
private function query_one($sql)
{
return $this->_getDb()->createCommand($sql)->queryOne();
}
/**
* 返回所有数组,如果该查询没有结果则返回空数组
* @param type $sql
* @return type
*/
private function query_all($sql)
{
return $this->_getDb()->createCommand($sql)->queryAll();
}
/**
* 返回一个 |