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]
  • #946 / Jun 26, 2009 12:16am

    PoetaWD

    97 posts

    Those are portuguese words…  😛

    I am from Brazil…

    Where u guys from ?

    Btw

    I keep going with the development of the application… so far so good… thanks for the help you are giving me.

  • #947 / Jun 26, 2009 1:01am

    OverZealous

    1030 posts

    I’m from Michigan, USA.  But I work overnight usually, so I’m up with the Aussies and Kiwis.  😊

  • #948 / Jun 26, 2009 1:40am

    PoetaWD

    97 posts

    I’m from Michigan, USA.  But I work overnight usually, so I’m up with the Aussies and Kiwis.  😊

    Hey man !

    I work at night too… not a fix job.. I work as a webdesigner in my free time. I am still a student, I spent 2 years of my life in medical school… but got tired…

    Now I am going to civil engineering.. but most of my time I spend in the computer…

    I got tired of PAYING other people to do my programing work.. so I am learning… It is pretty easy to learn the language… the hard is to get the tricks…

    I am realizing that experience is 99% of programing…

    Well thanks for helping me…

    I am trying to think something that I might have that would interest you since I dont have any money to donate…  :(

    I have a premium rapidshare account that I might be able to share with you ?
    I also have a premium account at dreamtemplate.com . You can ask me any template and I will send it in your email.

    Hehe… I know that is not much and that you didnt ask.. but I like you and would like to give you something in exchange for your help.

    :D

  • #949 / Jun 26, 2009 2:02am

    OverZealous

    1030 posts

    You could send me a souvenir from Brazil!  My wife actually has some friends in Brazil, but she always gets cool stuff, not me 😉  Of course, that’s probably too expensive, anyway!

    What part of Brazil are you from?

    Otherwise, I host my own server (I have a business plan for my home, which is nice), and I do a lot of my own graphic design :cheese:

  • #950 / Jun 26, 2009 2:53am

    PoetaWD

    97 posts

    You could send me a souvenir from Brazil!  My wife actually has some friends in Brazil, but she always gets cool stuff, not me 😉  Of course, that’s probably too expensive, anyway!

    What part of Brazil are you from?

    Otherwise, I host my own server (I have a business plan for my home, which is nice), and I do a lot of my own graphic design :cheese:

    I sure can !

    I do too most of my work… but something I need some “inpiration” a place to start… so I go there, in the template website,,

    Hehe ...

    I am from Belo Horizonte… not near the beach… more in the center of brazil.

    I lived one year in US as a exchange student, in Virginia… It was pretty cool… I love US… I plan (DREAM) going back one day… maybe after I finish college…

    Just tell me what u want and i´ll try to send you… hehe 😊

    I am done for today..  3am here…

    See ya

  • #951 / Jun 26, 2009 3:11am

    OverZealous

    1030 posts

    It would be just awesome to simply receive a postcard.  I have a P.O. box listed on my website. 😛

    I could start a collection of postcards where people are using DMZ.  That would be really fun!  I’ve got a blank corkboard in front of me…

  • #952 / Jun 26, 2009 2:46pm

    PoetaWD

    97 posts

    It would be just awesome to simply receive a postcard.  I have a P.O. box listed on my website. 😛

    I could start a collection of postcards where people are using DMZ.  That would be really fun!  I’ve got a blank corkboard in front of me…

    I sure will send you a nice card !

    :D

  • #953 / Jun 27, 2009 9:19pm

    PoetaWD

    97 posts

    Hey man…

    Bought you Post Card today..  I will send it monday… hehe

    I have a question… it is regarding relationships.

    In this case I have the objects Sale that has relationship with Type_of_sale and 2 profiles, one represents the Client and the other represent the Salesman.

    The problem is that there are no fields in the profile that would point that the profile is from a Client or a Salesman. That would be pointed by the object related to the profile called User !

    The Profile object only has the details of the user the role is in the User object.

    Here is my controller:

    $obj = new Sale();
    
    $obj->include_related('salestype', array('stName')); // include the name of the type of sale
    
    $obj->include_related('profile', array('stName')); // ??????????????????
    
    $obj->include_related('profile', array('stName')); // ??????????????????

     

    Would be best to include a field in the profile object to point if it is a Client or a Salesman ?

    Thanks in advance

  • #954 / Jun 28, 2009 4:57pm

    OverZealous

    1030 posts

    It’s easier than you imagine, if you are using advanced relationships from DMZ!

    $obj->include_related('client', array('stName'));
    $obj->include_related('salesman', array('stName'));

    Which are now accessible using $obj->client_stName and $obj->salesman_stName.

    This requires your models to look something like this (excuse any mistakes, I’m doing this from memory):

    class Sale extends DataMapper {
        $has_one = array(
            'client' => array(
                'class' => 'profile',
                'other_field' => 'clientsale'
            ),
            'salesman' => array(
                'class' => 'profile'
            )
        );
    }
    
    class Profile extends DataMapper {
        $has_many = array(
            'clientsale' => array(
                'class' => 'sale',
                'other_field' => 'client'
            ),
            'sale' => array(
                'other_field' => 'salesman'
            )
        );
    }

    Sales Table:

    id | ... | salesman_id | client_id
    ---+-----+-------------+----------
    10 | ... |           2 |        5
    11 | ... |           2 |        6

    Usage in controller

    $sale = new Sale();
    $sale->get_by_id($saleid);
    
    $client = $sale->client->get();
    $salesman = $sale->salesman->get();
    
    $sales_for_client = $client->clientsale->get();
    
    $sales_by_salesman = $salesman->sale->get();

    Now, you could rename these in a whole variety of ways.  The key is that you cannot have the same ‘related_field’ (ie: ‘clientsale’) for two different relationships, and that you have to declare both sides of the relationship.

  • #955 / Jun 29, 2009 9:56pm

    PoetaWD

    97 posts

    @DMZ

    Man…  took me about 3 hours reading the documentation and your post to figure this out…

    It FRIED my brain tottaly !

    I cant imagine how you did this… but… you did… and for that I take my hat off for you… really… you are great ! I hope one day I can do stuff like that…

    But getting back to the program…

    😊

    Again, I am building a table with the data from Sales… The fields I am having trouble with are Salesman (I named Staff) and Client.

    I will post the whole code cause it can be a example for other people there are learning…

    Here are my models:

    Profile.php

    <?php
    class Profile extends DataMapper {
    
        var $has_many = array(  
                              'client_sale' => array(
                              'class' => 'sale',
                              'other_field' => 'client'
                              ),
                              'staff_sale' => array(
                              'class' => 'sale',
                              'other_field' => 'staff'
                             ),
    
                              
                              "credito");
        
        var $has_one = array("user");
    
        function Profile()
        {
            parent::DataMapper();
        }
    }
    ?>

    Sale.php

    <?php
    class Sale extends DataMapper {
        
        $has_many se permite vários
        
        $has_one = array( 
                             'client' => array(
                               'class' => 'profile',
                             'other_field' => 'client_sale'
                             ),
                               'staff' => array(
                             'class' => 'profile',
                             'other_field' => 'staff_sale'
                             ),                          
                             "tipossale","credito"
                             );
        
        function Sale()
        {
            parent::DataMapper();
        }
    }
    ?>

    They do work…

    Here is my controlle :

    $obj = new Sale();
                
                $obj->include_related('tiposvenda', array('stNome'));            
                $obj->include_related('client', array('id','stName','stLastname')); // include all of the type columns
                $obj->include_related('staff', array('stName'));             
                $obj->get();            
                 
                $data['objects'] = $obj->all;

    The first question:


    How do I save the relationship ?

    $obj->cliente_id = $this->input->post('client');
                $obj->funcionario_id = $this->input->post('staff');

    Can I use the save() ???


    The second question:

    In my view i´m doing a loop like this:

    foreach ($objects as $list)
        {
    
    
        $tr = array(
                $list->id,
                    $list->tiposvenda_stNome,
                $list->cliente_id.' - '.$lista->client_stName.' '.$lista->cliente_stLastname,
                $list->staff_id.' - '.$lista->staff_stName
            );
        
            $this->table->add_row($tr);
        }

    But something is wrong because it gives me the staff name the same as the client name… the ID is being given right.

    <td>18</td><td>Passagem Aerea</td><td>8 - jk costa de mello paiva</td><td>6 - jk</td></tr>
    <tr>
    <td>19</td><td>Passagem Aerea</td><td>10 - klçklçklç 546546</td><td>6 - klçklçklç</td></tr>

    and when I remove the line:

    //$obj->include_related('client', array('id','stName','stLastname'));

    It will show the name of the staff…

    <tr>
    <td>18</td><td>Passagem Aerea</td><td>8 -  </td><td>6 - Gabriel</td></tr>
    <tr>
    <td>19</td><td>Passagem Aerea</td><td>10 -  </td><td>6 - Gabriel</td></tr>

    Very weird…

    Maybe I am doing something wrong… maybe is a BUG… I dont know… I hope you can help me..

    Thanks

  • #956 / Jun 29, 2009 10:11pm

    OverZealous

    1030 posts

    @PoetaWD
    For saving, see Save an Advanced Relationship (scroll down), but the basic idea is this:

    // save one advanced relationship
    $sale->save_client($client); // $client is already loaded)

    or to save multiple items at once:

    $sale->save(array(
        'client' => $client_profile, // client relationship
        'staff' => $staff_profile // salesman relationship
        $something_else // normal relationship
    ));

    As for your second issue, it looks like you might have a typo, because your ids are coming off ‘list’, but your names are coming off ‘lista’.  If this is just a copy/translation error, check this to see if we can track down the problem:

    $list->client->get();
    $list->staff->get();
    $tr = array(
        $list->id,
        $list->tiposvenda_stNome,
        $list->client_id.' - '.$list->client_stName.' '.$list->client_stLastname . '['.$list->client->id.' - '.$list->client->stName.' '.$list->client->stLastname.']',
        $list->staff_id.' - '.$list->staff_stName . '['.$list->staff->id.' - '.$list->staff->stName.']'
    );

    This should output the same information twice.  If the information is the same, for example:

    1 - John Smith [1 - John Smith]
    2 - John Smith [2 - John Smith]

    Then the issue is that the same information is in the database for both client and staff.

    If the information is different, like this:

    1 John Smith [1 - John Smith]
    2 John Smith [2 - Bob Johnson] // shouldn't happen!

    Then there may be a bug in DMZ, and I’d like to see the query it generated, which you can do by echo’ing $this->db->last_query() right after your get().

  • #957 / Jun 29, 2009 10:24pm

    PoetaWD

    97 posts

    Man you are fast…  😛

    It is a translation error… the code is in portuguese… sorry, here is the original:

    foreach ($objetos as $lista)
        {
                    $opcoes = anchor('realizar_venda/excluir_realizar_venda/'.$lista->id, 'Excluir',array('class' => 'linkdelete'));
                    $tr = array(
                $lista->id,
                $lista->tiposvenda_stNome,
                $lista->cliente_id.' - '.$lista->cliente_stNome.' '.$lista->cliente_stSobrenome,
                $lista->funcionario_id.' - '.$lista->funcionario_stNome,
                $opcoes
            );
            
            $this->table->add_row($tr);
        }

    I added the query screenshot as a image… I took it from the $this->output->enable_profiler(TRUE);

    Please take a look…

    Thanks

  • #958 / Jun 29, 2009 10:26pm

    PoetaWD

    97 posts

    sorry it is in portuguese…

  • #959 / Jun 29, 2009 10:29pm

    PoetaWD

    97 posts

    that was with the line commented…

    Here is the right one:

    SELECT `vendas`.*, `tiposvendas`.`stNome` AS tiposvenda_stNome, `profiles`.`stNome` AS cliente_stNome, `profiles`.`stSobrenome` AS cliente_stSobrenome, `profiles`.`stNome` AS funcionario_stNome
    FROM (`vendas`)
    LEFT JOIN `tiposvendas_vendas` as tiposvendas_vendas ON `vendas`.`id` = `tiposvendas_vendas`.`venda_id`
    LEFT JOIN `tiposvendas` as tiposvendas ON `tiposvendas`.`id` = `tiposvendas_vendas`.`tiposvenda_id`
    LEFT JOIN `profiles` as profiles ON `profiles`.`id` = `vendas`.`cliente_id`

    venda = sale
    stNome = stName
    funcionario = staff
    stSobrenome = stLastname

  • #960 / Jun 29, 2009 10:49pm

    OverZealous

    1030 posts

    I’m pretty sure it’s a bug in DMZ.  I’m doing some tests now, to see what’s going on.  It might take me a few minutes.

    Update: Yes, it is a bug.  I’ve got a fix in place, and will have an updated version of DMZ for download in about 10 minutes.  Thanks for finding this PoetaWD - it’s a very serious bug, but luckily had an easy fix!

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

ExpressionEngine News!

#eecms, #events, #releases