CodeIgniter Forums
DataMapper 1.6.0 - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Libraries & Helpers (
+--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358)

DataMapper 1.6.0 - El Forum - 06-16-2009

Validation functions go inside the models. Your example code isn't quite right, however, because $field is the name:
function _empty_select($field)
    return ($this->{$field} < 1) ? FALSE: TRUE;

This is fairly clearly shown in the docs: under Custom Validation

They can also go inside a new Extension model, although they work a little differently. Hmmmm, I should document that!

Also, production caching is a go with version 1.3.0 - see the links in my sig!

DataMapper 1.6.0 - El Forum - 06-16-2009

Ah you got a nice documentation up!!!

DM's documentation on that is not very clear. Yours however is!

Anyways I found that error with the $field thing lol... Just didn't fix the post after I found that.
Is there a way to make that function global to all models? since I have a few models that need that function?

Otherwise ill go get that new DMZ version!

DataMapper 1.6.0 - El Forum - 06-16-2009

[quote author="tdktank59" date="1245207507"]Is there a way to make that function global to all models? since I have a few models that need that function?[/quote]

I'm not trying to single you out, because it's a valid question. I added a FAQ to the docs, and that is answered here:

What's the best way to add a method I want to use multiple times? - but for validation rules you will want to look specifically at Extending DataMapper Directly, because at this moment, you cannot add validation rules to the new Extensions class.

That will be changed soon. ;-)

DataMapper 1.6.0 - El Forum - 06-16-2009

I understand. I just need to get used to the fact that you finally have a good documentation guide. (The one with the initial releases was a bit light on words if you get what I mean)

DataMapper 1.6.0 - El Forum - 06-20-2009


This is a very newbye question.. but I will do it anyway since I am very new with PHP or programing at all...

I have being messing around with this lybrarie and found this AWESOME !

But my queston is, how can it return for me if a ralationship was found or not ?

Like, I have made the table Users and Profiles.

Each user will have only one Profile... but... I want the system to check if the user already has a profile, if it doesnt, he will be redirected to a form to create his profile. Here is what I get so far:

$u = new User();        
$id_logado = '3';
            $u->where('id', $id_logado)->get();
            echo 'Logged user ID'.$u->id;
            if( ????????????????????? )
                echo 'You already has a profile !';
                echo 'Please fill out the form to create your profile';    

I need a function that will return TRUE if a profile associated with the user is found and false if no profile is found...

Thank you for the help

DataMapper 1.6.0 - El Forum - 06-20-2009

if( $u->exists() ) {
    // $u exists
} else {
    // $u does not exist

Also, you can use this to look up a user by id (it's just shorthand for what you already wrote):

Finally, make sure you look through the DataMapper documentation, at

Good Luck, and if you have any questions, feel free to ask.

DataMapper 1.6.0 - El Forum - 06-21-2009

Hey !

Thank you for the quick reply ... I was giving a second look at the user guide and I foud that he used the empty() function...


I did it like this:


and... it worked !

I will also try the way you said for learning porpose... thank you very much

DataMapper 1.6.0 - El Forum - 06-21-2009

Just an FYI:


does the exact same thing as your code. What I like about that is it is a little more obvious what you are trying to check (that profile exists, rather than profile->id is not zero).

DataMapper 1.6.0 - El Forum - 06-23-2009

Thanks for the great tool! Quick question about select_max that I don't seem to understand.

$tmpUser = new User ( );
$tmpUser->where ( 'username', 'someusername');

$tmpUser->get ();

$movieCount = $tmpUser->usermovie->get()->count();
echo "Count is ".$movieCount;

^^ works great. Now I want to print the last watched movie:

echo "<br>..".$tmpUser->usermovie->last_watched."<br>";

prints the last watched time. From the manual, select_max only returns an object with the last_watched field populated (in this example). How do I then relate this back to the object with all fields?

for example:
echo "<br>..".$tmpUser->usermovie->movie->get()."<br>";

DataMapper 1.6.0 - El Forum - 06-23-2009


This is how you get what you want:
// generates a query like (excluding the joins):
// SELECT * FROM usermovies ORDER_BY last_watched LIMIT 1
$tmpUser->usermovie->order_by('last_watched', 'DESC')->get(1);

Now $tmpUser->usermovie will contain a single usermovie with the maximum last_watched value.

Understand this, however: As far as the database knows, there could be more than one movie with the same maximum "last_watched" (apparently, you are using dates here, so it should not happen). That's why select_max cannot return a single usermovie. The example I give above only works consistently for columns where there can only be one max value in a column.