Files
zhamao-framework/src/ZM/DB/UpdateBody.php

56 lines
1.2 KiB
PHP
Raw Normal View History

2020-03-02 16:14:20 +08:00
<?php
namespace ZM\DB;
use ZM\Exception\DbException;
2021-07-09 01:38:30 +08:00
/**
* Class UpdateBody
* @package ZM\DB
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
*/
2020-03-02 16:14:20 +08:00
class UpdateBody
{
use WhereBody;
/**
* @var Table
*/
private $table;
/**
* @var array
*/
private $set_value;
2021-02-09 17:09:09 +08:00
2020-03-02 16:14:20 +08:00
/**
* UpdateBody constructor.
* @param Table $table
* @param array $set_value
*/
public function __construct(Table $table, array $set_value) {
$this->table = $table;
$this->set_value = $set_value;
}
/**
* @throws DbException
*/
2021-02-09 17:09:09 +08:00
public function save() {
2020-03-02 16:14:20 +08:00
$arr = [];
$msg = [];
2021-02-09 17:09:09 +08:00
foreach ($this->set_value as $k => $v) {
$msg [] = $k . ' = ?';
$arr[] = $v;
2020-03-02 16:14:20 +08:00
}
2021-02-09 17:09:09 +08:00
if (($msg ?? []) == []) throw new DbException('update value sets can not be empty!');
$line = 'UPDATE ' . $this->table->getTableName() . ' SET ' . implode(', ', $msg);
if ($this->where_thing != []) {
2020-03-02 16:14:20 +08:00
list($sql, $param) = $this->getWhereSQL();
$arr = array_merge($arr, $param);
2021-02-09 17:09:09 +08:00
$line .= ' WHERE ' . $sql;
2020-03-02 16:14:20 +08:00
}
return DB::rawQuery($line, $arr);
}
}