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

[eluser]OverZealous[/eluser]
Well, if you do that, your PHP session will infinitely loop if you have populated ->all (since $object->all[0] == $object). :-)

[eluser]PoetaWD[/eluser]
Thank you guys ! It is understood !

And... I am glad to know that I´m doing it in the very best way !

Big Grin

I will ask again if I have another question... Thank you !

[eluser]PoetaWD[/eluser]
Hey Guys,

Me again... Smile

Now I have a practical question... I also would like to know if am I doing this right...

For this example I will have 4 objects:

- Profiles (Both, client and salesman, are from the same object)
- Sales (Venda)
- Credit (Credito)
- Type of Sale (Tiposvenda)

I have a form where the user select the salesman, the client, the type of sales, an put the value of the sale.

The system will calculate the credit (Value x 4), will create a new object Credit and will it relating to the sale and the profile of the client.

It does that.. Big Grin ... but IT IS NOT saving the relationships of the object Sales (Venda)

That is so strange... it is saving the ralationships of the Credit object, but not the Sale's relationships...

Here is the controller:

Code:
$obj = new Venda();            

            $obj->nmValor = $this->input->post('nmValor');
            $obj->dtData = $this->input->post('dtData');
            $obj->stObservacoes = $this->input->post('stObservacoes');

            $t = new Tiposvenda();
            $t->where('id', $this->input->post('tipo'));
            $t->get();

            $f = new Profile();
            $f->where('id', $this->input->post('funcionario'));
            $f->get();

            $c = new Profile();
            $c->where('id', $this->input->post('cliente'));
            $c->get();
                                            
            $b = new Credito();
            $b->nmQuantidade = CalcularCredito($this->input->post('nmValor'));

            $obj->save();
            $b->save();
            
            $b->save($obj);
            $b->save($c);

            
            $obj->save($t);
            $obj->save($f);
            $obj->save($c);

Well.. I am learning.. I think it is getting pretty good... what do you think ?

Can you help me to get this working ?

Thank you again guys...

[eluser]tdktank59[/eluser]
The controller looks fine...

Check to make sure the models have the correct relationships (the $has_one or $has_many)

PS: Even though saving them all 1 at a time does work to lighten the code if your saving a relation to the same object you can combine them

for example you can combine $obj->save to be $obj->save(array($t,$f,$c));
and $b->save can be $b->save(array($obj,$c));

[eluser]OverZealous[/eluser]
A few things:
1) Are $t, $f, and $c getting loaded correctly [ex: echo('Tiposvenda->id: ' . $t->id) ]

2) A tip for saving multiple objects - you can save as many as you want in one go:
Code:
$obj->save( array($t, $f, $c) );
Also, this will automatically save any changes to $obj, so you don't need to separately write $b->save() if you have $b->save( array($object, $c) )

3) Always check the results of your saves, because the error might be happening before you save the object:
Code:
if( ! $obj->save( array($t, $f, $c) )) {
    // something didn't validate:
    echo $obj->error->string;
}
The error field contains a lot of information, check out [urlhttp://stensi.com/datamapper/pages/validation.html]Error Messages[/url] (scroll way down).

4) Finally, the original DataMapper cannot save multiple, different relationships to the same model more than once. I would guess that, even if it worked, $c and $f wouldn't save correctly.

You probably cannot do what you are trying to do with the client and salesman in DM. Instead, you might want to look at DMZ, which I have been developing since stensi (original developer of DM) disappeared a while back. There's updated documentation, and you should specifically look at Advanced Relationships.

DMZ is a near drop-in replacement for DataMapper, there's instructions on upgrading here.

[eluser]tdktank59[/eluser]
@OverZealous

With DataMapper you can have multiple of the same thing (client, employee etc...)

See "Self Referencing Relationships"
http://stensi.com/datamapper/pages/relationtypes.html

Basically you end up creating models that extend the original user model in that example to encompass the managers, employees and supervisors

Back to the OP.
Check your models for the proper relations setup (I had a problem a while back because it was not spelled properly). And after that print out some error strings of the save calls...

[eluser]OverZealous[/eluser]
@tdktank59
You are absolutely correct. ;-P You can do what the docs recommend, but that breaks all kinds of things. For example, this can cause problems:
Code:
// Posts have one Creator and one Editor, both are Users
$user = $post->creator->get(); // returns as a Creator object
// Users have a Company.  However, Company is not related to Creator, so the relationship lookup would fail!
$user->company->get();

Also, this causes problems:
Code:
$user = new User();
$user->get_by_id($userid);
// Now, how to I save $user as the Creator and Editor on my post?
$post->save($user); // fails, because Post isn't related to User directly.

So, yes, you can save multiple relationships with DM, but, in my opinion, they don't work very well. I suppose I should have been more accurate when describing the issue, instead of spouting off rhetoric. Smile

[eluser]PoetaWD[/eluser]
I see.. I think my problem is in my models...

The has_many and has_one variables... I will review that !

Here is it updated:

Code:
$obj = new Venda();            
                        $obj->nmValor = $this->input->post('nmValor');
            $obj->dtData = $this->input->post('dtData');
            $obj->stObservacoes = $this->input->post('stObservacoes');
            
            
            $t = new Tiposvenda();
            $t->where('id', $this->input->post('tipo'));
            $t->get();
            echo 'Type of sales id: '.$t->id.'<br />';
            
            
            $f = new Profile();
            $f->where('id', $this->input->post('funcionario'));
            $f->get();
            echo 'Salesman id: '.$f->id.'<br />';
            
                        $c = new Profile();
            $c->where('id', $this->input->post('cliente'));
            $c->get();
            echo 'Client id: '.$c->id.'<br />';
            
            
            $b = new Credito();
            $b->nmQuantidade = CalcularCredito($this->input->post('nmValor'));
            
            $obj->save(array($t, $f, $c));
            $b->save(array($obj, $c));

It loads everything fine...

Here:

Type of sales id: 7
Salesman id: 6
Client id: 8

POST DATA

$_POST['funcionario'] 6
$_POST['cliente'] 10
$_POST['tipo'] 8
$_POST['dtData'] 09/06/2009
$_POST['nmValor'] 122112
$_POST['stObservacoes'] 121212

DATABASE: poetawd_smfc QUERIES: 19

0.0004 SELECT *
FROM (`ci_sessions`)
WHERE `session_id` = '3556aeab9844f71885543031afb67f83'
AND `user_agent` = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;'

0.0003 SELECT * FROM `vendas` LIMIT 1

0.0003 SELECT * FROM `tiposvendas` LIMIT 1

0.0003 SELECT *
FROM (`tiposvendas`)
WHERE `id` = '8'

0.0005 SELECT * FROM `profiles` LIMIT 1

0.0003 SELECT *
FROM (`profiles`)
WHERE `id` = '6'

0.0003 SELECT *
FROM (`profiles`)
WHERE `id` = '10'

0.0004 SELECT * FROM `creditos` LIMIT 1

0.0003 INSERT INTO `vendas` (`nmValor`, `created`, `stObservacoes`, `updated`) VALUES ('122112', '2009-06-25 18:23:24', '121212', '2009-06-25 18:23:24')

0.0009 SELECT *
FROM (`tiposvendas_vendas`)
WHERE `venda_id` = 13
AND `tiposvenda_id` = '8'

0.0012 SELECT *
FROM (`profiles_vendas`)
WHERE `venda_id` = 13
AND `profile_id` = '6'

0.0002 SELECT *
FROM (`profiles_vendas`)
WHERE `venda_id` = 13
AND `profile_id` = '10'

0.0003 INSERT INTO `creditos` (`nmQuantidade`, `created`, `updated`) VALUES (488448, '2009-06-25 18:23:24', '2009-06-25 18:23:24')

0.0002 SELECT *
FROM (`creditos_vendas`)
WHERE `credito_id` = 11
AND `venda_id` = 13

0.0002 SELECT *
FROM (`creditos_vendas`)
WHERE `credito_id` = 11
LIMIT 1

0.0002 INSERT INTO `creditos_vendas` (`credito_id`, `venda_id`) VALUES (11, 13)

0.0003 SELECT *
FROM (`creditos_profiles`)
WHERE `credito_id` = 11
AND `profile_id` = '10'

0.0002 SELECT *
FROM (`creditos_profiles`)
WHERE `credito_id` = 11
LIMIT 1

0.0002 INSERT INTO `creditos_profiles` (`credito_id`, `profile_id`) VALUES (11, '10')

[eluser]PoetaWD[/eluser]
OverZealous :

I will also give a look at your script man...

I think for now the basic DM will work for me, but I will need more advanced relationships !

* The code works now... The problem was in the has_many and has_one variables...

Now I realize that I need to have it declared in both models...

Thank you

[eluser]tdktank59[/eluser]
@OverZealous
Well at that point you would no longer be calling a user model anymore...
To save an employee it would be $e = new Employee();
where Employee extends the user model. Thus not breaking anything...

At least thats the way I understood it... back when I was using those types of relations...

(DMZ makes it soo much easier lol)




Theme © iAndrew 2016 - Forum software by © MyBB