2

I'm having problems saving my custom entity to the db. I created a custom entity called 'review' using hook_entity_info() with a custom controller. I have specified some parameters in the controller using:

      public function create(array $values = array()) {
      global $user;
      $values = array(
      'rid' => '',
      'uid' => $user->uid,
      'nid' => $_GET['nid'],
      'created' => REQUEST_TIME,
      'changed' => REQUEST_TIME,
      'published' => 1,
      'quality' => '',
      'design' => '',
      'versatility' => '',
      'design' => '',
   );
   return parent::create($values);
 }

Everything seems to be working just fine but when I try to submit my form to create a new entity not all fields are saved in the db.

I attached the value of the fields in the form using field_attach_form('review', $review, $form, $form_state); and in my submit function I have this:

$review_submission = (object) $form_state['values'];
field_attach_submit('review', $review_submission, $form, $form_state);
$review = entity_create('review', array('type' => 'review'));
$entity = entity_metadata_wrapper('review',  $review);
$entity->save($review_submission); //This saves info but not fields

This creates a new entity but the fields 'quality', 'design', 'versatility', 'relevance' are not saved. However if I use this instead in my submit function:

review_submission = (object) $form_state['values'];
field_attach_submit('review', $review_submission, $form, $form_state);
$review = entity_create('review', array('type' => 'review'));
$entity = entity_metadata_wrapper('review',  $review);
entity_get_controller('review')->save($review_submission); //This saves fields but not info

those fields are correctly saved BUT then 'rid', 'uid', 'nid', 'created', 'changed', 'published' are not saved.

What am I missing? How can I saved the entity info and the values from the form fields?

0

As far as I understand, you need to provide these values manually before the save, for example:

$entity->created = isset($entity->is_new) ? REQUEST_TIME : $entity->created;
$entity->changed = REQUEST_TIME;
$entity->uid = $user->uid;

or copy them directly from $form_state['values'].

I've found entityFormSubmit method from Inline Entity Form module which is manually copying all fields which are not fields to entity properties, so gave me the idea for this code:

$form_id = $form_state['build_info']['form_id'];
$entity = $form['#entity'] ?: $form_state['build_info']['args'][0];
$entity_type = $entity->entityType();
$entity_info = entity_get_info($entity_type);
$entity_values = drupal_array_get_nested_value($form_state['values'], $form['#parents']);
list(, , $bundle) = entity_extract_ids($entity_type, $entity);

$values_excluding_fields = $info['fieldable'] ? array_diff_key($entity_values, field_info_instances($this->entityType, $bundle)) : $entity_values;
foreach ($values_excluding_fields as $key => $value) {
  $entity->$key = $value;
} 
if ($entity_info['fieldable']) {
  field_attach_submit($entity_type, $entity, $form, $form_state);
}
$entity->created = isset($entity->is_new) ? REQUEST_TIME : $entity->created;
$entity->changed = REQUEST_TIME;
entity_save($entity_type, $entity);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.