• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DataMapper 1.6.0

Can someone explain me how to use additional variables while saving relationships with dataMapper?

I have 3 tables: users, projects, projects_users. I am assigning users to projects. But I also have an option to make user a manager for the project. How to handle this? Should I create table managers_projects? Or maybe there is a way to add 'is_mananger' field into projects_users?

This is just an example. Usually I create additional tables and custom functions to handle the data. But this is very time consuming.

Any help?

Please see the DataMapper OverZealous Addition for an updated version of DM that supports options like that without having extra tables.
The forum link is in my sig, as well as a documentation link.

The feature you want to look at is Working With Join Fields. You can easily add an is_manager column to your join table, and set, read, and query it.

Thank you, this is a very useful addition!

Hey guys -- its your favorite noob again.

I have the following statement:

$usermovies = $tmpUser->usermovie->like_related_movie('title', $title)->get();

I was wondering if/how I can order these results based on a field (title) in the related table?

$usermovies = $tmpUser->usermovie->like_related_movie('title', $title)->order_by('title')->get();

I'm not able to check this, but you should simply try:
$tmpUser->usermovie->like_related_movie('title', $title)->order_by_related_movie('title')->get();

I think the code is smart enough to make that work.

I tried figuring this out on my and have racked my brain for about 6 hours. I am just not sure what the ORM is sending to the db.

Schema is:
last_watched timestamp


username varchar



I am trying to determine is a user has a movie:
$tmpUser = new User();
tmpUser->where ( 'username', $username);
$usermovies = $tmpUser->usermovie->like_related_movie('title', $movie->title)->order_by_related_movie('title')->get();

echo "<br> count is ".$usermovies->count(); //return 3
echo "<br> count is ".count($usermovies->all); // returns 3
echo "<br> count is ".count($usermovies); // returns 1
When in fact the user does not have a usermovie that is related to the title. Does that make enough sense to provide me with some help? Is there I way I can print out what sql is being sent to the DB? That would help be debug this as well.

Turn on CodeIgniter's profiling. That will print out all queries. Optionally, $this->db->last_query() will print out the last query.

count($usermovies) will never return anything useful (and in fact, I would have expected PHP to throw an error), because you are counting an object, not an array.

count($usermovies->all) will return the number of usermovies returned from the last get().

$usermovies->count() will, in your example, return the total number of movies associated with $tmpUser. This is because you haven't added any query parameters at this point. It's the same thing as saying $tmpUser->usermovies->count(). If you want to get the count, you need to re-create the query:
$count = $tmpUser->usermovie->like_related_movie('title', $movie->title)->count();

If you are only trying to see if the movie exists, I would skip the order_by, since it adds unnecessary overhead to the database query.

I found out that the default id for all the tables for datamapper is "id" ... and for one of my project its ItemId on absolutely all of the tables ... so this makes problems in most of the datamapper methods (even the basic ones - get, save, ...)

so - isn't a good idea this to be set as e parameter for the whole datamapper ... Smile

also - if I use alpha_dash for some text - characters as @ also makes problems ... Sad ... how do u deal with them Smile

changing the 'id' parameter may turn into a headache and could cause unexpected results, but if i were forced to do it in a pinch, i would (on a working copy at first) run the following search & replace commands:

"['id']" to "['itemid']" (to convert any array accessor
"->id" to "->itemid" (coverting object accessors)
"`id`" to "`itemid`" (converting sql - those are backticks)

can't say that will get EVERY instance, but that's what you're going for. Otherwise, no theres no variable setting such as $idfield (although there could be)

as for alpha_dash, you can create any method on your model and use it in the rules array. It just has to start with an underscore '_'

so to create a rule that checks alpha_dash PLUS the '@' symbol you could add this to your models,or to datamapper (or prolly better, an extension of it) if you want it available to all models.
function _alpha_dash_at($str)
    return ( ! preg_match("/^([[email protected]])+$/i", $str)) ? FALSE : TRUE;
then in your validation array on the field you want to use it on include that in the rules array – ie rules' => array('alpha_dash_at')

hope that helps.



I have a problem, I need a special relation:
I have galleries in the gallery table (1 record = 1 gallery) and images in the image table (1 row = 1 image). The simple many to many relation works, but I need a "priority relation", because I want to order my images in the galleries by priority. This is a many to many relation so I cant add a priority column to the images table, because an image can be related with multiple galleries.

Can I solve my problem with the Datamapper? What's the easiest way?


Sorry, I upgraded to a new version of DMZ, and I realized you added a new feature: "Join Fields". This is exactly what I need! Thanks!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.