Welcome Guest, Not a member yet? Register   Sign In
DataMapper 1.6.0

[eluser]OverZealous[/eluser]
It would be just awesome to simply receive a postcard. I have a P.O. box listed on my website. Tongue

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

[eluser]PoetaWD[/eluser]
[quote author="OverZealous.com" date="1246014688"]It would be just awesome to simply receive a postcard. I have a P.O. box listed on my website. Tongue

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...[/quote]

I sure will send you a nice card !

Big Grin

[eluser]PoetaWD[/eluser]
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:

Code:
$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

[eluser]OverZealous[/eluser]
It's easier than you imagine, if you are using advanced relationships from DMZ!

Code:
$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):
Code:
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:
Code:
id | ... | salesman_id | client_id
---+-----+-------------+----------
10 | ... |           2 |        5
11 | ... |           2 |        6

Usage in controller
Code:
$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.

[eluser]PoetaWD[/eluser]
@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...

Smile

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
Code:
<?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
Code:
<?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 :
Code:
$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 ?
Code:
$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:

Code:
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.

Code:
<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:

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

It will show the name of the staff...

Code:
<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

[eluser]OverZealous[/eluser]
@Poetawd
For saving, see Save an Advanced Relationship (scroll down), but the basic idea is this:
Code:
// save one advanced relationship
$sale->save_client($client); // $client is already loaded)
or to save multiple items at once:
Code:
$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:
Code:
$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:
Code:
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:
Code:
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().

[eluser]PoetaWD[/eluser]
Man you are fast... Tongue

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

Code:
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

[eluser]PoetaWD[/eluser]
sorry it is in portuguese...

[eluser]PoetaWD[/eluser]
that was with the line commented...

Here is the right one:

Code:
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

[eluser]OverZealous[/eluser]
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!




Theme © iAndrew 2016 - Forum software by © MyBB