Skip to content

Commit 1ba6f24

Browse files
authored
Merge pull request #124 from Planetbiru/3.15.0
3.15.0
2 parents af21acc + bc3bce1 commit 1ba6f24

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,7 @@ This enhancement improves developer ergonomics when working with deeply nested d
10581058
Added support for the `@TimeRange` validation annotation to validate time values within a specific range.
10591059

10601060
**Usage Example:**
1061+
10611062
```php
10621063
/**
10631064
* @TimeRange(min="08:00", max="17:00")

src/Database/PicoDatabasePersistence.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MagicObject\Exceptions\EntityException;
1010
use MagicObject\Exceptions\InvalidAnnotationException;
1111
use MagicObject\Exceptions\InvalidFilterException;
12+
use MagicObject\Exceptions\InvalidParameterException;
1213
use MagicObject\Exceptions\NoInsertableColumnException;
1314
use MagicObject\Exceptions\NoColumnMatchException;
1415
use MagicObject\Exceptions\NoDatabaseConnectionException;
@@ -2917,6 +2918,65 @@ public function deleteBy($propertyName, $propertyValue)
29172918
throw new DataRetrievalException($e->getMessage());
29182919
}
29192920
}
2921+
2922+
/**
2923+
* Deletes a database record based on its primary key value(s).
2924+
*
2925+
* This method constructs and executes a DELETE SQL query to remove a record
2926+
* that matches the given primary key value(s). It returns the number of affected rows.
2927+
* If an exception occurs during execution, the method returns 0.
2928+
*
2929+
* @param mixed $propertyValues The primary key value or an associative array of key-value pairs.
2930+
* - If the table has a single-column primary key, this can be a scalar.
2931+
* - If the table has a composite primary key, this must be an associative array.
2932+
* @return int The number of deleted records (0 or 1 typically).
2933+
*/
2934+
public function deleteByPrimaryKey($propertyValues)
2935+
{
2936+
try
2937+
{
2938+
$sqlQuery = $this->deleteByPrimaryKeyQuery($propertyValues);
2939+
$stmt = $this->database->executeQuery($sqlQuery);
2940+
return $stmt->rowCount();
2941+
}
2942+
catch(Exception $e)
2943+
{
2944+
return 0;
2945+
}
2946+
}
2947+
2948+
/**
2949+
* Builds a DELETE SQL query to remove a record based on primary key value(s).
2950+
*
2951+
* This method uses the internal table metadata to determine the primary key columns
2952+
* and creates a `WHERE` clause that matches all key(s).
2953+
*
2954+
* @param mixed $propertyValues The primary key value or an associative array of key-value pairs.
2955+
* - For a single-column primary key: scalar value.
2956+
* - For composite key: associative array like ['id' => 5, 'lang' => 'en'].
2957+
* @return PicoDatabaseQueryBuilder The prepared query builder with the DELETE SQL query.
2958+
* @throws InvalidParameterException If the primary key values are invalid or missing.
2959+
*/
2960+
public function deleteByPrimaryKeyQuery($propertyValues)
2961+
{
2962+
// Convert to array if necessary
2963+
$propertyValues = $this->toArray($propertyValues);
2964+
$info = $this->getTableInfo();
2965+
$primaryKeys = $info->getPrimaryKeys();
2966+
2967+
if ($this->isValidPrimaryKeyValues($primaryKeys, $propertyValues))
2968+
{
2969+
$queryBuilder = new PicoDatabaseQueryBuilder($this->database);
2970+
$where = $this->createWhereByPrimaryKeys($queryBuilder, $primaryKeys, $propertyValues);
2971+
return $queryBuilder
2972+
->newQuery()
2973+
->delete()
2974+
->from($info->getTableName())
2975+
->where($where);
2976+
}
2977+
2978+
throw new InvalidParameterException("Invalid or missing primary key values.");
2979+
}
29202980

29212981
/**
29222982
* Retrieves a single matched record from the database based on the specified property name and value.

src/MagicObject.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,6 +2537,32 @@ public function size()
25372537
return $length;
25382538
}
25392539

2540+
/**
2541+
* Deletes a record from the database using its primary key value(s).
2542+
*
2543+
* This method delegates the delete operation to the persistence layer and ensures
2544+
* that the database connection is available before proceeding. It supports both
2545+
* single-column and composite primary keys.
2546+
*
2547+
* @param mixed $primaryKeyValue The value(s) of the primary key to identify the record.
2548+
* - For single-column primary key: a scalar value (e.g., 5).
2549+
* - For composite primary key: an **indexed array** of values
2550+
* in the same order as defined in the table (e.g., [1, 2]).
2551+
*
2552+
* @return int The number of deleted records (1 if successful, 0 if no matching record found).
2553+
*
2554+
* @throws NoDatabaseConnectionException If no database connection is available.
2555+
*/
2556+
public function deleteRecordByPrimaryKey($primaryKeyValue)
2557+
{
2558+
if ($this->_databaseConnected()) {
2559+
$persist = new PicoDatabasePersistence($this->_database, $this);
2560+
return $persist->deleteByPrimaryKey($primaryKeyValue);
2561+
} else {
2562+
throw new NoDatabaseConnectionException(self::MESSAGE_NO_DATABASE_CONNECTION);
2563+
}
2564+
}
2565+
25402566
/**
25412567
* Magic method called when a user calls any undefined method.
25422568
* The __call method checks the prefix of the called method and

0 commit comments

Comments
 (0)