Question

I want to query all persons and then convert to an associative array using the column name rather than the phpName. I know that within Php code, you should always the phpName but I'm trying to elegantly dump the contents into a form which uses snake_case. Thanks for the help.

Lets say I have this table in my schema:

<table name='person'>
  <column name='id' phpName='Id' /> 
  <column name='first_name' phpName='FirstName' /> 
  <column name='last_name' phpName='LastName' /> 
</table>

and I have the below query:

$people = PersonQuery::create()->find()->toArray();
var_dump($people);

This will give me:

array(1) { [0]=> 
    array(3) { 
        ["Id"]=> int(1) 
        ["FirstName"]=> string("John") 
        ["LastName"]=> string("Doe") 
    } 
} 

but I really want this:

array(1) { [0]=> 
    array(3) { 
        ["id"]=> int(1) 
        ["first_name"]=> string("John") 
        ["last_name"]=> string("Doe") 
    } 
} 

Cheers!

Was it helpful?

Solution

How about:

$people = PersonQuery::create()->find()->toArray(BasePeer::TYPE_FIELDNAME);
var_dump($people);

Look in the file BasePersonPeer for other options, including translateFieldName($name, $fromType, $toType) which is another way of doing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top