I'm using a custom plugin with a service and controller to save relational fields to entries. I'm able to do this just fine, but when it saves an entry, it sets the author as NULL in the database and it no longer shows in the author's list of authored entries.

I've tried to both not set the author at all and to manually set the author to the previous author. Craft gives me an Internal Service Error saying "Property 'Craft\EntryModel.author' is read only." when trying to set the author in the code. Removing the retrieval and setting of the author saves the entry but marks the author as NULL

Has anyone else faced this? Is it a bug?

For reference, here's the heart of this service's code:

    $criteria3 = craft()->elements->getCriteria(ElementType::Entry);
    $criteria3->id = $kitId;
    $currentKit = $criteria3->first();
    $currentAuthor = $currentKit->author;

    $kit = new EntryModel();
    $kit->sectionId = '6';
    $kit->id = $kitId;

    $kit->author = $currentAuthor;
    $success = craft()->entries->saveEntry($kit);
  • 1
    Perhaps try either $currentKit->authorId to get the attribute from the Entry record, or if that fails try $currentKit->author->first(). Feb 18 '16 at 8:52
  • Unfortunately no luck with either of those. The only other additional thought I have is that when you use the save entries form on the frontend to edit entries, the author is maintained. I'll have to look at Craft's service for that and see what they do.
    – Natalie
    Feb 18 '16 at 15:21

Josh Angell's suggestion did work, I was just testing it on an entry that already had been updated with a NULL author. (Thanks Josh!)

For the record, I used $currentAuthor = $currentKit->authorId; to get my previous author, and then set the author with $kit->authorId = $currentAuthor. This does seem to maintain the author entirely.

  • Excellent! Glad it all worked out :) Feb 18 '16 at 17:20

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.