@@ -75,6 +75,20 @@ public function setTableName($table_name) {
75
75
$ this ->tableName = $ table_name ;
76
76
}
77
77
78
+ /**
79
+ * @return string
80
+ **/
81
+ public function getPrimary () {
82
+ return $ this ->primary ;
83
+ }
84
+
85
+ /**
86
+ * @param string $primary
87
+ **/
88
+ public function setPrimary ($ primary ) {
89
+ $ this ->primary = $ primary ;
90
+ }
91
+
78
92
/**
79
93
* Constructs a RestfulDataProviderDbQuery object.
80
94
*
@@ -112,7 +126,9 @@ public function defaultSortInfo() {
112
126
$ sorts = array ();
113
127
foreach ($ this ->getIdColumn () as $ column ) {
114
128
if (!empty ($ this ->getPublicFields [$ column ])) {
129
+ // Sort by the first ID column that is a public field.
115
130
$ sorts [$ column ] = 'ASC ' ;
131
+ break ;
116
132
}
117
133
}
118
134
return $ sorts ;
@@ -230,7 +246,8 @@ public function getQueryCount() {
230
246
public function getTotalCount () {
231
247
return intval ($ this
232
248
->getQueryCount ()
233
- ->execute ());
249
+ ->execute ()
250
+ ->fetchField ());
234
251
}
235
252
236
253
/**
@@ -341,22 +358,20 @@ protected function replace($id) {
341
358
* {@inheritdoc}
342
359
*/
343
360
public function update ($ id , $ full_replace = FALSE ) {
344
- $ query = db_update ($ this ->getTableName ());
345
- foreach ($ this ->getIdColumn () as $ index => $ column ) {
346
- $ query ->condition ($ column , current ($ this ->getColumnFromIds (array ($ id ), $ index )));
347
- }
348
-
349
361
// Build the update array.
350
362
$ request = $ this ->getRequest ();
351
363
static ::cleanRequest ($ request );
352
364
$ save = FALSE ;
353
365
$ original_request = $ request ;
354
366
355
367
$ public_fields = $ this ->getPublicFields ();
356
- $ fields = array ();
368
+
369
+ $ id_columns = $ this ->getIdColumn ();
370
+
371
+ $ record = array ();
357
372
foreach ($ public_fields as $ public_field_name => $ info ) {
373
+ // Ignore passthrough public fields.
358
374
if (!empty ($ info ['create_or_update_passthrough ' ])) {
359
- // Allow passing the value in the request.
360
375
unset($ original_request [$ public_field_name ]);
361
376
continue ;
362
377
}
@@ -365,62 +380,67 @@ public function update($id, $full_replace = FALSE) {
365
380
if ($ this ->isPrimaryField ($ info ['property ' ])) {
366
381
continue ;
367
382
}
368
- // Check if the public property is set in the payload.
369
- if (!isset ($ request [$ public_field_name ])) {
370
- if ($ full_replace ) {
371
- $ fields [$ info ['property ' ]] = NULL ;
372
- }
383
+
384
+ if (isset ($ request [$ public_field_name ])) {
385
+ $ record [$ info ['property ' ]] = $ request [$ public_field_name ];
373
386
}
374
- else {
375
- $ fields [$ info ['property ' ]] = $ request [$ public_field_name ];
387
+ // For unset fields on full updates, pass NULL to drupal_write_record().
388
+ elseif ($ full_replace ) {
389
+ $ record [$ info ['property ' ]] = NULL ;
376
390
}
377
391
378
392
unset($ original_request [$ public_field_name ]);
379
393
$ save = TRUE ;
380
394
}
381
395
396
+ // No request was sent.
382
397
if (!$ save ) {
383
- // No request was sent.
384
398
throw new \RestfulBadRequestException ('No values were sent with the request. ' );
385
399
}
386
400
387
- if ( $ original_request ) {
388
- // Request had illegal values.
401
+ // If the original request is not empty, then illegal values are present.
402
+ if (! empty ( $ original_request )) {
389
403
$ error_message = format_plural (count ($ original_request ), 'Property @names is invalid. ' , 'Property @names are invalid. ' , array ('@names ' => implode (', ' , array_keys ($ original_request ))));
390
404
throw new \RestfulBadRequestException ($ error_message );
391
405
}
392
406
393
- // Once the update array is built, execute the query.
394
- $ query ->fields ($ fields )->execute ();
407
+ // Add the id column values into the record.
408
+ foreach ($ this ->getIdColumn () as $ index => $ column ) {
409
+ $ record [$ column ] = current ($ this ->getColumnFromIds (array ($ id ), $ index ));
410
+ }
411
+
412
+ // Once the record is built, write it.
413
+ if (!drupal_write_record ($ this ->getTableName (), $ record , $ id_columns )) {
414
+ throw new \RestfulServiceUnavailable ('Record could not be updated to the database. ' );
415
+ }
395
416
396
417
// Clear the rendered cache before calling the view method.
397
418
$ this ->clearRenderedCache (array (
398
419
'tb ' => $ this ->getTableName (),
399
420
'cl ' => implode (', ' , $ this ->getIdColumn ()),
400
421
'id ' => $ id ,
401
422
));
402
- return $ this ->view ($ id , TRUE );
423
+
424
+ return $ this ->view ($ id );
403
425
}
404
426
405
427
/**
406
428
* {@inheritdoc}
407
429
*/
408
430
public function create () {
409
- $ query = db_insert ($ this ->getTableName ());
410
-
411
- // Build the update array.
412
431
$ request = $ this ->getRequest ();
413
432
static ::cleanRequest ($ request );
414
433
$ save = FALSE ;
415
434
$ original_request = $ request ;
416
435
417
436
$ public_fields = $ this ->getPublicFields ();
418
- $ fields = array ();
419
- $ id_values = array_fill (0 , count ($ this ->getIdColumn ()), FALSE );
437
+ $ id_columns = $ this ->getIdColumn ();
420
438
439
+
440
+ $ record = array ();
421
441
foreach ($ public_fields as $ public_field_name => $ info ) {
442
+ // Ignore passthrough public fields.
422
443
if (!empty ($ info ['create_or_update_passthrough ' ])) {
423
- // Allow passing the value in the request.
424
444
unset($ original_request [$ public_field_name ]);
425
445
continue ;
426
446
}
@@ -431,46 +451,38 @@ public function create() {
431
451
continue ;
432
452
}
433
453
434
- // Check if the public property is set in the payload.
435
- if (($ index = array_search ($ info ['property ' ], $ this ->getIdColumn ())) !== FALSE ) {
436
- $ id_values [$ index ] = $ request [$ public_field_name ];
437
- }
438
-
439
454
if (isset ($ request [$ public_field_name ])) {
440
- $ fields [$ info ['property ' ]] = $ request [$ public_field_name ];
455
+ $ record [$ info ['property ' ]] = $ request [$ public_field_name ];
441
456
}
442
457
443
458
unset($ original_request [$ public_field_name ]);
444
459
$ save = TRUE ;
445
460
}
446
461
462
+ // No request was sent.
447
463
if (!$ save ) {
448
- // No request was sent.
449
464
throw new \RestfulBadRequestException ('No values were sent with the request. ' );
450
465
}
451
466
452
- if ( $ original_request ) {
453
- // Request had illegal values.
467
+ // If the original request is not empty, then illegal values are present.
468
+ if (! empty ( $ original_request )) {
454
469
$ error_message = format_plural (count ($ original_request ), 'Property @names is invalid. ' , 'Property @names are invalid. ' , array ('@names ' => implode (', ' , array_keys ($ original_request ))));
455
470
throw new \RestfulBadRequestException ($ error_message );
456
471
}
457
472
458
- $ passed_id = NULL ;
459
-
460
- // If we have the full primary key passed use it.
461
- if (count (array_filter ($ id_values )) == count ($ id_values )) {
462
- $ passed_id = implode (self ::COLUMN_IDS_SEPARATOR , $ id_values );
463
- }
473
+ // Once the record is built, write it and view it.
474
+ if (drupal_write_record ($ this ->getTableName (), $ record )) {
475
+ // Handle multiple id columns.
476
+ $ id_values = array ();
477
+ foreach ($ id_columns as $ id_column ) {
478
+ $ id_values [$ id_column ] = $ record [$ id_column ];
479
+ }
480
+ $ id = implode (self ::COLUMN_IDS_SEPARATOR , $ id_values );
464
481
465
- // Once the update array is built, execute the query.
466
- if ($ id = $ query ->fields ($ fields )->execute ()) {
467
- return $ this ->view ($ id , TRUE );
482
+ return $ this ->view ($ id );
468
483
}
484
+ return ;
469
485
470
- // Some times db_insert() does not know how to get the ID.
471
- if ($ passed_id ) {
472
- return $ this ->view ($ passed_id );
473
- }
474
486
}
475
487
476
488
/**
0 commit comments