What is the expected data type to be returned when using the prepValue() method? So while building a field type (FT), what is the expected data type of the returned value from the prepValue method? I am not sure I am using this method right, as I am running into a PHP error when trying to save the FT.

My FT stores data in two ways, the first as a JSON string stored in the element content table. When my FT is saved, I used the onAfterElementSave() method for additional processing. During this method, I am creating rows in a db table created by my plugin. What I desire to happen is when I call my field from the template on the front-end, it returns a data model which I can then pass to other methods for further manipulation (in the template). All this works great.

Here is my prepValue() code in my FT. 

public function prepValue($value)
{
    $value = json_decode($value);

    return new MyPlugin_MyModelNameModel((array) $value);
}


All this works fine until I go to save my entry, which is producing this error "Internal Server Error - Object of class stdClass could not be converted to string". When I return the data from the prepValue() method as a string, it works fine when saving but then break in the template because it's expecting to be a model.

I even added a __toString() method to my model in hopes that if the data needs to be a string, my model could dynamically pass the JSON back as a string but that isn't working either.

Here is the stack trace from the craft.log

2014/08/14 19:28:35 [error] [php] Object of class stdClass could not be converted to string (/Users/justinkimbrell/Sites/craft_dev/craft/app/helpers/StringHelper.php:61)
Stack trace:
#0 /Users/justinkimbrell/Sites/craft_dev/craft/app/fieldtypes/BaseFieldType.php(148): arrayToString()
#1 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/ContentService.php(366): Craft\GoogleMaps_GoogleMapFieldType-&gt;getSearchKeywords()
#2 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/ContentService.php(137): Craft\ContentService-&gt;_updateSearchIndexes()
#3 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/ElementsService.php(898): Craft\ContentService-&gt;saveContent()
#4 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/EntriesService.php(141): Craft\ElementsService-&gt;saveElement()
#5 /Users/justinkimbrell/Sites/craft_dev/craft/app/controllers/EntriesController.php(440): Craft\EntriesService-&gt;saveEntry()
#6 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/actions/CInlineAction.php(49): Craft\EntriesController-&gt;actionSaveEntry()
#7 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CController.php(308): CInlineAction-&gt;runWithParams()
#8 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CController.php(286): Craft\EntriesController-&gt;runAction()
#9 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CController.php(265): Craft\EntriesController-&gt;runActionWithFilters()
#10 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CWebApplication.php(282): Craft\EntriesController-&gt;run()
#11 /Users/justinkimbrell/Sites/craft_dev/craft/app/etc/web/WebApp.php(865): Craft\WebApp-&gt;runController()
#12 /Users/justinkimbrell/Sites/craft_dev/craft/app/etc/web/WebApp.php(254): Craft\WebApp-&gt;_processActionRequest()
#13 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/base/CApplication.php(180): Craft\WebApp-&gt;processRequest()
#14 /Users/justinkimbrell/Sites/craft_dev/craft/app/index.php(196): Craft\WebApp-&gt;run()
#15 /Users/justinkimbrell/Sites/craft_dev/public/index.php(14): require_once()
REQUEST_URI=/index.php/admin/entries/news/47

******************************************************************************************************


Hopefully the issue is clear. I am just unsure of how to properly use this method, and wondering how I can return data in a model and not as a string.
Looks like the error is occurring when your field type’s getSearchKeywords() function is called. The value that’s passed into that function will have already been run through prepValue(), so at that point it looks like it will be a MyPlugin_MyModelNameModel instance (whatever the actual class name is).

If your field type doesn’t override getSearchKeywords(), the BaseFieldType version will just pass the value off to StringHelper::arrayToString(), which will do its best to convert whatever variable it was passed into a string. Something in there must be tripping it up.

Your best bet here is probably to just override getSearchKeywords() in your field type. It should be returning a space-delimited list of keywords you want to be stored for the current field value.