5

I have been trying two separate methods to accomplish and both keep resulting in the same errors no matter what I try.

The entry I am saving has a few fields the last of which is a matrix block (I am able to save the entry fine when not trying to save the matrix field to it.). This matrix block has two entry field types and the last field is a table field. I do not need to save actual data to these fields; it's fine that they are null/empty. I made sure that in the matrix block settings for these fields are not marked as required.

I am testing this via ajax calls in jQuery.

First Method (described here): Craft sends back an error stating that...

Argument 1 passed to Craft\MatrixService::validateBlock() must be an instance
of Craft\MatrixBlockModel, array given, called in 
/path/to/project/app/fieldtypes/MatrixFieldType.php 
on line 393 and defined

It appears to me that craft is expecting a matrix block model not an array. But even Brandon Kelly shows an example here of this method working...so what am I doing wrong..?

Here's my code:

private function _buildEntry ($college, $now, &$data) {
    $entry = new EntryModel();
    $matrixData = array();
    $entry->sectionId = 4;
    $entry->authorId = 1;
    $entry->typeId = 1;
    $entry->enabled = 1;
    $entry->getContent()->title = $college->collegeName." ".$now;
    $entry->getContent()->timeEntryStartDate = $now;
    $entry->getContent()->timeEntryCollege = array($college->id);

    //set up the matrix data
    $matrixData['new1'] = array(
        'type'      => 'weekBlock',
        'enabled'   => true
    );

    $entry->getContent()->timeEntryWeek = $matrixData;

    //save the new entry
    $success = craft()->entries->saveEntry($entry);

    if(!$success){
        Craft::log('Could not save the entry "'.$entry->title.'"', LogLevel::Error);
    } else {
        Craft::log('Saved the entry! '.$entry->title.'"', LogLevel::Error);
    }
}

Second Method (described here): Craft sends back an error stating that...

Trying to get property of non-object

My code for this second part which I call after craft successfully saves the entry (which works fine)

public function actionTest($id = 3) {

    $block = new MatrixBlockModel();

    $block->fieldId    = 20; // Matrix field's ID
    $block->ownerId    = 178; // ID of entry the block should be added to
    $block->typeId     = 6;
    $block->getContent()->setAttributes(array(
        'timeEntryHouse' => array(183),
        'timeEntryEmployee' => array(1),
        'timeEntryDays' => null
    ));

    $success = craft()->matrix->saveBlock($block, false);
    if (!$success)
    {
        Craft::log('Couldn’t save the entry "'.$id.'"', LogLevel::Error);
    } else {
        $this->returnJson('worked');
    }
}
3

ok just found out the answer to the second method. I was using the incorrect typeId apparently. I wasn't able to find this in any of the error logs, so this was just luck.

To make sure you get the correct typeId do so in this way:

$matrix = craft()->fields->getFieldByHandle("fieldHandle"); //look in craft_fields table
$matrixBlocks = craft()->matrix->getBlockTypesByFieldId($matrix->id);

You can iterate through $matrixBlocks and look at each blocks id but in my case I knew there was only one block so I just accessed the id like this

$typeId = $matrixBlocks[0]['id'];

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.