5

I'm trying to add entries to a Matrix block with one field type of 'entry' using the following:

$block = new MatrixBlockModel();
$block->fieldId    = 9;             // Matrix field's ID
$block->ownerId    = $collectionID; // ID of entry the block should be added to
$block->typeId     = 2;             // ID of block type
$block->getContent()->setAttributes(array(
    'contentRss' => array($entry->id)
));

$success = craft()->matrix->saveBlock($block);

The code saves the new block and entry, but when I view the entry it has not linked the entry. I notice in the database tables it has not created an entry in the craft_relations table.

Where am I going wrong? :)

7

Possibly a duplicate of this one here but hey, here is where I think you are going wrong:

Change this part:

$block->getContent()->setAttributes(array(
  'contentRss' => array($entry->id)
));

To this:

$block->getContent()->contentRss = array($entry->id);
2
  • No worries, did it work? Jul 29 '14 at 5:26
  • Excellent! In which case could you mark this answer as correct to better help out other people? Jul 30 '14 at 5:27

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.