Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Sometimes mapcClassToTable is not enough

posted by aurelien 9 years ago

Hello All,

I dont know if the topic has been discussed before, but I met some issues in my code because of using costum table names.

I used fORM::mapClassToTable method to specify the custom table names, however it was not working with related records.

For example : when doing a $user = new MyUser(); $user->store(); it would fail in the method classisize for a related table, for example classisize('my_group_notplural') (sould have returned 'MyGroup' for example).

Workaround was to do a "foreach $model_class: $activate = new $model_class;" : instanctiating the classes updates fORM with the class to table map.

sounds like what i was dealing with over here

posted by vena 9 years ago

I'd obviously like to get this resolved - can you give me the error message being produced (with line number) and a small sample of code causing it?

posted by wbond 9 years ago

For the above code, let's say i have two tables : company_proj_jobs_ref, and company_proj_files_ref with many files_ref for one jobs_ref. I made too model classes : JobsRef and FilesRef.

The following code will fail :

$job = new JobsRef($existing_pk);
$job->store();

With an error like the one bellow :

Mon Feb 15 11:57:35 2010 error 141.33.102.211 PHP Fatal error: Uncaught exception 'fProgrammerException' with message 'The noun specified could not be singularized' in /opt/apache/***/fGrammar.php:517
nStack trace:
n#0 /opt/apache/***/fORM.php(337): fGrammar::singularize('company_proj_files_...')
n#1 /opt/apache/***/fActiveRecord.php(2668): fORM::classize('company_proj_files_...')
n#2 /opt/apache/+++/ajax.php(196): fActiveRecord->store()
n#3 /opt/apache/+++/index.php(18): include('/opt/apache/...')
n#4 {main}
n thrown in /opt/apache/***/fGrammar.php on line 517, referer: xxx

Reason : in the method fORM::classize, it does not find the related table in the table-to-class array, and then tries to find it by singularizing/camelizing the table name, and fails at it.

When calling $activate = new FilesRef(); previously, it loads the table to class reference into fORM and classize works.

I am not sure of the actual PHP code that produces an error. If the one i provide does not actually fails for you, i can look into it further to provide the code that is failing.

posted by aurelien 9 years ago

But I don't think anything is wrong actually :

If the class is not instantiated, the configure() method is not called, hence fORM table to class map not beeing updated.

The solution may be to add a convention about fORM::mapClassToTable() : do not use it in configure() but in the init.php file or whatever file is called first. (in that case, it might be better suited to have a method like mapClassNameToTable instead)

posted by aurelien 9 years ago

Maybe a call like this :

fORM::mapClassToTables(array(
  'MyClass' => 'my_custom_table',
  'MyOtherTable' => 'my_other_custom_table'
));
posted by aurelien 9 years ago

I will be looking into the issue with configure() not being called yet at this point - I should be able to fix that.

Please note that you can pass a class name as the first parameter to fORM::mapClassToTable(). I might also just need to change the recommendation to have fORM::mapClassToTable() be called in the init script instead of in ::configure(). I'll update here with what I find.

posted by wbond 9 years ago