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? :)

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Anthony
  • 375
  • 2
  • 10

1 Answers1

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);
Josh Angell
  • 2,311
  • 16
  • 23