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

1 Answer 1

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? Commented Jul 29, 2014 at 5:26
  • Excellent! In which case could you mark this answer as correct to better help out other people? Commented Jul 30, 2014 at 5:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.