Programatically Adding an Association Key to an Express Entity Form

Hoping this is a quick and easy reply for someone. I am creating a package that builds express entities and associations between them. When creating the express entity’s form, I can’t for the life of me figure out how to add an association key.

This is what I have so far…

$eName = 'Schedule Event';
$eHandle = 'schedule_event';
$eHandlePlural = 'schedule_events';
$object = Express::getObjectByHandle($eHandle);
if (!is_object($object)) {
   $object = Express::buildObject($eHandle, $eHandlePlural, $eName, $pkg);
   
   $attrName = 'Event Title';
   $attrHandle = 'eventtitle';
   $object->addAttribute('text', $attrName, $attrHandle);
			
   $objectEntity = $object->save();
}

$eName = 'Schedule Event Session';
$eHandle = 'schedule_event_session';
$eHandlePlural = 'schedule_event_sessions';
$objectSub = Express::getObjectByHandle($eHandle);
if (!is_object($objectSub)) {
   $objectSub = Express::buildObject($eHandle, $eHandlePlural, $eName, $pkg);

   $attrName = 'Schedule Event Session Title';
   $attrHandle = 'scheduleeventsessiontitle';
   $objectSub->addAttribute('text', $attrName, $attrHandle);

   $objectSubEntity = $objectSub->save();
}

$eName = 'Schedule Speaker';
$eHandle = 'schedule_speaker';
$eHandlePlural = 'schedule_speakers';
$objectSp = Express::getObjectByHandle($eHandle);
if (!is_object($objectSp)) {
   $objectSp = Express::buildObject($eHandle, $eHandlePlural, $eName, $pkg);

   $attrName = 'Speaker Name';
   $attrHandle = 'speakername';
   $objectSp->addAttribute('text', $attrName, $attrHandle);

   $objectSpEntity = $objectSp->save();
}

if ($object && $objectSub && $objectSp) {
   $object->buildAssociation()->addOneToMany($objectSub, 'schedule_event_session')->save();
   $object->buildAssociation()->addManyToMany($objectSp, 'speakers')->save();
}

$form = $object->buildForm('Form');
$form->addFieldset('Basics')
    ->addAttributeKeyControl('eventtitle')
	->addAssociationControl($object->getAssociation('speakers'));
$form = $form->save();

Any thoughts?

I think I dumbed on the answer…

not

->addAssociationControl($object->getAssociation(‘speakers’));

but

->addAssociationControl(‘speakers’);

I’m not sure where I got the former from but hopefully this will shorten someone’s google adventure in the future.

Not a direct answer to your question but I’m wondering if you might be better served going a different route.

Why not create the entities, the forms and everything you need from the dashboard then use the Concrete migration tool to export it all as XML and finally use the core importer from your package to install it wherever you want.

Just some food for thoughts.

Thanks for the reply but I have no idea what that all means :slight_smile: I pretty much follow the tutorials and embellish from there. Which is sometimes difficult. For example, I used this to find how to create the entities and how to create the associations

but I wish it was a consistent example. In other words, not switch from the boats/marinas example to a worker/skill example, a little students/teachers and back to the boats/marinas.

This is always a great reference but often lacks certain details.

There is a great list of how to add express attributes and their options but nothing about how to set the options for the associations. And nothing about adding key controls to an express form (or their options).

Sorry for the rant :slight_smile:

You’re right, I should have been more precise.

Concrete has a migration tool that is helpful when migrating from one version to another, but also when simply migrating data from one site to another.

It lets you select what you want to migrate, like users, pages, attributes…. And Express Entities as well as Entries.

You can decide to migrate existing Express entries. The migration tool will then generate an XML file that contains all the information to recreate your Express entity.

So, say you create your entity from the dashboard, you then export it as an XML file, you add that file to your package, add 1 line of code to your package’s install routine, and that’s it. The Express entity will be created whenever you install the package.

So, say you add the XML called express_entities.xml in a folder called config in your package, you can add this code to your package’s controller:

public function install()
{
    parent::install();
    $this->installContentFile('config/express_entities.xml');
}

And that’s it.
Much easier than creating the entity programmatically.

Here’s the tutorial you need: Using the Concrete Migration Tool Addon :: Concrete CMS

Good luck and reach out again if anything is not clear.

Ahhh! now I see what you mean. That is fantastic for future reference.

Thank you