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]
  • #781 / Apr 24, 2009 1:09am

    camporter1

    22 posts

    There appears to be a problem with the validation errors. Here’s my validation for firstname:

    var $validation = array(
            array(
                'field' => 'firstname',
                'label' => 'First Name',
                'rules' => array('trim', 'required', 'xss_clean', 'min_size' => 1, 'max_size' => 25, 'strip_tags')
            ),
    (etc)

    And here’s the controller test code that I was using to figure out how error handling works:

    $profile = new Profile();
                $profile->where('user_id', $profileid)->get();
                
                $profile->firstname = "abcdefghijklmnopqrstuvwxyz";
                
                $profile->save();
                print($profile->error->firstname);

    Instead of getting an error about the fact that the firstname is too long (over 25), I get the error that: The First Name field must be at least 1. Which, even if it were the case, seems confusing by itself.

    Anyone know if I’m doing something wrong?

  • #782 / Apr 24, 2009 1:34am

    camporter1

    22 posts

    Argh. My fault. Didn’t notice there was a max_length and max_size. Two different validator types. The length one wasn’t listed in the ‘predefined’ list on the validation manual page, and the form validation one wasn’t working either (max_length[25]).

  • #783 / Apr 24, 2009 3:50pm

    NachoF

    171 posts

    Im trying to figure out how this works and why its worth it… but Im a little confused on many things… first of all.. in the provided example.. why does the User Model not have a var $table?? while Country and Book do specify the name of the table…

  • #784 / Apr 24, 2009 4:45pm

    OverZealous

    1030 posts

    If you read the documentation more carefully, you’ll see that $table and $model are automatically determined if they are not provided, based on the class name of the model.  Providing them is optional.

    DataMapper is about increasing development productivity, as well as helping to standardize your codebase.  95% of your database access code is abstracted away, and validation can be automated for most objects.  It speeds up development, helps to ensure data integrity, and is significantly easier to read.

  • #785 / Apr 24, 2009 5:45pm

    camporter1

    22 posts

    You will most certainly not miss using SQL queries or even just activerecord (although DataMapper works similarly).

  • #786 / Apr 24, 2009 8:42pm

    NachoF

    171 posts

    Ok, Im still having trouble grasping the concept though.. but Im very willing to give it a shot.. please help me with this little example and I will try to understand it all so that I wont need any help changing the rest of my project.

    My project has a table ‘products’ and a table ‘existence’ (although Im willing to change the name to existences for the sake of this to work)... in the products table I only have id and name of the product.. while in the existences table I have id (im not sure I need this), id_product ammount, price and date .... I do this cause its for a company that receives different shipments of the same product at different times and different prices… how exactly would I create the datamapper classes for this two models?? and how would I do a select that returns a table

    ProductName   |  Price   | Ammount


    I know its a lot to ask but I have been trying to figure it out for the past half and hour and Im not sure how to get this done.

    I would also appreciate recommendations on naming conventions.
    Edit:
    I dont understand something.. I have products and existences… one product has many existences and one existence has only one product… why do I need a new table??

    A Database Error Occurred
    Error Number: 1146
    “Table ‘company.existences_products’ doesn’t exist”

  • #787 / Apr 24, 2009 10:56pm

    OverZealous

    1030 posts

    First, as the docs say, every single model requires an id field.  (Also, it is preferred that you have an id field on all of your relationship tables, but this is not actually used by DataMapper.)

    Second, the original DataMapper only supports Fifth form database normalization, which means that every relationship requires it’s own table, even if it is 1-to-N related.  So the original DM won’t support using product_id.  However, DMZ DOES support it.  DMZ is a drop-in replacement, so you can decide which one you prefer.  The additions provided by DMZ are not nearly as well documented as the original DM.

    Third, what you are describing is fairly basic.  Each Model needs to be named as the singular form, with the table in plural form.  You add ‘product’ to the $has_one array for Existence, and you add ‘existence’ to the $has_many table of the Product model.  The example on Setting up Relationships is almost an exact duplicate of what you are trying to do.

    To select a row, you simply use the get() method, which is related to the ActiveRecord code DM is based on.  If you want to get all Existences of a specific Product, it looks like this:

    $product = new Product();
    $product->get_by_id($id);
    $product->existence->get();
    
    foreach($product->existence->all as $exist) {
        // handle each existence here
    }

    I believe this is also incredibly well explained.

    Using a new code library is always going to be difficult.  You need to really take the time to read through the examples, and read through the instructions.  With a library as well documented as DataMapper, it shouldn’t be this difficult.

  • #788 / Apr 24, 2009 11:56pm

    NachoF

    171 posts

    If you want to get all Existences of a specific Product, it looks like this:

    $product = new Product();
    $product->get_by_id($id);
    $product->existence->get();
    
    foreach($product->existence->all as $exist) {
        // handle each existence here
    }

     

    Thanks, I got that working now….. one more question, what I want is a table that has every existence of a product.. for instance

    productName |  Quantity |  Price
    Whisky             25         1300
    Whisky             30         1200
    Beer                 100         500

    The way i did that before was using a very specidic query inside my model and then just use this->table->generate($query); and save that to a $data[‘table’] variable that gets echoed inside my view…. is there no way i can use the table generate function anymore if I use this?

  • #789 / Apr 25, 2009 1:42am

    OverZealous

    1030 posts

    Do you mean all existences of all products?  Because I showed you what you asked.  If you mean all existences, don’t make it so complicated:

    $existence = new Existence();
    $existence->get(); // contains all existences
    foreach($existence->all as $e) {
        $product = $e->product->get();
        // do whatever
    }

    If you want to do multiple results in a single query, use DMZ instead, and try this:

    // note: add or replace 'id', 'name' with any fields you need from Product.
    $existence->join_related('product', array('id', 'name'))->get();
    foreach($existence->all as $e) {
        $product_id = $e->product_id;
        $product_name = $e->product_name;
        // do whatever
    }

    Join related only works for $has_one relationships, by-the-way.

    Now, you should be able to go from here, because, as I mentioned earlier, the manual for DataMapper is very complete, and has a lot of examples for how to build queries.

  • #790 / Apr 25, 2009 3:46am

    NachoF

    171 posts

    Do you mean all existences of all products?  Because I showed you what you asked.  If you mean all existences, don’t make it so complicated:

    $existence = new Existence();
    $existence->get(); // contains all existences
    foreach($existence->all as $e) {
        $product = $e->product->get();
        // do whatever
    }


    Now, you should be able to go from here, because, as I mentioned earlier, the manual for DataMapper is very complete, and has a lot of examples for how to build queries.

    Yes, thats what I meant… all existences of all products… my bad… anyway, I dont think Ill be needing DMZ…following your advices I have accomplished the table creation like so

    $this->table->set_heading('Name', 'Price', 'Quantity');
    $e = new Existence();
            $e->get();
            foreach($e->all as $existence) {
            $product = $existence->product->get();
            $this->table->add_row($product->name, $existence->price, $existence->quantity);        
            }
            $data['ProductsTable']= $this->table->generate();

    Ill keep on messing with this cause it definitely seems to save a lot of time.. thanks man.

  • #791 / Apr 25, 2009 7:23am

    jongliko

    7 posts

    Hi, I’m new to Datamapper. I’m loving it ! But I’m stuck in a little work I’m trying to do. Hope some datamapper user can help me figure out the solution.

    I have 4 tables Artistes, Videos, Styles, Types. Each of this tables is link with the three others with has_many.

    A video can have many artistes, many styles (rock, jazz, thriller) and many types (movie, interview, concert, clip..).

    I’m trying to show recursively my data starting with the type.

    $types = new Type();
    $types = $types->get()->all;
            
    foreach($types as $type){
            
       echo '<h3>'.$type->nom.'</h3><p>';<br />
            <br />
       $styles = $type->style->get()->all;<br />
                <br />
       foreach($styles as $style){</p>
    
    <p>      echo '</p><h5>'.$style->nom.'</h5><p>';<br />
               <br />
          $videos = $type->style->video->get()->all;<br />
                <br />
          foreach($videos as $video){<br />
             <br />
              echo $video->nom;<br />
              echo '<br />
    ';<br />
                       <br />
          } <br />
       }<br />
    }

    I can’t figure out How to show the videos which are in a type and in a style.

    The regular query would have been something like that :

    SELECT
        videos.nom
    FROM
        db.videos
        INNER JOIN db.types_videos 
            ON (videos.id = types_videos.video_id)
        INNER JOIN db.types 
            ON (types.id = types_videos.type_id)
        INNER JOIN db.styles_videos 
            ON (styles_videos.video_id = videos.id)
        INNER JOIN db.styles 
            ON (styles_videos.style_id = styles.id)
    where styles.id = $id_style AND types.id = $id_type;


    Ideally I wanted to show the Artist who have a video in this style and in this category before the videos of the artist, but I’m already stuck here with the videos.

    Thanks for your help.

  • #792 / Apr 25, 2009 12:22pm

    OverZealous

    1030 posts

    @jongliko
    What you want to use is DataMapper’s where_related method.  This method allows you to query based on the joined information:

    $videos->where_related_type('id', $type_id);
    $videos->where_related_style('id', $style_id);
    $videos->get();

    You can read more about the options for querying on related objects here.  Just scroll down to Get (Advanced).

    For example, if you already have the type loaded in, it could look like this:

    $videos->where_related($type);

    And that’s it!

  • #793 / Apr 25, 2009 7:09pm

    qureshi

    14 posts

    Hi,

    I need help again.

    Here is the scenario:
    A user can have many links. He can associate those links with one or many verses. A verse thus can have many links. The following relation-tables exist in the DB: links_users, links_verses, users_verses.

    I know how to get all links associated with a given verse, or a given user, but how do I get only those links associated with both verse and user at the same time?

    Thanks for reading,

    Nadeem Qureshi

  • #794 / Apr 25, 2009 7:16pm

    qureshi

    14 posts

    Hi again,

    I read the documentation more closely and this seems to work:
    $user->link->where_related_verse(‘id’, $verse->id)->get();

    Is it possible to donate to the DataMapper project? I would like to. This tool is priceless!

    Nadeem

  • #795 / Apr 25, 2009 9:33pm

    jongliko

    7 posts

    @OverZealous Thanks for your help ! I was going crazy 😊 . Now I really can start working with Datamapper.

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

ExpressionEngine News!

#eecms, #events, #releases