5

I have a section of type channel with a matrix field. I want to know how I would save the matrix content while I save the entry as well. I know I can save the entry as follows:

    $entry = new EntryModel();
    $content = array('productName' => $data[0]['Product'][0], 'productQuantities' => $data[0]['Product'][1]);
    $entry->sectionId = 10;
    $entry->typeId = 10;
    $entry->postDate   = date('Y-m-d');
    $entry->authorId   = 1;

    $result = $entry->setContentFromPost($content);

    $entry->getContent()->title = "Fancy Title";

    craft()->entries->saveEntry($entry);

The content variable above stores the content matrix block for this entry should have. So the Matrix has two fields within it one is of entry type and the other is numerical. I want to set these two fields before "craft()->entries->saveEntry($entry);". Please someone tell me how this is possible. I know for the entries type field I have to create a relation but please help me by letting me know how to do that as well as how to create this new matrix block containing that relation etc.

5

Ok there are two parts to this question - how to save relationships and how to save matrix blocks whilst saving an entry.

How to save relationships

To deal with the first, it is my understanding that the entries need to exist first so you may need to create them if they aren't already. Then all you need to do is pass an array of entry ids to the relevant field:

$entry->getContent()->entriesField = $targetEntryIds;

How to save matrix blocks whilst saving an entry

Bryan is correct in that you can create and save blocks all by themselves, however if all you want to do is add some blocks to a new entry then the following setup will work just fine:

$matrixData = array();

$matrixData['new1'] = array(
  'type' => 'blockType',
  'enabled' => true,
  'fields' => array(
    'fieldOne' => 'Some string or data',
    'fieldTwo' => '12'
  )
);

// You can repeat this part for each block you want to add, just increment the newN number.
$matrixData['new2'] = array(
  'type' => 'blockType',
  'enabled' => true,
  'fields' => array(
    'fieldOne' => 'Some more text',
    'fieldTwo' => '16'
  )
);

// set matrix field to contain those blocks before saving the entry
$entry->setContentFromPost(array('matrixField' => $matrixData));

This gist from Brandon explains all on the Matrix front, I am merely repeating most of it here for your benefit.

Best of luck!

  • When using this on an entry it adds the new block as expected but it will delete any old blocks that were present. How can we avoid this? – Radmation Mar 28 at 21:00
4

Matrix blocks are elements like entries themselves. There's an API for them even. You will want to load your matrix data into an array of MatrixBlockModels, then run craft()->matrix->saveBlock on each one. You will need the field id and type id for the block in question, which you can get from the Fields Service (you'll need to look this up in the Craft source code) and Matrix Service. You will also need the owner which can be set by running setOwner on the MatrixBlockModel and handing it the entry model.

It gets more involved, but this should give you a start.

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.