Cethy February 2016

Using column names when generating JSON in Propel

When using toJSON() on an ObjectCollection the names of the properties are always based on the PHP-names. For instance:

For the column type_name the JSON property becomes TypeName, etc.

Is there a way to make Propel use the name of the field/column instead?

Answers


Cezille07 March 2016

If you don't mind using json_encode, try using the object's toArray() with arguments:

use Map\AuditTableMap as TableMap;

$something = new Something();
$something->setSomeColumnValue("value");
echo json_encode($something->toArray(SomethingMap::TYPE_FIELDNAME));

Output:

{"some_column_value": "value"}

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
 */


NobleUplift March 2016

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.

$something = new Something();
$something->setSomeColumnValue("value");
$json = $something->toJSON();

$tableMap = \Propel::getDatabaseMap()->getTableMap('Something');
$columnMaps = $tableMap->getColumns();
$phpNames = array();
$columnNames = array();
foreach ($columnMaps as $columnMap) {
    $phpNames[] = '"' . $columnMap->getPhpName() . '"';
    $columnNames[] = '"' . $columnMap->getColumnName() . '"';
}
$json = str_replace($phpNames, $columnNames, $json);

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.

Post Status

Asked in February 2016
Viewed 3,899 times
Voted 13
Answered 2 times

Search




Leave an answer