Faster way to populate modx placeholders in a form from related objects?

142 Views Asked by At

I have a couple of related objects in a database that I need to update from the same form. It's a simple address object related to a contact object VIA 1:1 relation.

I can swear that when I was retrieving the object at one point I was setting all the placeholders in one $object->toArray() call, but that seems to not be the case anymore. [I am not sure what changed]

Here is what I am doing now, which does work:

$thisEntity = $this->modx->getObject('Entities', array('id' => $entity, 'token' => $token));

$entityData = $thisEntity->toArray();

// extra lines
$entityContacts = $thisEntity->EntityContact->toArray();

foreach($entityContacts as $key => $value){
    $entityData[$key] = $value;
}
// extra lines


$this->modx->setPlaceholders($entityData, 'fi.');

I'm sure at one point I wasn't using the extra lines and had the form populated, but not how.

Is there a faster easier way to populate the placeholder array [$entityData] from the Entities object and the EntityContact object in one step?

3

There are 3 best solutions below

0
On

I guess not. You can use array_merge for your example or create custom query which gives you summary data.

0
On

Can't give you a working example hence the lack of my experience, but:

  • play with toArray() options described here. (is your data lazy or related?)

  • dump the object after the first call and post the structure here so we can see and find out what happens.

  • this similar thread on modx forums may help too.

  • lastly the toArray() source is at github under blob -> master -> core -> xpdo -> om -> xpdoobject.class.php /cant post a third link :)/

0
On

You are close.

Using modUser as an example for the presentation of a single object:

$modx->toPlaceholders($modx->user->toArray(), 'user');

if you are retrieving a collection of information placeholders are a bad idea outside a chunk.

Instead:

$out='';
if ($collection){
    foreach ($collection as $object){

        if (is_object ($object) && $object instanceof someclass){
            $out .= $modx->getchunk('chunkName', $object);
        }
    }
}

return $out;

or

$modx->toPlaceholder($out, 'somename');