|
9 | 9 | use MagicObject\Exceptions\EntityException; |
10 | 10 | use MagicObject\Exceptions\InvalidAnnotationException; |
11 | 11 | use MagicObject\Exceptions\InvalidFilterException; |
| 12 | +use MagicObject\Exceptions\InvalidParameterException; |
12 | 13 | use MagicObject\Exceptions\NoInsertableColumnException; |
13 | 14 | use MagicObject\Exceptions\NoColumnMatchException; |
14 | 15 | use MagicObject\Exceptions\NoDatabaseConnectionException; |
@@ -2917,6 +2918,65 @@ public function deleteBy($propertyName, $propertyValue) |
2917 | 2918 | throw new DataRetrievalException($e->getMessage()); |
2918 | 2919 | } |
2919 | 2920 | } |
| 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 | + } |
2920 | 2980 |
|
2921 | 2981 | /** |
2922 | 2982 | * Retrieves a single matched record from the database based on the specified property name and value. |
|
0 commit comments