Skip to content

Commit

Permalink
Updated the logic for calculating number of deleted records in Model-…
Browse files Browse the repository at this point in the history
…>deleteMatchingDbTableRows(array $cols_n_vals): int
  • Loading branch information
rotexdegba committed Jun 7, 2024
1 parent af5bd78 commit eb194de
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"phpunit/phpunit": "^9.0",
"php-coveralls/php-coveralls": "^2.0",
"vimeo/psalm": "^5.4.0",
"rector/rector": "^0.19.0",
"rector/rector": "^1.0.0",
"symfony/polyfill-php80": "^1.26",
"symfony/polyfill-php81": "^1.26",
"symfony/polyfill-php82": "^1.26",
Expand All @@ -43,10 +43,9 @@
"Composer\\Config::disableProcessTimeout",
"vendor/bin/phpunit --coverage-text"
],
"rector-clear": "vendor/bin/rector --clear-cache",
"rector": "vendor/bin/rector process src --dry-run -vvv",
"psalm": "vendor/bin/psalm --threads=1",
"qa": "composer test && composer rector-clear && composer rector && composer psalm"
"qa": "composer test && composer rector && composer psalm"
},
"suggest": {
"rotexsoft/leanorm-cli": "A Command-line tool for rotexsoft/leanorm for generating Model, Record & Collection Classes for tables & views in a specified database."
Expand Down
22 changes: 11 additions & 11 deletions src/LeanOrm/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,10 @@ public function deleteMatchingDbTableRows(array $cols_n_vals): int {

//delete statement
$del_qry_obj = (new QueryFactory($this->getPdoDriverName()))->newDelete();
$sel_qry_obj = (new QueryFactory($this->getPdoDriverName()))->newSelect();
$del_qry_obj->from($this->getTableName());
$sel_qry_obj->from($this->getTableName());
$sel_qry_obj->cols([' count(*) ']);
$table_cols = $this->getTableColNames();

foreach ($cols_n_vals as $colname => $colval) {
Expand All @@ -1477,7 +1480,8 @@ public function deleteMatchingDbTableRows(array $cols_n_vals): int {
$colval[$key] = $this->stringifyIfStringable($val);
}

$this->addWhereInAndOrIsNullToQuery($colname, $colval, $del_qry_obj);
$this->addWhereInAndOrIsNullToQuery(''.$colname, $colval, $del_qry_obj);
$this->addWhereInAndOrIsNullToQuery(''.$colname, $colval, $sel_qry_obj);

} else {

Expand All @@ -1487,6 +1491,7 @@ public function deleteMatchingDbTableRows(array $cols_n_vals): int {
}

$del_qry_obj->where(" {$colname} = :{$colname} ", [$colname => $this->stringifyIfStringable($colval)]);
$sel_qry_obj->where(" {$colname} = :{$colname} ", [$colname => $this->stringifyIfStringable($colval)]);
}
}

Expand All @@ -1498,19 +1503,14 @@ public function deleteMatchingDbTableRows(array $cols_n_vals): int {
$dlt_qry_params = $del_qry_obj->getBindValues();
$this->logQuery($dlt_qry, $dlt_qry_params, __METHOD__, '' . __LINE__);

$result = $this->db_connector->executeQuery($dlt_qry, $dlt_qry_params, true);
$matching_rows_before_delete = (int) $this->fetchValue($sel_qry_obj);

if( $result['query_result'] === true ) {
$this->db_connector->executeQuery($dlt_qry, $dlt_qry_params, true);

//return number of affected rows
$pdo_statement_used_for_query = $result['pdo_statement'];
$result = $pdo_statement_used_for_query->rowCount();
} else {
$matching_rows_after_delete = (int) $this->fetchValue($sel_qry_obj);

// something went wrong
// TODO: Maybe throw an exception
$result = 0;
} // if( $result['query_result'] === true )
//number of deleted rows
$result = $matching_rows_before_delete - $matching_rows_after_delete;
} // if($cols_n_vals !== [])
} // if ( $cols_n_vals !== [] )

Expand Down
10 changes: 2 additions & 8 deletions src/LeanOrm/Model/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,18 +483,12 @@ public function saveInTransaction($data_2_save = null): ?bool {
*/
public function __set($key, $val): void {

if (
$this->getModel() instanceof \GDAO\Model
&& in_array($key, $this->getModel()->getTableColNames())
) {
if (in_array($key, $this->getModel()->getTableColNames())) {
//$key is a valid db column in the db table assoiciated with this
//model's record.
$this->data[$key] = $val;

} elseif(
$this->getModel() instanceof \GDAO\Model
&& in_array($key, $this->getModel()->getRelationNames())
) {
} elseif (in_array($key, $this->getModel()->getRelationNames())) {
//$key is a valid relation name in the model for this record.
$this->related_data[$key] = $val;

Expand Down

0 comments on commit eb194de

Please sign in to comment.