[Solar-talk] Changes afoot: a new Model class
Paul M Jones
pmjones at ciaweb.net
Thu Feb 8 10:14:45 PST 2007
On Feb 8, 2007, at 9:22 AM, Travis Swicegood wrote:
> Hopefully the Spamhaus issue is fixed and this comes through :-)
>
>
> Rodrigo Moraes wrote:
>> The problem Paul mentioned is how to make the automatic model finding
>> in _hasOne() / _hasMany() relationships if the names are, let's say,
>> all singular. Keeping the example of the 'post' model:
>>
>> $this->_hasOne('blog');
>> $this->_hasMany('comment');
>>
>
> The simplest solution here is simple plural. Everything has a "s"
> added
> to the end of it if it's plural. It doesn't necessarily look as
> elegant, but it does remove an extra layer while still allowing you to
> differentiate.
Unfortunately, that's too naive a solution, even for English --
"equipment" for example, or "series", or "men" ... you get the idea.
And it completely ignores non-English naming, which is one of the
Solar goals (internationalization, etc). Granted, I ignored it
myself with the inflection thing, but that's going away now based on
the entirely accurate assessments of Rodrigo, Andreas, Antti, et al.
(Thanks again, guys, for correcting me on that one. :-)
Long as we're on the subject, I have completely removed all
inflection in my working copy. The new relationship defs are going
to look something like this, now that inflection is removed:
class Solar_Model_Nodes_Blogs extends Solar_Model_Nodes {
protected function _setup()
{
// many blog-entries to one area
$this->_belongsTo('area', array(
'model' => 'areas',
'fkcol' => 'area_id',
'eager' => false,
));
// one blog entry to many comments
$this->_hasMany('comments', array(
'fkcol' => 'node_id',
));
}
}
You can specify any name you want ('area', 'comments') and point to
any model you want, using any foreign-key column you want. Defaults
use the model name whenever possible. Generally, I expect models
will be named in the plural, so you'll get plurals by default.
--
Paul M. Jones <http://paul-m-jones.com>
Solar: Simple Object Library and Application Repository
for PHP5. <http://solarphp.com>
Join the Solar community wiki! <http://solarphp.org>
Savant: The simple, elegant, and powerful solution for
templates in PHP. <http://phpsavant.com>
More information about the solar-talk
mailing list