j4mie / paris

A lightweight Active Record implementation for PHP5, built on top of Idiorm.
http://j4mie.github.com/idiormandparis/
996 stars 131 forks source link

Namespaces & table names #88

Closed michaelward82 closed 10 years ago

michaelward82 commented 10 years ago

Lets image I have a model of class User. I have a namespace to the User class, which is vendor\model. My table name is user.

Using the factory method, everything works as expected:

Model::factory('User')->find_one(20) // Works

However, using the static methods I get an error indicating that Paris is looking for the table vendor_model_user.

use vendor/model/User;
User::find_one(20) // Wants vendor_model_user table

I can avoid this problem if I manually specify the table name. This is something I can do, but I don't want to take unneeded manual steps.

So two questions:

  1. Is this expected behaviour (I imagine it is)
  2. Is there anything I can do about it? Many of my models already extend from another class which itself extends from Model, if that helps.
treffynnon commented 10 years ago

You want to use the factory for all access.

You can alias on the model like so if you wish:

class User extends Model {
    public static function one($id) {
        return Model::factory(__CLASS__)
            ->find_one($id);
    }
}
$user = User::one(20);
michaelward82 commented 10 years ago

I think I will opt for manually specifying the table name in the model.

The way namespace are handled, is it the intended behaviour for Paris? I believe the static methods could work using the short name for the class, which I believe can be determined.

I'm not suggesting that is what Paris should do, but it is something it could do.

Thoughts?

treffynnon commented 10 years ago

Well I didn't add namespace support so I am not sure what the intention of that patch was. If it could be achieved without losing PHP 5.2 support then that would be great. I haven't really looked myself so I can't tell you I am afraid.

michaelward82 commented 10 years ago

Ok, I'll investigate.

michaelward82 commented 10 years ago

The behaviour is expected and documented.

From paris.php, line 204:

       /**
         * Convert a namespace to the standard PEAR underscore format.
         * 
         * Then convert a class name in CapWords to a table name in 
         * lowercase_with_underscores.
         *
         * Finally strip doubled up underscores
         *
         * For example, CarTyre would be converted to car_tyre. And
         * Project\Models\CarTyre would be project_models_car_tyre.
         */
        protected static function _class_name_to_table_name($class_name) {
            ...

Given the existing documentation and the need for backward compatibility, any changes to behaviour here would probably require an option to switch it on.

Given that the only configuration I can see is in the Idiorm ORM class, I'm not sure how we would want to set such an option given that it's nothing that Idiorm is concerned with.

michaelward82 commented 10 years ago

Maybe a property of Model - which could be set individually, or using inheritance?