Foreign keys in Generator config

Asked by nloding on 2010-01-18

I certainly hope development on CoughPHP has stopped! I've only just found it!

I'm looking to manually specify foreign keys in the generator config. What I am testing out this software with is, as many other people, a blog system. One table has two references to the PK of my user's table, to allow for the possibility of another user on the system performing an action that needs to be logged.

So for the "author" field, I use the field name of "user_id" which automatically links back to the "user" table's PK. But there is also a field recording who approved an action, "approved _by", and that should link back to the "user" table's PK also. How do I specify that in the config??

Question information

Language:
English Edit question
Status:
Answered
For:
CoughPHP Edit question
Assignee:
No assignee Edit question
Last query:
2010-01-25
Last reply:
2010-01-26
nloding (nathan-nloding) said : #1

Um, yeah, that was a typo above that I feel I should correct. It should ready that I hope that development HASN'T stopped ... oops!

Anthony Bush (awbush) said : #2

No, development hasn't stopped :) There's been a light amount of user contributions (partial postgres & mssql DB add-ons) and partial implementation of the 2008 meeting minutes which needs to get wrapped up and released soon; mostly we've been too busy *using* CoughPHP!

But to your question: custom foreign keys is a useful feature CoughPHP doesn't currently have. We plan to add support for it and possibly a completely separate schema config that other projects can make use of too like view-controller generators. For now, we get around it by doing one of these:

1. Use column names that have the other table's PK *in* the name. In your example that would mean changing "approved_by" to "approved_by_user_id". Then you can customize the "field_settings/id_regex" setting so that "*_user_id" maps to the user table. You can do this for all DBs, just one DB, or just for the table:

In the "database_schema_generator.inc.php" config file we often use this:

 $config['field_settings']['id_to_table_regex'] = array(
  '/^(.*)_id/', // look for exact table name first, only when that fails will we try to match one of the ones below
  '/^billing_(.*)_id$/',
  '/^shipping_(.*)_id$/',
  '/^parent_(.*)_id$/',
  '/^child_(.*)_id$/',
  '/^primary_(.*)_id/',
  '/^default_(.*)_id/',
 );

You'd merely need to add "/^approved_by_(.*)_id$/" and re-generate.

2. The other way, which doesn't require column renaming, is to manually add the methods into the concrete (non-generated) class. You can copy-paste from other generated relationships and modify as needed or use the generator script in the 1.4 branch: http://bazaar.launchpad.net/%7Ecoughphp/coughphp/1.4/annotate/head%3A/scripts/gen_cough_object_methods.php

See example usage (launchpad formatting isn't great): http://pastie.org/783084

Thank you for your interest in CoughPHP!

nloding (nathan-nloding) said : #3

I would just like to make sure I fully understand the mechanics here. The 'id_to_table_regex' parameter is basically saying "take what's matched in the regex and try to match that name against a table." And it will then link that particular field, "billing_user_id" for example, to "user_id" in "user" because the regex matched? Would the same regex work for other tables? As in, "billing_cart_id" would match to "cart_id" in "cart" etc., automatically?

Anthony Bush (awbush) said : #4

Close, but not quite! It doesn't link against field names, it links against table names. The part it links to is the part in parenthesis. Notice in my examples that "_id" is not in the parenthesis, so if you used:

    $config['field_settings']['id_to_table_regex'] = array(
        '/^(.*)_id/', // look for exact table name first, only when that fails will we try to match one of the ones below
        '/^billing_(.*)_id$/',
    );

And you have a field named "billing_cart_id," then it will link to the "cart" table, if one exists. It does not matter what the primary key of the cart table is named, so long as it has one. It could just be "id," for example, although CoughPHP developers prefer the convention "table_name_id."

Hope this helps!

Can you help with this problem?

Provide an answer of your own, or ask nloding for more information if necessary.

To post a message you must log in.