CodeIgniter Forums
DataMapper 1.6.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358)



DataMapper 1.6.0 - El Forum - 06-14-2009

[eluser]Khoa[/eluser]
Hi, I just notice this: the count() function does not seem to execute the extending get() function in the child class.

For example: I have a model User that extends DataMapper. Inside this model, it has an override get() method which makes sure that we always deal with active users

Code:
function get($limit = NULL, $offset = NULL)
{
    $this->where('active', 1);
    return parent::get($limit, $offset);
}

When I do this, it gives me only active users which is great.

Code:
$user = new User();
echo $user->get();

But when I do this, it counts all users instead of only the active ones.

Code:
$user = new User();
echo $user->count();

I looked inside the count() method, it does not call the extending get() method. So where can I put the $this->where('active', 1); so that it can be used by both get() and count() methods, and probably many others? Or I have to extend the count function again and duplicate this where clause?

Thanks,

Khoa


DataMapper 1.6.0 - El Forum - 06-14-2009

[eluser]OverZealous[/eluser]
You are correct, count is an optimized method that does not call get(). If it did, then get would fully load the query.

There is no way to do what you ask (include a method to count both at once). I also don't recommend doing what you did, as you leave no way to override the embedded where('active', 1). This means you would never be able to look up non-active items, even from within related classes or when looking up a specific id directly.

You should look at your usage pattern, and determine if maybe this is something that can be fixed by tweaking your design, or just call $user->where('active')->count() and $user->where('active')->get() whenever you need to. That makes more sense to me, as it clearly states your purpose within your code, and doesn't leave weird bugs.

If you are looking something up a lot, then you could do what I do, and add a method called get_active(), which adds the where clause and calls get(). Then you haven't overridden the default get(). You could add a method for counting, although if you've already looked up the collection, you can just call count($user->all), which is probably more efficient.


DataMapper 1.6.0 - El Forum - 06-14-2009

[eluser]Khoa[/eluser]
Thanks Phil. That's actually a good point. Putting such condition inside the override get() method will make it hard (or maybe impossible) to get inactive items. I also use this in another place, so will look into it before rolling out this pattern too much :-)

That's always a problem when starting new framework, do it, looks good today, not good tmr, then redo it, change this, change that, I think I spend a lot of time just to figure out what is the best way to use CI, and now what is the best way to use DM! LOL Smile

Speaking of that, how did you start with CI? The doc is fantastic but it is very discrete, function by function. Is there a recommended way of building apps with CI? And also with DM?

Khoa


DataMapper 1.6.0 - El Forum - 06-14-2009

[eluser]OverZealous[/eluser]
You know, it's only been about 6 months or so, and I've already forgotten how I started. I know I was googling for a new framework, because I had always used Java in the past and (ironically) wanted something more portable (ie: using hosted solutions).

As far as learning it, it sounds like you and I are similar. I just write test code, and see what happens. I happened to have had a small website project I was getting paid for, so I used that as a springboard to try out some ideas. I really like the documentation, although I still spend a lot of time slogging through code, because I never trust that documentation matches up 100% with implementation. ;-)

I think the goal with CI has always been to provide building blocks, more than a blueprint. You can build in a whole variety of directions. That freedom can sometimes be overwhelming.

One other CI library I do recommend is the Template library (not the one built into CI). It is very powerful for helping to structure and organize your views. I'm even using it with certain AJAX responses, setting HTTP headers to send extra information.

DataMapper was a complete fluke. It happened to have a recent update on the CI forums, and caught my eye. I started using it, and digging around the source code to understand how it worked. A month or so later, I'm posting tweaks to stensi, and within about 2 months I was providing help on the forums. And now I have extended it, and (as far as I know) I am currently the only person actively developing it.


DataMapper 1.6.0 - El Forum - 06-14-2009

[eluser]Khoa[/eluser]
I feel like it's a little bit...pity to not have such guidelines, recommendations built into the online docs and probably sample apps (actively updated as CI grows, not those built and just sit there). The framework is great when we start it, with just a few lines, bang, we got something up and running! WOW! How cool is that! But when I go into more serious problems, like building complicated CMS and stuff like that, it starts to get a bit tricky about which way to go.

Basically, the doc is great, it's just missing the piece that glues those pages together imo.

When started, I found one book and some online tuts about using CI, which was cool, but again, just tut by tut, nothing that covers a big picture. Sometimes I feel like I want to...make another framework for CI! LOL! Sounds crazy right? Something that says: ok, to do this, you put it here, to do that, you put it over there. Or at least write some more guidelines for beginners to start with (if anyone needs such thing Smile)

Regarding DM, I read the first few pages, I saw you contributed a lot to this library. I really appreciate that, especially when you are still actively supporting it Smile

We should have a sub-forum for this. It deserves that. One post is getting too long now Smile

You know what, I'm building my blog right now and CI/DM will definitely on my blog list Smile

Khoa


DataMapper 1.6.0 - El Forum - 06-14-2009

[eluser]OverZealous[/eluser]
LOL on the subforum.

If you end up using DMZ (see my sig), feel free to post on that topic as well (or instead). It's much shorter. ;-)


DataMapper 1.6.0 - El Forum - 06-14-2009

[eluser]Khoa[/eluser]
I think DM, and also DMZ are really the core of CI now! I never build an application that has so much functionality so far without writing any SQL! So they deserve a sub-forum Smile

I think I'm kind of "SQL fan" sometimes as I enjoy writing SQL a lot! I know, you might be laughing right? Wink But the thing is I love the feeling that I have control over exactly what my SQL does and have just the records I want to be returned. But I start to give up (a little bit) that habit now thanks to DM Smile

Hey, that DMZ looks great! It has a lot of cool features. I will look at switching now Wink

BTW, just one small question that I had last night: do you feel that DM (dont know about DMZ) seem to generate too many SQLs statements? Too many little ones? Or maybe because I did not use it at the optimal way :-P

Khoa


DataMapper 1.6.0 - El Forum - 06-14-2009

[eluser]OverZealous[/eluser]
There is often some misconception about the number of statements, mostly due to the looking up of fields. When DM first instantiates a model's class, it needs to look up the columns in the database. This causes an extra query per class.

Also, it isn't very efficient in some ways, but a lot of that is alleviated in DMZ, especially through the include_join_fields and join_related (which should probably be called include_related). These two alone can often remove dozens or hundreds of extra queries.

I plan on eliminating the first problem (for production systems) in a future update to DMZ by caching all of the per-class data. I have the how figured out, I just need to write it.

Anyway, good luck with DMZ!


DataMapper 1.6.0 - El Forum - 06-14-2009

[eluser]Khoa[/eluser]
Those include_related functions look like just what I need! My app at the moment ends up having a lot of SQLs, especially those inside loop. Eg get the list of all categories, then loop through it and display the list of entries inside each category. This kind of thing generates heaps of small SQLs!

I will surely use DMZ ;-)

Again, thanks for your support Phil. I just hope I don't bother you so much with my little PHP/DM knowledge and tons of questions Smile


DataMapper 1.6.0 - El Forum - 06-16-2009

[eluser]tdktank59[/eluser]
I should know this by know...
But where do I put the custom validation function that I want to use on multiple models?
I can get it working on a single model but I don't want duplicates of the function...

Code:
/**
* Empty Select
* Returns FALSE if the field is empty otherwise TRUE
*
* @param int $field - The select value to check
* @return boolean TRUE/FALSE
*/
function _empty_select($field)
{
    return ($field < 1) ? FALSE: TRUE;
}

@OverZealous
Cant wait for that caching! let me know when that's released I will be more than happy to "break" it in! Ive got some statements that call roughly 20 different tables lol That should help a bit! (A lot of relations being called in)

Im also quite impressed even when I was printing out 1000 ish rows with roughly 5 related tables, DM held up strong! (very short load time for the server) The longest part was printing all the damn data (I have recently paginated that page lol)