Welcome Guest, Not a member yet? Register   Sign In
DataMapper related records and form repopulation
#1

[eluser]Francis Robert[/eluser]
Hi, I'm using DataMapper for my current project and I'm running into a little problem when it comes to repopulate a form with related records when they have not been saved, using validation. Here's the scenario:

I have a first object named 'Template', this object only has a name so we have:
Code:
$t = new Template();
$t->name = 'my tag name';

I have a second object named 'Templocale', this object has all the other information that needs to exists in one or more languages:
Code:
$d = new Templocale();
$d->locale = 'en';
$d->subject = 'An email subject';
$d->body = 'An email body';

I save the reference:
Code:
$t->save($d);

We add a second language:
Code:
$d = new Templocale();
$d->locale = 'fr';
$d->subject = 'Un sujet de courriel';
$d->body = 'Un texte de courriel';

I save the reference:
Code:
$t->save($d);

My problem is, if I filll in my form to enter a new record, while not entering the value that goes into $t->name, I get the error message that this value is required (following validation), which is totally normal, but the rest of the form comes back empty, obviously because it wasn't saved and is not part of the template yet.

How to overcome this? Is there a way to create, from the controller, an instance of Template(), then one instance of Templocale() for each language, then relate those instances to Template() (where they belong) and send that "not yet saved" instance to the form to repopulate it?


Second and last question:
Suppose I have the same Template() object as above and that all its related Templocale() are loaded into it in memory, is there a way to query those related Templocale() without querying the database?

Example, I iterate through the active languages in the application as follow:
Code:
foreach ($locales as $locale)
{
    //    Instead of doing the following (querying the database everytime?):
    $myTemplate->templocale->where('locale', $locale->name)->get();

    //    I'd like to do something like this:
    $myTemplate->templocale->where('locale', $locale->name)->select();
    //    to select the Templocale() that corresponds to my parameter, in memory.
}

I cannot iterate directly through $myTemplate->templocale because locales can be switched on and off.

Thanks for any help.
Francis
#2

[eluser]Francis Robert[/eluser]
Found the solution to my problem here, finally %-P
http://ellislab.com/forums/viewthread/90...10/#531730

(feel free to delete my request if necessary)




Theme © iAndrew 2016 - Forum software by © MyBB