The underscore is a special character in Kohana and gets translated into a directory separator but since I have a bunch of existing code that uses underscores in table names, I want to know if it's possible to configure Kohana to understand that in some way.
views:
91answers:
4One way to do this would be by putting your model classes in subdirectories of the Model folder.
For example, if you had a table called user_profiles
, your directories would look like this:
application/
...classes/
......model/
.........user/
............profile.php
and profile.php
would be like this:
<?php defined('SYSPATH') or die('No direct access.');
class Model_User_Profile extends ORM
{
}
I would recommend going with the above approach (we use it at the company where I work), as it's the "standard" Kohana way of doing things, so you will have fewer hassles down the road. However, if you need to for some reason, you could also use the _table_name
property of the ORM
class (see docs here):
application/
...classes/
......model/
.........userprofile.php
and profile.php
would be like this:
<?php defined('SYSPATH') or die('No direct access.');
class Model_UserProfile extends ORM
{
protected $_table_name = 'user_profiles'; // <== manually setting table name
}
This can also be helpful if your tables don't quite follow the singular vs. plural convention Kohana uses.
Underscores will be translated into directory separator, that's how kohana autoloading works.
But, that's not really matters as you can define $_table_name
properties in your model
.
class Model_Profile extends ORM
{
protected $_table_name = 'user_profiles';
}
Kohana doesnt replace underscores in table names. You can use everything you want in plain queries ($this->db->query($sql)
), Database Query Builder ($this->from($table_name)->...->execute()
) or any kind of AR (ORM, Sprig, Jelly - all of them allow you to set table_name
property). Also you can use default (calculated from class name) table names as notJim described.
Yes, you can. Because your model name is the table name with last word pluralized by default. I mean that
Model_Foo_Bar {
}
will be foo_bars
table which Kohana will try to find by default.