Express entry values not saved

Hi!
I’m trying to add express entries programmatically, but I got stuck with this problem. Entry is created, but attribute values are not saving and entry appears empty.

$entry = Express::buildEntry('my-object')
  ->setContactName('Name')
  ->setContactEmail('email@email')
  ->save();

Somehow this does not set any values to the fields. The entry is created, but the attributes are empty.

I’ve checked that the attribute handles are correct. They are contact_name and contact_email.

Adding entries from the dashboard works fine, attributes are saved correctly.

I’ve also tried without the magic method like $entry->setAttribute(‘contact_name’, ‘Test’), but this also won’t work.

C5 version 8.5.4.

Any ideas what can I do to fix this? What am I missing? Thanks!

I’ll get some eyes on this for you.

Depending on the context of where you’re using this you might need to explicitly persist those changes.
try adding:

$em = Database::connection()->getEntityManager();
$em->persist($entry);
$em->flush();

Thank you! Unfortunately persist changes did not help and the attributes became empty again.

I’m using this in pagetype controller. I just have a feeling I’ve done something wrong, but can’t figure out what! I’ll explain more about the case…

I have a page template for a page type, where I’ve added a basic HTML form for users to submit their contact info. Whenever an admin creates this page type, there is event to create new Express object specific for this page. It works fine.

When the form is submitted, I’ve used ajax to call function in pagetype’s controller:

 $('#lead-form').submit(function(e) {
      e.preventDefault();
      $.ajax({
          type: "POST",
          url: '<?=$controller->action('submitLead', $cID)?>',
          data: $(this).serialize(),
          success: function(response){
              location.reload();
          }
      });
  });

I can tell ajax is working fine. On the controller I have this function:

public function submitLead($cID) {
      $page = Page::getByID($cID);
      $handle = $page->getCollectionID() . '-lead';

      if(Express::getObjectByHandle($handle) && isset($_POST)) {
        $name = $email = "";
        if(isset($_POST["name"])) { $name = $_POST["name"]; }
        if(isset($_POST["email"])) { $email = $_POST["email"]; }

        $entry = Express::buildEntry($handle)
        ->setContactName($name)
        ->setContactEmail($email)
        ->save();

        $em = Database::connection()->getEntityManager();
        $em->persist($entry);
        $em->flush();

        \Log::addDebug($name . " - " . $email);
      }

      // Add cookie to remember user has given contact info
      setcookie($cID . "-contact-given", true, time() + (86400 * 30), "/");
      return;  
    }

The Log::addDebug logs correctly POST data, so the values are fine.

And still, the entry gets created but the attributes name and email are empty.

Problem solved!

I figured out I was using same attribute handles for every express object that I created. They need to be individual handles, even if using them in different objects.

So the problem was not adding the entry, but when I created the object.

2 Likes

@mandakos The documentation doesn’t explicitly mention that attribute handles must be unique, but you can see in all of the examples that the handles are prefixed with the object handle.

I ran into this problem myself, but got blocked when creating the objects through the Dashboard. It warned me that the handle must be unique. How did you manage to make the attributes with the same handles?

1 Like

I was creating the objects programmatically in on_page_version_approve event. The object handle was created by page id to be unique. So at first I had this:

Events::addListener('on_page_version_approve', function($event) {
  $page = $event->getPageObject();
  $updatedPage = Page::getByID($page->getCollectionID());

  if ($updatedPage->getAttribute('add_form')) {
    $handle = $updatedPage->getCollectionID() . 'form';
    
    if(!Express::getObjectByHandle($handle)) {
      $handlePlural = $handle . 's';
      $name = $updated_page->getCollectionName() . " / " . $updated_page->getAttribute('live_date')->format("d.m.Y");

      $object = Express::buildObject($handle, $handlePlural, $name);
      $object->addAttribute('text', 'Nimi', 'contact_name');
      $object->addAttribute('text', 'Email', 'contact_email');
      $objectEntity = $object->save();

      $form = $object->buildForm('Yhteystiedot');
      $form->addFieldset('Tiedot')
          ->addAttributeKeyControl('contact_name')
          ->addAttributeKeyControl('contact_email')
      $form = $form->save();

      $entityManager = $object->getEntityManager();
      $objectEntity->setDefaultViewForm($form);
      $objectEntity->setDefaultEditForm($form);
      $entityManager->persist($objectEntity);
      $entityManager->flush();
    }
  }
});

And fixed this by adding the unique handle after every attribute handle:

      $object->addAttribute('text', 'Nimi', 'contact_name_' . $handle);
      $object->addAttribute('text', 'Email', 'contact_email_' . $handle);

I was following the documentation, but didn’t notice there was unique handles for every attribute. :grinning: I realized this after I tried to create object from the dashboard with these same attribute handles and it gave a warning as @Myq also said.

1 Like