Well, if you do that, your PHP session will infinitely loop if you have populated ->all (since $object->all[0] == $object). 😊
This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
September 05, 2008 12:32pm
Subscribe [115]#931 / Jun 24, 2009 6:21pm
Well, if you do that, your PHP session will infinitely loop if you have populated ->all (since $object->all[0] == $object). 😊
#932 / Jun 25, 2009 9:58am
Thank you guys ! It is understood !
And… I am glad to know that I´m doing it in the very best way !
:D
I will ask again if I have another question… Thank you !
#933 / Jun 25, 2009 2:42pm
Hey Guys,
Me again… 😊
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.. :D ... 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:
$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…
#934 / Jun 25, 2009 3:03pm
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));
#935 / Jun 25, 2009 3:07pm
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:
$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:
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 Error Messages (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.
#936 / Jun 25, 2009 3:12pm
@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…
#937 / Jun 25, 2009 3:26pm
@tdktank59
You are absolutely correct. 😜 You can do what the docs recommend, but that breaks all kinds of things. For example, this can cause problems:
// 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:
$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. 😊
#938 / Jun 25, 2009 3:31pm
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:
$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.'
';
$f = new Profile();
$f->where('id', $this->input->post('funcionario'));
$f->get();
echo 'Salesman id: '.$f->id.'
';
$c = new Profile();
$c->where('id', $this->input->post('cliente'));
$c->get();
echo 'Client id: '.$c->id.'
';
$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’)
#939 / Jun 25, 2009 3:34pm
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
#940 / Jun 25, 2009 3:44pm
@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)
#941 / Jun 25, 2009 5:12pm
Another question… :S
Like, I have the Document object and related to it Document Type and Profiles(Clients) objects,
I want to build a HTML table that will have:
Id | Client Name | Document Type
——————————————————————
1 | Jonh | File
2 | Marie | Folder
In my controller:
$obj = new Documento();
//get all the documents from DB
$obj->get()->all;
//get all the related profiles
$obj->profiles->get()->all; //???????????????????????????????????
//get all the related document types
$obj->Typesdocuments->get()->all; //??????????????????????????????
//define $data as what ??
$data['objects'] = $obj->all; //?????????????????In the view file:
foreach ($objetos as $lista)
{
//write the ID of the document
echo $lista->id
//write the name of the client
echo $lista->profile->stNome;
//write the name of the Document Type
echo $lista->Typesdocument->stNome;
}I am kind of lost now… dont even know where to look,,, 😛 .. you guys are my hope..
Thanks in advance
#942 / Jun 25, 2009 5:26pm
OK, for starters, does Document have one or many Users? One or many Document Types? I would guess you only have one.
If you only have one, you don’t need the ->all. You just do this (for example):
$type = $obj->typesdocuments->get();As for your question, the original DataMapper doesn’t provide a way to join these results. You will have to pass in all Documents to your view, then loop through each document, get() the user and type, and process them:
<? foreach($documents as $doc):
$doc->profiles->get(); // adds a query for each $doc
$doc->typesdocuments->get(); // adds another query for each $doc
?> output data here <?= htmlspecialchars($doc->profiles->name) ?>
<? endforeach; ?>If you were to try DMZ, you could actually include columns from $has_one related items and reduce your number of queries substantially. It would look like this in the controller:
$documents->include_related('profile', array('name')); // include just the name column
$documents->include_related('typesdocument', '*'); // include all of the type columns
$documents->get();And this is your view:
<? foreach($documents as $doc): ?>
<tr>
<td><?= htmlspecialchars($doc->id) ?></td>
<td><?= htmlspecialchars($doc->profile_name) ?></td>
<td><?= htmlspecialchars($doc->typesdocument_name) ?></td>
</tr>
<? endforeach; ?>I don’t have everything labeled the same, so I apologize if that trips you up.
Also, a suggestion:
1) Make sure you name your variables correctly. It’s confusing (for me at least) to have a variable called $objetos, because there is no meaning to your variable. If you had called it $documents (or whatever is correct in your language) – and passed it in as ‘documents’ – it would be easier for you to read and any developers down the road.
#943 / Jun 25, 2009 5:34pm
Thanks MAN !
Now I understand why the DMZ is better !
I will update right now !
I will also try to do as you said and post the results here !
Thank you very much for the quick help !
Update:
As quick as it was… IT WORKED !!!
Much better !!!! Thank you !!!
I guess that I will have to post questions in the DMZ topic !
Thank you man ! This is great !
#944 / Jun 25, 2009 7:10pm
@OverZealous
If Im not mistaken his variables are in spanish. So they do have meaning lol… (I know it was trippen me up too.)
#945 / Jun 25, 2009 11:48pm
Yes, well, objetos = objects, I assume, so it doesn’t really reflect the meaning of ‘documentos’ or ‘perfiles’. That was the point of my suggestion, not a language issue.