Skip to content

Commit

Permalink
update save method,add method deleteModel
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Nov 22, 2023
1 parent 0dc494f commit f33d873
Showing 1 changed file with 47 additions and 53 deletions.
100 changes: 47 additions & 53 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
class Model
{
use ModelEventTrait;

/**
* @var DBInterface The database connection instance.
*/
Expand Down Expand Up @@ -345,6 +344,9 @@ public function setEntry($property, $value): void
}
$type = $this->getSchema($property)->getDataType();
if (gettype($value) === $type) {
// if (! $this->validateDateTime($value)) {
// throw new \InvalidArgumentException("invalid datetime format for argument '$property'");
// }
$this->entries[$property] = $value;
if ($this->hasOption('called_get')) {
$this->addToOption('updated_values', $property, $value);
Expand Down Expand Up @@ -704,13 +706,8 @@ public static function create(array $data, $useTransaction = false)
*/
public function save(): bool
{
$this->createModelStructure();
$this->createTimestamp();

if ($this->getEntry('id') == Schema::DEFAULT_VALUE_UNSET) {
$this->removeEntry('id');
}

$query = Query::insert($this->getTable(), Insert::INSERT_DATA);

$query->data($this->getEntries());
Expand All @@ -719,14 +716,15 @@ public function save(): bool

static::setQuery($query);

if ($this->event('saving') === false) {
if ($this->event('saving', $this) === false) {
return false;
}
$db = $this->getDatabaseConnection();
$result = $db->query((string) $query)->run($query->getParams());

$result = $this->getDatabaseConnection()->query((string) $query)->run($query->getParams());

if ($result) {
$this->event('saved');
if ($result === true) {
$this->setEntry($this->getKeyName(), (int) $db->lastInsertId());
$this->event('saved', $this);
return true;
}

Expand Down Expand Up @@ -761,14 +759,14 @@ public function update(): bool

$params = array_merge($query->getParams(), [$this->getKeyName() => $id]);

if ($this->event('updating') === false) {
if ($this->event('updating', $this) === false) {
return false;
}

$result = $this->getDatabaseConnection()->query((string) $query)->run($params);

if ($result) {
$this->event('updated');
$this->event('updated', $this);
return true;
}

Expand All @@ -778,11 +776,11 @@ public function update(): bool
/**
* Perform a model operation in a transaction.
*
* @param callable $callback
* @param \Closure $callback
* @param array ...$args
* @return void
*/
public static function transaction(callable $callback, array ...$args)
public static function transaction(\Closure $callback, array ...$args)
{
return static::getDatabaseConnection()->transaction($callback, $args);
}
Expand Down Expand Up @@ -815,9 +813,9 @@ public function updateInTransaction()
* @param callable|null $toQuery
* @param DataRulesInterface|null $rules
* @param self|null $model
* @return DataCollectionInterface|null
* @return DataCollectionInterface
*/
public static function get(array|string $columns = '*', ?callable $toQuery = null, ?DataRulesInterface $rules = null, ?self $model = null): ?DataCollectionInterface
public static function get(array|string $columns = '*', ?callable $toQuery = null, ?DataRulesInterface $rules = null, ?self $model = null): DataCollectionInterface
{

$model = $model ?? static::newInstance();
Expand All @@ -839,17 +837,9 @@ public static function get(array|string $columns = '*', ?callable $toQuery = nul

$statement = $model->getDatabaseConnection()->query((string) $query);

if ($rules) {
$data = $statement->fetchPretty($rules);
} else {
$data = $statement->fetch();
}
$data = $rules ? $statement->fetchPretty($rules) : $statement->fetch();

if (!$data) {
return null;
}

return (new DataCollection($data))
return (new DataCollection($data ?? []))
->map(
fn ($item) => static::newInstance()
->setOption('called_get', [])
Expand All @@ -862,9 +852,9 @@ public static function get(array|string $columns = '*', ?callable $toQuery = nul
*
* @param array|string $columns
* @param DataRulesInterface|null $rules
* @return DataCollectionInterface|null
* @return DataCollectionInterface
*/
public static function all(array|string $columns = '*', ?DataRulesInterface $rules = null): ?DataCollectionInterface
public static function all(array|string $columns = '*', ?DataRulesInterface $rules = null): DataCollectionInterface
{
return static::get($columns, null, $rules);
}
Expand All @@ -876,9 +866,9 @@ public static function all(array|string $columns = '*', ?DataRulesInterface $rul
* @param int|null $count_until
* @param array|string $columns
* @param DataRulesInterface|null $rules
* @return DataCollectionInterface|null
* @return DataCollectionInterface
*/
public static function limit(int $start_from, ?int $count_until = null, array|string $columns = '*', ?DataRulesInterface $rules = null): ?DataCollectionInterface
public static function limit(int $start_from, ?int $count_until = null, array|string $columns = '*', ?DataRulesInterface $rules = null): DataCollectionInterface
{
return static::get($columns, fn (Select $q) => $q->limit($start_from, $count_until), $rules);
}
Expand Down Expand Up @@ -907,9 +897,9 @@ public static function find(int $id, array|string $columns = '*', ?DataRulesInte
* @param mixed $value
* @param array|string $columns
* @param DataRulesInterface|null $rules
* @return DataCollectionInterface|null
* @return DataCollectionInterface
*/
public static function findBy(string $term, $value, array|string $columns = '*', ?DataRulesInterface $rules = null): ?DataCollectionInterface
public static function findBy(string $term, $value, array|string $columns = '*', ?DataRulesInterface $rules = null): DataCollectionInterface
{
return static::get($columns, fn ($query) => $query->where([$term => $value]), $rules);
}
Expand All @@ -920,9 +910,9 @@ public static function findBy(string $term, $value, array|string $columns = '*',
* @param array $terms
* @param array|string $columns
* @param DataRulesInterface|null $rules
* @return DataCollectionInterface|null
* @return DataCollectionInterface
*/
public static function search(array $terms, array|string $columns = '*', ?DataRulesInterface $rules = null): ?DataCollectionInterface
public static function search(array $terms, array|string $columns = '*', ?DataRulesInterface $rules = null): DataCollectionInterface
{
if (empty($terms)) {
throw new \InvalidArgumentException('Invalid terms.');
Expand All @@ -936,9 +926,9 @@ public static function search(array $terms, array|string $columns = '*', ?DataRu
* @param Condition $conditions
* @param array|string $columns
* @param DataRulesInterface|null $rules
* @return DataCollectionInterface|null
* @return DataCollectionInterface
*/
public static function where(Condition $conditions, array|string $columns = '*', ?DataRulesInterface $rules = null): ?DataCollectionInterface
public static function where(Condition $conditions, array|string $columns = '*', ?DataRulesInterface $rules = null): DataCollectionInterface
{
return static::get($columns, fn (Select $q) => $q->whereConditions($conditions), $rules);
}
Expand All @@ -951,9 +941,9 @@ public static function where(Condition $conditions, array|string $columns = '*',
* @param mixed $to
* @param array|string $columns
* @param DataRulesInterface|null $rules
* @return DataCollectionInterface|null
* @return DataCollectionInterface
*/
public static function between(string $targetColumn, string|int|float $from, string|int|float $to, array|string $columns = '*', ?DataRulesInterface $rules = null): ?DataCollectionInterface
public static function between(string $targetColumn, string|int|float $from, string|int|float $to, array|string $columns = '*', ?DataRulesInterface $rules = null): DataCollectionInterface
{
return static::get($columns, fn (Select $q) => $q->whereInBetween($targetColumn, $from, $to), $rules);
}
Expand All @@ -964,13 +954,13 @@ public static function between(string $targetColumn, string|int|float $from, str
* @param Condition $conditions
* @param array|null $params
* @param self|null $model
* @return bool
* @return bool return true if success, false
*/
public static function delete(Condition $conditions, ?array $params = null, ?self $model = null): bool
{
$model = $model ?? static::newInstance();

if ($model->event('deleting') === false) {
if ($model->event('deleting', $model) === false) {
return false;
}

Expand All @@ -982,7 +972,7 @@ public static function delete(Condition $conditions, ?array $params = null, ?sel
$result = $model->getDatabaseConnection()->query((string) $query)->run($params);

if ($result) {
$model->event('deleted');
$model->event('deleted', $model);
}
return $result;
}
Expand All @@ -996,6 +986,7 @@ public static function delete(Condition $conditions, ?array $params = null, ?sel
public static function deleteById(string|int $id): bool
{
$model = static::newInstance();

if (!$model->validateId($id)) {
throw new \InvalidArgumentException("Error Processing ID($id),ID must be type {$model->getKeyType()}");
}
Expand Down Expand Up @@ -1023,6 +1014,20 @@ public static function deleteByIds(array $ids): bool
return $success;
}

/**
* Delete models from the database based on its id.
* @return bool
*/
public function deleteModel(): bool
{
if (!$this->hasEntry($this->getKeyName())) {
throw new \Exception("Error Processing Delete, {$this->getKeyName()} not defined");
}
$id = $this->getEntry($this->getKeyName());

return static::deleteById($id);
}

/**
* Delete models from the database by their primary keys in a transaction.
*
Expand Down Expand Up @@ -1050,17 +1055,6 @@ public static function truncate(): bool
return $model->getDatabaseConnection()->query((string) $query)->run();
}

/**
* Get the last inserted ID for the model.
*
* @return int
*/
public static function lastInsertId(): int
{
$model = static::newInstance();
return (int) $model->getDatabaseConnection()->lastInsertId();
}

/**
* Validate a model ID.
*
Expand Down

0 comments on commit f33d873

Please sign in to comment.