If you don't mind using json_encode, try using the object's toArray() with arguments:
use Map\AuditTableMap as TableMap;
$something = new Something();
In other words, use the argument <ObjectName>Map::TYPE_FIELDNAME to output an array with column names.
The docs are amazing, but they're quite confusing to navigate. I found the following comment from one of the generated models in my project. This is for version 2.0@dev, which I'm using; note that it may differ in your version. (I'd suggest looking at the docs for more formal guidance, but you can take a peek at your models too.)
* Exports the object as an array.
* You can specify the key type of the array by passing one of the class
* type constants.
* @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME,
* TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM.
* Defaults to TableMap::TYPE_PHPNAME.
* @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE.
* @param array $alreadyDumpedObjects List of objects to skip to avoid recursion
* @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE.
* @return array an associative array containing the field names (as keys) and field values
If you only want to strictly use the toJSON call, then you'll have to do some post-processing manipulation of the string, because the only option allowed with the toJSON method is to include or not include lazy-loaded columns.
One caveat to this code is that if the value matches one of your column names exactly, it will be replaced. The only way to eliminate this is to json_decode the JSON object and only replace the keys, but if you don't want to use json_encode, I don't suppose you'd want to use json_decode.
@Cezille07's answer is the most correct in this case. My answer is mainly to show how TableMap/ColumnMaps can be used for post-processing, which is something I didn't know about when I started with Propel.
Asked in February 2016Viewed 3,899 timesVoted 13Answered 2 times