[Deprecated] DMZ 1.6.2 (DataMapper OverZealous Edition) |
[eluser]tdktank59[/eluser]
@rootman It looks like you are trying to compare an ID field to an email field. That would be your problem there... As far as saving goes. You need to have at least 1 side of the relationship saved before you can save more to it. The way I normally work relationship saves are as follows. This example happens to be a one to one relation however it would work in all cases if I am not mistaken. Code: $u = new User();
[eluser]OverZealous[/eluser]
@silvergear99 include_related only includes the field for output. If you want to save contact_related, you need to manually load the contact, and save that field separately: Code: $user->contact->get();
[eluser]silvergear99[/eluser]
@OverZealous Thanx, it works now. ![]() Code: $related = $user->from_array($_POST, array('username','group')); I have like this now, but is their a better way to save ?
[eluser]OverZealous[/eluser]
I'm not sure what these lines are for: Code: $related = $user->from_array($_POST, array('username','group')); I'm guessing that isn't doing anything, because $related never has get() called, therefore there is no object to save. Also, you must check the results of every call to save(). Otherwise, you won't be able to catch errors and report them. Finally, you should wrap multiple calls to save in a transaction, to ensure that you can rollback any changes if an error occurs: Code: $user->trans_begin();
[eluser]silvergear99[/eluser]
[quote author="OverZealous" date="1262619566"]I'm not sure what these lines are for: Code: $related = $user->from_array($_POST, array('username','group')); I'm guessing that isn't doing anything, because $related never has get() called, therefore there is no object to save. [/code][/quote] But $related includes the group (Can be: Admin, Registred or Banned) the user has chosen from the dropdown. (At least I thought so). If I dont save($related) the group is never saved. I actually never used transactions before, but that was very easy. I will definitely use that in the future. Thanx for your tips.
[eluser]silvergear99[/eluser]
This is odd. If validation fails on contact_name and the error message is echod. Yet it still save the username. Code: if($this->input->post('username'))
[eluser]TheJim[/eluser]
@cube1893 [quote author="cube1893" date="1262221499"] Thanks, include_related helps but it only works for one-to-one relationships (applicant). How can I achieve the same for one-to-many relationships? For example if I want to access all "note" objects for an application in the second foreach loop. If I call $applicationg->notes->get() in every loop, it causes bad performance, again.[/quote] You're looking at the problem the wrong way. Do your get on the "many" part of that relationship, and use include_related to pull in the "one" part (it doesn't just work on one-to-one relationships; it's that it only works for the "one" side of any relationship). Then you can grab everything in one query. And of course follow the other tips to make sure you're only pulling in fields you need and consider your logic so that you're sure you really need to load everything you're attempting to grab. Finally, if you absolutely need to load a lot of objects, take a look at my earlier get_streaming post. It makes a significant difference when loading a lot of objects.
[eluser]OverZealous[/eluser]
[quote author="silvergear99" date="1262633590"]This is odd. If validation fails on contact_name and the error message is echod. Yet it still save the username. [/quote] Uh, that's because I sent you a bad example. ![]() Code: // .. snip ... Alternatively, you could not report an error, and just check the error properties on $user and $user->contact after the fact. (These should be filled with the reason why the save failed.) Also, you shouldn't need to have more than one transaction. The PHP method to connect to the database should always return the same DB handle, so the transaction is global. You can start and end the transaction on any object, and all internal transactions are committed or rolled back at once. Because transactions can be expensive, you should only create the minimum needed. (You can, however, nest transactions if you need.) Finally, I didn't mention this before, but transactions require a specific table type if you are using MySQL. Pretty much all other common databases support transactions out-of-the-box.
[eluser]silvergear99[/eluser]
OverZealous Thanx it works much better now. But I have an other problem now ![]() No matter what I'm doing I just can't save when I'm using a radiobox. Get the error: Error Number: 1054 Unknown column 'Array' in 'field list'. But If I change type to 'dropdown' it saves. Code: view:
[eluser]OverZealous[/eluser]
@silvergear99 You'll have to do some basic debugging. Look at the generated HTML source, output whats being sent back with the CI profiler, or even just echo some code to see what is happening. |
Welcome Guest, Not a member yet? Register Sign In |