update
This commit is contained in:
@@ -21,6 +21,38 @@ class Products
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bezpieczne wstawienie/odczyt produktu po (client_id, offer_id).
|
||||
* Wymaga UNIQUE KEY uk_products_client_offer (zob. migracja 031).
|
||||
* Zwraca product_id istniejacego lub nowo wstawionego wiersza.
|
||||
* Odporne na race condition: przy bledzie 1062 (Duplicate entry) robi SELECT.
|
||||
*/
|
||||
static public function ensure_product( $client_id, $offer_id, array $insert_data = [] )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$client_id = (int) $client_id;
|
||||
$offer_id = trim( (string) $offer_id );
|
||||
if ( $client_id <= 0 || $offer_id === '' ) return 0;
|
||||
|
||||
$existing_id = (int) $mdb -> get( 'products', 'id', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer_id ] ] );
|
||||
if ( $existing_id > 0 ) return $existing_id;
|
||||
|
||||
$row = array_merge( $insert_data, [ 'client_id' => $client_id, 'offer_id' => $offer_id ] );
|
||||
try
|
||||
{
|
||||
$mdb -> insert( 'products', $row );
|
||||
$new_id = (int) $mdb -> id();
|
||||
if ( $new_id > 0 ) return $new_id;
|
||||
}
|
||||
catch ( \PDOException $e )
|
||||
{
|
||||
if ( strpos( (string) $e -> getMessage(), '1062' ) === false ) throw $e;
|
||||
}
|
||||
|
||||
return (int) $mdb -> get( 'products', 'id', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer_id ] ] );
|
||||
}
|
||||
|
||||
static public function delete_products( $product_ids ) {
|
||||
global $mdb;
|
||||
if ( empty( $product_ids ) || !is_array( $product_ids ) ) {
|
||||
|
||||
Reference in New Issue
Block a user