1

When I am creating a new field (and I've tried plain text, redactor, supertable and matrix field) and associating it with an existing entry type (using the Control Panel to do so), I run into an issue where I can't load data to the field with a migration.

Here's code from the migration I might try to run to put some data in this field (in this case, it is a redactor).

  foreach ($attorneys as $attorney) { // iterate over json data we are going to import.
    $craft_attorney_id = // lookup attorney id
    $entry = Entry::find()->id($craft_attorney_id)->status(['enabled','disabled'])->one();
    if (!$entry) {
      continue;
    }
    $accolades = 'test text';
    $entry->setFieldValues(array('accolades' => $accolades));
    if(!\Craft::$app->elements->saveElement($entry)) {
        throw new \Exception("Couldn't save entry: " . print_r($entry->getErrors(), true));
    }
  }

When I view one entry in the CP and enter data in the new field, and then rerun the migration (take it down then up), the new data from the migration is saved just fine (for that element. but not for any others).

I've set the redactor field to 'full html', and also stripped the tags from the field to be inserted.

Our content table has 56 columns, but many are text, so I don't think I'm running into an issue with row length ( https://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html#innodb-maximums-minimums )

Tried clearing all the caches.

Tried upgrading from 3.0.9 to 3.0.27.

Also tried to refresh fields on the fields service: \Craft::$app->fields->refreshFields(); at the beginning of the migraiton.

If I try to read from the entry in the same migration, I get this exception

Invalid field handle: accolades (/.../vendor/craftcms/cms/src/base/Element.php:1932)
#0 /../vendor/craftcms/cms/src/base/Element.php(1612): craft\base\Element->normalizeFieldValue('accolades')
#1 /../migrations/m181016_224151_import_accolades_v2.php(59): craft\base\Element->getFieldValue('accolades')
#2 /../vendor/craftcms/cms/src/db/Migration.php(56): craft\contentmigrations\m181016_224151_import_accolades_v2->safeUp()

Except I can find that field using the fieldService. Maybe it is something to do with the normalization?

Am I saving the entry incorrectly? Do I need to do some kind of special call to refresh the element mapping of fields?

Thanks!

  • Could you please explain the first two lines of your code? I'm not sure if you have a creative way to fetch your entries of if you don't know what's happening exactly – Robin Schambach Oct 21 '18 at 19:50
  • Hi @RobinSchambach, sorry, I should have explained that. I'm just pulling from a json file (previously parsed) and looking up the existing entry id by the name that is pulled from the json file. I will update the code sample. – mooreds Oct 22 '18 at 14:32
  • The "problem" I see is: if it doesn't find a valid id the value might be null or 0 and if that's the case Craft will ignore the parameter and instead of returning null as well it will return the first found Entry that might not have the field in it's layout. Can you really make sure every title is 100% bound to an entry id? – Robin Schambach Oct 22 '18 at 14:47
  • If we don't see an entry, we continue. At least that is the intention, so that we always have a valid entry to associate the data with. I thought the if (!$entry) { line did that. Or am I missing your point? – mooreds Oct 23 '18 at 17:31
  • My point is you should check if your id is valid as well. If id is null Craft will return the first entry it can find that might have a different layout because it can be in a different section – Robin Schambach Oct 23 '18 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.