You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.1 KiB
79 lines
2.1 KiB
<?php
|
|
class Model
|
|
{
|
|
protected $table = '';
|
|
protected $fillable = [];
|
|
/**
|
|
* 创建记录
|
|
*
|
|
* @param array $data
|
|
* @return void
|
|
*/
|
|
public function create(array $data)
|
|
{
|
|
$_data = [];
|
|
foreach ($this->fillable as $col) {
|
|
$_data[$col] = empty($data[$col]) ? '' : $data[$col];
|
|
}
|
|
DB::insert_table($this->table, $_data);
|
|
}
|
|
/**
|
|
* 创建或更新
|
|
*
|
|
* @return void
|
|
*/
|
|
public function createOrUpdate(array $keyCol, array $data)
|
|
{
|
|
|
|
$where = ' 1=1 ';
|
|
foreach ($keyCol as $key => $col) {
|
|
$where .= " and " . $col[0] . " = '" . $col[1] . "'";
|
|
}
|
|
// echo "select count(*) from $this->table where $where ";
|
|
|
|
list($cnt) = DB::fields(
|
|
"select count(*) from $this->table where $where "
|
|
);
|
|
if ($cnt == 0) {
|
|
foreach ($keyCol as $key => $col) {
|
|
$data[$col[0]] = $col[1];
|
|
}
|
|
self::create($data);
|
|
} else {
|
|
self::update($keyCol, $data);
|
|
}
|
|
}
|
|
public function delete(array $keyCol, array $data)
|
|
{
|
|
$where = ' 1=1 ';
|
|
foreach ($keyCol as $key => $col) {
|
|
$where .= " and " . $col[0] . " = '" . $col[1] . "'";
|
|
}
|
|
// echo "delete from $this->table where $where ";
|
|
DB::query(
|
|
"delete from $this->table where $where "
|
|
);
|
|
}
|
|
public function update(array $keyCol, array $data)
|
|
{
|
|
$_data = [];
|
|
foreach ($this->fillable as $col) {
|
|
if (in_array($col, array_keys($data))) $_data[$col] = empty($data[$col]) ? '' : $data[$col];
|
|
}
|
|
$where = ' 1=1 ';
|
|
foreach ($keyCol as $key => $col) {
|
|
$where .= " and " . $col[0] . " = '" . $col[1] . "'";
|
|
}
|
|
DB::update_table($this->table, $_data, $where);
|
|
}
|
|
/**
|
|
* 获取记录
|
|
*
|
|
* @param array $cond
|
|
* @return void
|
|
*/
|
|
public function get(array $cond)
|
|
{
|
|
return [];
|
|
}
|
|
}
|
|
|