Sascha A.
Sascha A.

Reputation: 4626

Propel - how can I set column name for PHP same as in DB automatically

I used Propel 2 and want to have the column-names for PHP just the same like they are in dhe DB. I used a schema.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<database name="timetable" defaultIdMethod="native">
	<table name="entry" phpName="Entry">
		<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
		<column name="date" type="date"  required="true" />
		<column name="timeBegin" phpName="timeBegin" type="time"  required="true" />
		<column name="timeEnd" type="time" required="true" />
		<column name="description" type="varchar" size="128" required="true" />
		<column name="expert_id" type="integer" required="true"/>
		<column name="project_id" type="integer" required="true"/>
		<foreign-key foreignTable="expert" phpName="Expert" refPhpName="Entry">
			<reference local="expert_id" foreign="id"/>
		</foreign-key>
		<foreign-key foreignTable="project" phpName="Project" refPhpName="Entry">
			<reference local="project_id" foreign="id"/>
		</foreign-key>
	</table>
</database>

Propel generates by this the names for the tables and columns. The names in the mySQL-database are written in the correct manner, just as described in the schema.xml. But by default Propel generates the names for the columns in PHP with a capital letter and everything behind is a lowercase. This is not what I want, I got for example "Timeend" instead of "timeEnd" Look at this easy query:

<?php
	require 'vendor/autoload.php';
	include "generated-conf/config.php";
	
	$entry = EntryQuery::create()
	->find()
	->exportTo('JSON');
	
	echo $entry;
It produces:
{"Entries":{"Entry_0":{"Id":1,"Date":"11.09.2015","timeBegin":"09:00","Timeend":"19:00","Description":"","ExpertId":5,"ProjectId":7}}} What I want is this: id, date, timeBegin, timeEnd, description, expertId, projectId (or expert_id, project_id).
I know that I can force Propel to do this by using for every column phpName="timeBegin" and so on (like I do just for demonstraiting in the example above), but I find this to inconvenient. This should be possible by some settings perhaps in the propel.xml

Upvotes: 0

Views: 609

Answers (2)

David Huerta
David Huerta

Reputation: 31

You could use an regex to fix the schema.xml to remove the phpName atribute only for columns

search:

phpName="(\w)*" type

replace: type

Then rebuild your models....

model:build

An ugly side effect you will have is the getter/setter methods turns into this...

$object->getid(); $object->gettimebegin();

Upvotes: 0

William
William

Reputation: 186

You can use the toArray method to return with the column names.

$entry = EntryQuery::create()
    ->find()
    ->toArray(null, null, \Propel\Runtime\Map\TableMap::TYPE_FIELDNAME);

You can see a snippet here:

http://sandbox.propelorm.org/46731f6

The resulting JSON's root is a bit different, but I'm sure you can work around that.

Upvotes: 1

Related Questions