My 'party' entity doesn't actually need any properties:

$p = array();
$p = (object) $p;
entity_save('party', $p);
dsm($p);

The above code appears to fail.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

klausi’s picture

Status: Active » Postponed (maintainer needs more info)

What is the hook_entity_info() implementation for your party entity? Do you implement a controller class that takes care of the saving?

joachim’s picture

So far I'm using the built-in one the docs recommend.

fago’s picture

Status: Postponed (maintainer needs more info) » Fixed

I guess the problem is an exception, you are not seeing due to a bug. See #973116: drupal_write_record() fails when saving new entity. If not, please re-open.

joachim’s picture

Status: Fixed » Active

This is still producing no result in the DB and an empty message with code updated about an hour ago.

(Not that it matters all that much if the status property is required; that means all entities need at least one key!)

joachim’s picture

Oh, misread the other bug's resolution -- status is not required :)

fago’s picture

Status: Active » Fixed

great :)

joachim’s picture

Status: Fixed » Active

No, that means this is still active!

If status WERE required, then you'd always have at least one property. But that's not the case.

fago’s picture

Category: bug » support

!? Sry, I don't get what's the issue here? You want to be able to build entity types without properties?

joachim’s picture

Yup!

klausi’s picture

Why? You want to have entities that do not contain any data? Hm, then the only reasonable information that you store is the amount of entities. You can implement a counter more easily, or do I miss something?

joachim’s picture

Category: support » bug

Party module.

A party is a groupings of profile entities that do not need to have a uid.

So far, I've no need for any data on a party apart from its ID. I still need an admin UI to list them and give links to some (yet to be built) page that shows you a single party by (somehow) grouping together its profile entities.

... Then there's the party edit page. No idea how the form for that's going to work :/

klausi’s picture

Then it should be sufficient to just create an empty party entity and pass it to the edit form where the actual input/selection of profiles happens. No need to save() beforehand.

For the edit page: you will have to implement something like (assuming 'party' is the machine name of your entity type)

function party_form($form, &$form_state, $party, $op = 'edit') { }

for an example see wsclient_ui.inc

joachim’s picture

> Then it should be sufficient to just create an empty party entity and pass it to the edit form where the actual input/selection of profiles happens. No need to save() beforehand.

The edit form is likely to be pretty hairy because it's going to have to solve the problem of multiple entity forms within one form.

Also, even once those profile entities are created, there's still no properties to save in the party!

fago’s picture

>Also, even once those profile entities are created, there's still no properties to save in the party!
I guess you'll need to store references on profiles, thus profile ids? Or are you using a field for that?

Still you'll need at least an entity identifier - have you defined that in your hook_schema()? If so, are you getting an error message when saving?

joachim’s picture

I'm not sure how I'll store them yet -- at least at the moment, probably in a custom table. At CPH some sort of entity relationship managing API was raised as an idea.

> Still you'll need at least an entity identifier - have you defined that in your hook_schema()? If so, are you getting an error message when saving?

Yup, there's a party id. But when saving a new one, that's not set. So it's just an empty array.

fago’s picture

Can I find the code somewhere, so I'd have a look.

fago’s picture

Status: Active » Postponed (maintainer needs more info)
joachim’s picture

Yup, will post the code soon... I'd lost my github password :/

Damien Tournoud’s picture

Title: saving an empty entity doesn't work » drupal_write_records() cannot save an empty (full default) row (was: saving an empty entity doesn't work)
Project: Entity API » Drupal core
Version: 7.x-1.x-dev » 8.x-dev
Component: Entity CRUD API - main » database system
Status: Postponed (maintainer needs more info) » Active

This is a core bug. drupal_write_records() cowardly refuses to save an empty (ie. only defaults) row.

jbrown’s picture

Title: drupal_write_records() cannot save an empty (full default) row (was: saving an empty entity doesn't work) » drupal_write_record() cannot save an empty (full default) row (was: saving an empty entity doesn't work)

There is no function called drupal_write_records().

jbrown’s picture

Status: Active » Needs review
FileSize
461 bytes
Damien Tournoud’s picture

We probably need a ->useDefaults() here.

jbrown’s picture

Status: Needs review » Needs work

I'm on it!

jbrown’s picture

Status: Needs work » Needs review
FileSize
1.39 KB
jbrown’s picture

#24: write_empty_row.patch queued for re-testing.

jbrown’s picture

#24: write_empty_row.patch queued for re-testing.

jbrown’s picture

#24: write_empty_row.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, write_empty_row.patch, failed testing.

jbrown’s picture

Status: Needs work » Needs review
FileSize
2.26 KB
898 bytes
joachim’s picture

Status: Needs review » Needs work

I applied this to 7.x (my 8.x needs reinstalling...) and successfully wrote an empty entity to the database.

Nearly RTBC, just a formatting nitpick:

+++ b/core/modules/simpletest/tests/common.test
@@ -1962,6 +1962,11 @@ class DrupalDataApiTest extends DrupalWebTestCase {
+    // Insert a record with no columns populated

Comment is missing a full stop.

+++ b/core/includes/common.inc
@@ -6923,6 +6924,7 @@ function drupal_write_record($table, &$record, $primary_keys = array()) {
+      $default_fields[] = $field;
       continue;

@@ -6973,7 +6971,10 @@ function drupal_write_record($table, &$record, $primary_keys = array()) {
+      ->useDefaults($default_fields);

While we're at it, I think this could do with a comment to explain it, especially as finding docs on useDefaults() seems a bit tricky.

6 days to next Drupal core point release.

jbrown’s picture

Status: Needs work » Needs review
FileSize
2.49 KB
2.54 KB

useDefaults() docs are readily available on api.drupal.org ;-)
http://api.drupal.org/api/drupal/core--includes--database--query.inc/fun...

joachim’s picture

Status: Needs review » Reviewed & tested by the community

Took me about six google searches to find them though! :/

Tests still pass, comments are good. RTBC :)

catch’s picture

Status: Reviewed & tested by the community » Needs review

Looks good to me and nice test addition. Committed/pushed to 8.x, moving back to 7.x. Please re-upload the 7.x patch so the test bot can run it.

catch’s picture

Version: 8.x-dev » 7.x-dev
jbrown’s picture

FileSize
2.49 KB
jbrown’s picture

FileSize
2.49 KB

Try again...

tim.plunkett’s picture

Issue tags: +Needs backport to D7

Fixing tag.

joachim’s picture

#36: write-empty-row.patch queued for re-testing.

joachim’s picture

Status: Needs review » Reviewed & tested by the community

Tests pass, setting RTBC.

jbrown’s picture

Wow - that's an oldie!

David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs review

Hm, this makes sense but I'm not sure about it for Drupal 7. Trying this:

$empty = array();
drupal_write_record('users', $empty);

Before the patch it's a no-op. After the patch it's a PDO exception. Granted that's a mistake in your code, but maybe we want to find a kinder way to indicate it in Drupal 7?

The patch also changes the NULL return value in case of a no-op, although I guess that was never documented.

David_Rothstein’s picture

-  if (empty($fields)) {
-    return;
-  }
......
   else {
+    // Create an UPDATE query.
     $query = db_update($table)->fields($fields);

Won't this also fail with an error if $fields is empty? (I noticed the patch has no test coverage for the update case, only the insert case.)

This part might affect Drupal 8 too.

mgifford’s picture

Version: 7.x-dev » 8.x-dev
Issue summary: View changes

Looking for D8 feedback on "Won't this also fail with an error if $fields is empty?" from @David_Rothstein in #42.

We should be sure this is fixed in D8 before back-porting it to D7.

@David_Rothstein - I also don't know what "kinder way to indicate" this we are available. I think it would be better than what we have now.

jhedstrom’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

If this is still an issue in 8, the patch from 3 years ago definitely needs reroll.

jbrown’s picture

Version: 8.0.x-dev » 7.x-dev

"drupal_write_record() and drupal_schema_fields_sql() removed in favor of merge queries and entity API"
https://www.drupal.org/node/2340291

chx’s picture

Aye but Insert::preExecute will silently quit (in D8 too) if you make a query that uses only defaults, check the last return statement, it's missing a defaultFields check.

Edit: amateescu found this.

  • catch committed 51dd3e2 on 8.3.x
    Issue #970338 by jbrown, joachim: Fixed drupal_write_record() cannot...

  • catch committed 51dd3e2 on 8.3.x
    Issue #970338 by jbrown, joachim: Fixed drupal_write_record() cannot...

  • catch committed 51dd3e2 on 8.4.x
    Issue #970338 by jbrown, joachim: Fixed drupal_write_record() cannot...

  • catch committed 51dd3e2 on 8.4.x
    Issue #970338 by jbrown, joachim: Fixed drupal_write_record() cannot...

  • catch committed 51dd3e2 on 9.1.x
    Issue #970338 by jbrown, joachim: Fixed drupal_write_record() cannot...
daffie’s picture

Status: Needs work » Fixed
daffie’s picture

Status: Fixed » Closed (fixed)