ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

DataMapper 1.6.0

September 05, 2008 12:32pm

Subscribe [115]
  • #976 / Jul 15, 2009 4:54pm

    gshipley

    6 posts

    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:

    usermovie:
    id
    last_watched timestamp
    etc
    
    movie:
    id
    title
    runtime 
    etc
    
    user:
    id
    username varchar
    etc
    
    movies_usermovies:
    id
    movie_id
    usermovie_id
    
    usermovies_users:
    id
    user_id
    usermovie_id

    I am trying to determine is a user has a movie:

    $tmpUser = new User();
    tmpUser->where ( 'username', $username);
    $tmpUser->get();
                    
    $usermovies = $tmpUser->usermovie->like_related_movie('title', $movie->title)->order_by_related_movie('title')->get();
    
    echo "
     count is ".$usermovies->count(); //return 3
    echo "
     count is ".count($usermovies->all); // returns 3
    echo "
     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.

  • #977 / Jul 15, 2009 5:29pm

    OverZealous

    1030 posts

    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.

  • #978 / Aug 02, 2009 5:09am

    pesho_h_k

    17 posts

    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 ... 😊

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

  • #979 / Aug 02, 2009 7:22am

    ntheorist

    84 posts

    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("/^([-a-z0-9_-@])+$/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.

    n

  • #980 / Sep 17, 2009 6:09pm

    Peet86

    14 posts


    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?

    Thanks!

    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!

  • #981 / Sep 23, 2009 12:38pm

    introvert

    83 posts

    Hello,

    I have 2 simple models with has_one relation:

    class Feed extends DataMapper {<br /> var $has_many = array('item');</p> <p> function __construct() {<br /> parent::DataMapper();<br /> }<br /> }</p> <p>class Item extends DataMapper {<br /> var $has_one = array('feed');</p> <p> function __construct() {<br /> parent::DataMapper();<br /> }<br /> }

    When I want to relate those 2 records:

    $f = new Feed();<br /> $f->save();<br /> $i = new Item();<br /> $i->save();<br />       <br /> $f->save($i);

    I get error that items_feeds table doesnt exist. Why would DM require additional table if there is has_one relation? If I try removing has_many from Feed model, I get the same result.

    What am I doing wrong? Should I pass any additional data to DM in such cases?

    Thanks in advance!

  • #982 / Sep 23, 2009 1:51pm

    BrianDHall

    760 posts

    I don’t think Datamapper supports in table foreign keys, so every relation amongst two tables must have its own join table.

    Check out Overzealous Extension to Datamapper for support of in-table foreign keys, which eliminate the need for join tables in cases of has_one relationships.

  • #983 / Oct 15, 2009 7:47am

    ennis

    11 posts

    I was wondering How I would go about this with datamapper,

    I have a clients table and a clients_clients table that links a client as a child of another clinet (id, parent_id, child_id)

    But it doesnt seem to work,  can datamapper models relate to them selfs?

    I have jst had a look under the advanced relationshops section still a bit confused though

    Sorry if what i write seems confusing 😊

    Cheers.

  • #984 / Oct 15, 2009 8:35am

    OverZealous

    1030 posts

    @ennis
    I hate to keep repeating myself, but if you want more advanced features (such as self relationships), you should switch to using DMZ, my extended version of DataMapper.

    The discussion board is available in my signature, and you can learn a lot more about it from the link above.

  • #985 / Oct 15, 2009 8:44am

    ennis

    11 posts

    Was just about to post 😊

      var $has_many = array(
          ‘relatedclient’ => array(
            ‘class’ => ‘client’,
            ‘other_field’ => ‘client’
          ),
          ‘client’ => array(
            ‘other_field’ => ‘relatedclient’
          )
      );

    Thanks you I figured it out was already using DMZ thanks

  • #986 / Oct 18, 2009 8:46am

    Benedikt

    85 posts

    Hi,
    As far as I understood everything is an object.

    So if I have a car (table: cars) and this car has a color (table: colors) I have two models, a car model and a color model, right?

    So let’s say my car has a lot more attributes, e.g. what kind of tyres, what kind of radio, what kind of seats etc, I have also the tables “tyres”, “radios”, “seats” etc, right?

    So let’s assume I want to bundle these attributes and set a prefix only for the car’s attributes. How can I do this?

    I want to have the “main objects” like “car” without any table-prefix. But attributes should be with a prefix. I know how to set a join-prefix and a table-prefix for certain models. Then I can also set the table name für “plurals”. Could I do the following to realize the grouping (same prefix for the car’s attributes):

    Color-Model:
    $table = ‘car_colors’;
    $prefix = ‘’;
    $join_prefix = ‘join_’;

    Car-Model:
    $table = ‘cars’;
    $prefix = ‘’;
    $join_prefix = ‘join_’;

    etc.

    Would that work?

    Thanks for your help!!!

  • #987 / Oct 18, 2009 6:41pm

    Benedikt

    85 posts

    Sorry for spamming this thread, but is Datamapper still under active development, e.g. update to more recent versions of CodeIgniter if necessary?

  • #988 / Oct 21, 2009 12:41am

    SMatanza

    1 posts

    I’m studying any Codeigniter ORM, is this project still active ?
    Thanks in advance.

  • #989 / Feb 17, 2010 6:35am

    Mitja B.

    45 posts

    HI,

    can you please tell me benefits of DMZ DataMapper against Doctrine? Why i should use DMZ Datamaper instead of Doctrine?

  • #990 / Feb 17, 2010 4:12pm

    BaRzO

    105 posts

    @ennis
    I hate to keep repeating myself, but if you want more advanced features (such as self relationships), you should switch to using DMZ, my extended version of DataMapper.

    The discussion board is available in my signature, and you can learn a lot more about it from the link above.

    Follow this

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases