Skip to content

Commit

Permalink
fix: update row via barry
Browse files Browse the repository at this point in the history
  • Loading branch information
papac committed Dec 26, 2020
1 parent ef2079b commit 67d9fbb
Showing 1 changed file with 39 additions and 41 deletions.
80 changes: 39 additions & 41 deletions src/Database/Barry/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,60 +436,39 @@ public function save()
*/
$primary_key_value = $this->getKeyValue();

/**
* If primary key value is null, we are going to start the creation of new
* row
*/
// If primary key value is null, we are going to start the creation of new row
if ($primary_key_value == null) {
/**
* Insert information in the database
*/
$r = $model->insert($this->attributes);

/**
* We get a last insertion id value
*/
$primary_key_value = $model->getLastInsertId();
// Insert information in the database
$row_affected = $model->insert($this->attributes);

/**
* Transtype value to the define primary key type
*/
if ($this->primary_key_type == 'int') {
$primary_key_value = (int) $primary_key_value;
} elseif ($this->primary_key_type == 'float') {
$primary_key_value = (float) $primary_key_value;
} elseif ($this->primary_key_type == 'double') {
$primary_key_value = (float) $primary_key_value;
} else {
$primary_key_value = (string) $primary_key_value;
}
// We get a last insertion id value
$primary_key_value = $model->getLastInsertId();

/**
* Set the primary key value
*/
// Set the primary key value
$this->attributes[$this->primary_key] = $primary_key_value;

$this->original = $this->attributes;

if ($r == 1) {
if ($row_affected == 1) {
$this->fireEvent('oncreated');
}

return $r;
return $row_affected;
}

$primary_key_value = $this->transtypeKeyValue($primary_key_value);

// Check the existent in database
if (!$model->exists($this->primary_key, $primary_key_value)) {
return 0;
}

// We set the primary key value
$this->original[$this->primary_key] = $primary_key_value;

$update_data = [];

foreach ($this->attributes as $key => $value) {
if (!isset($this->original[$key])
|| $this->original[$key] != $value
) {
if (!array_key_exists($key, $this->original) || $this->original[$key] !== $value) {
$update_data[$key] = $value;
}
}
Expand All @@ -499,15 +478,35 @@ public function save()
$update_data = $this->original;
}

$r = $model
->where($this->primary_key, $primary_key_value)
->update($update_data);
$row_affected = $model->where($this->primary_key, $primary_key_value)->update($update_data);

if ($r == 1) {
if ($row_affected == 1) {
$this->fireEvent('onupdate');
}

return $r;
return $row_affected;
}

/**
* Transtype the primary key value
*
* @param mixed $primary_key_value
* @return mixed
*/
private function transtypeKeyValue($primary_key_value)
{
// Transtype value to the define primary key type
if ($this->primary_key_type == 'int') {
$primary_key_value = (int) $primary_key_value;
} elseif ($this->primary_key_type == 'float') {
$primary_key_value = (float) $primary_key_value;
} elseif ($this->primary_key_type == 'double') {
$primary_key_value = (float) $primary_key_value;
} else {
$primary_key_value = (string) $primary_key_value;
}

return $primary_key_value;
}

/**
Expand All @@ -530,8 +529,7 @@ public function delete()
return 0;
}

$deleted = $model->where($this->primary_key, $primary_key_value)
->delete();
$deleted = $model->where($this->primary_key, $primary_key_value)->delete();

if ($deleted) {
$this->fireEvent('ondeleted');
Expand Down

0 comments on commit 67d9fbb

Please sign in to comment.