[Solar-talk] Changes afoot: a new Model class
Paul M Jones
pmjones at ciaweb.net
Wed Feb 7 19:21:47 PST 2007
On Feb 7, 2007, at 7:52 PM, Rodrigo Moraes wrote:
> On 2/7/07, Paul M Jones wrote:
>> I have this in place right now:
>>
>> $this->_hasMany('comments', array('limit' => 25, 'order' =>
>> 'comments.id DESC'));
>>
>> So you always get the last 25 comments, no offset. Would that help?
>
> Yes, it would help in some cases, when you know or control the amount;
> there are other cases where you'd need to select more (or at least
> paged) data.
Yeah, I have to agree with you here. I can see AJAX-y kinds of
things needing to page through related records on the same "main"
record.
> But nothing that a subclass can't solve (more below)...
>
>> I've been thinking about that, and I really like the idea, but it's
>> not possible with Solar_Sql_Model just yet. The question is, how
>> would you implement it? What would the method-calls look like in
>> practice?
>>
>> Maybe I can add something to RecordSet that reloads based on the page
>> number, for $post->comments->loadPage(2) to make a new SELECT and
>> reload new comments data.
>>
>> Actually, that's not a bad idea. Let me think on it some more. And,
>> of course, happy to take suggestions.
>
> Well, a method could be defined in a Record subclass:
>
> $post->getRelatedPage('comments', 2);
>
> And then execute the SELECT directly, avoiding the load/reload. Bad
> idea? I'd prefer the method call than loading/reloading. :)
>
> Thinking on this, there are too many possibilities we can play with in
> subclasses.
Here's some background:
When you specify a to-many relationship in the Model, and select a
Record, the corresponding property in the Record is NULL to begin
with. We've been using a blog-post example, so I'll stay with that:
$post->comments would be NULL until you ask for its value, and then
__get() takes over.
__get() sees that $post->comments is NULL, and is a to-many
relationship defined $this->_model->related. It does a SELECT at
that moment, then populates $post->comments as a RecordSet, based on
the relationship options. You can then iterate through the RecordSet
of $post->comments.
The thing about this is, because $post->comments is a RecordSet
object, we can use methods on that object. So, $post->comments-
>loadPage(2), or something like that, is possible. It already has
all the information it needs to do the same SELECT as before, just
with a different LIMIT and OFFSET.
With all that in mind, it seems like RecordSet is the place for that
kind of behavior. Of course, it would apply to all classes descended
from RecordSet: voila, no need to subclass RecordSet.
Does all that make sense? Let me know if I'm missing anything.
Thanks, guys, for taking such a quick interest. :-)
--
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