CodeIgniter Forums
DataMapper ORM v1.8.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper ORM v1.8.0 (/showthread.php?tid=37531)



DataMapper ORM v1.8.0 - El Forum - 05-23-2012

[eluser]animatora[/eluser]
[quote author="Hakkai" date="1337757968"]is there any solution for me to repopulate the form after validation has failed?

set_value is not functioning.[/quote]

Try something like:

Code:
public function signup()
{
   $u = new User();
  
   if($_POST){
     $u->name = $this->input->post('name');
     if($u->save())
     {
       .....
     }
   }
}
in the view you can pass the User object and get the value like:

Code:
<input type="text" value="<?= $u->name ?>"/>



DataMapper ORM v1.8.0 - El Forum - 05-23-2012

[eluser]shenanigans01[/eluser]
@WanWizard

alright thanks, I will use the id field Smile would you be able to provide me a quick query that builds an array of everyscore belonging to the user with id 1 and a quick snip of how to output it? should be able to build the rest of my app from that


DataMapper ORM v1.8.0 - El Forum - 05-23-2012

[eluser]WanWizard[/eluser]
[quote author="Hakkai" date="1337757968"]is there any solution for me to repopulate the form after validation has failed?

set_value is not functioning.[/quote]
You're not validating form values, you're validating a Datamapper object.

Which doesn't have any relation to a form, unlike CI's Form validation, which as the name implies validates form data (and knows what came from the form, so it can re-populate).

I you want to work with DM validation as primary means of validation, you'll have to setup your controller logic differently, and use a DM object as the basis.

So in a create method
- create a new object
- populate it with default values if needed
- assign it to the view
- in the view use $object->fieldname to add the field value to the HTML (or form methods used)

If the form was submitted the flow is
- create a new object
- populate it with the values from the form
- run the validation
- if validated, save and redirect away from the create method
- if not, assign the object to the view

For edit you don't start with an empty object, but you'll get the one to edit. Otherwise the procedure is the same.


DataMapper ORM v1.8.0 - El Forum - 05-23-2012

[eluser]WanWizard[/eluser]
[quote author="mikem562" date="1337783054"]would you be able to provide me a quick query that builds an array of everyscore belonging to the user with id 1 and a quick snip of how to output it? should be able to build the rest of my app from that[/quote]
Output might be a bit difficult, because that will depend on the app:
Code:
// get user number 1
$user = new User(1);

// alternatively, you can do
$user = new User();
$user->get_by_id(1); // functionally the same

// get this users scores
$user->scores->get();

// output some user fields
echo $user->id, $user->name, $user->somethingelse;

// and the scores
foreach ($user->scores as $score)
{
    echo $score->field, $score->otherfield, '<br />';
}



DataMapper ORM v1.8.0 - El Forum - 05-23-2012

[eluser]Hakkai[/eluser]
[quote author="WanWizard" date="1337807419"][quote author="Hakkai" date="1337757968"]is there any solution for me to repopulate the form after validation has failed?

set_value is not functioning.[/quote]
You're not validating form values, you're validating a Datamapper object.

Which doesn't have any relation to a form, unlike CI's Form validation, which as the name implies validates form data (and knows what came from the form, so it can re-populate).

I you want to work with DM validation as primary means of validation, you'll have to setup your controller logic differently, and use a DM object as the basis.

So in a create method
- create a new object
- populate it with default values if needed
- assign it to the view
- in the view use $object->fieldname to add the field value to the HTML (or form methods used)

If the form was submitted the flow is
- create a new object
- populate it with the values from the form
- run the validation
- if validated, save and redirect away from the create method
- if not, assign the object to the view

For edit you don't start with an empty object, but you'll get the one to edit. Otherwise the procedure is the same.[/quote]

i roughly get the idea already, thanks for the advice.


DataMapper ORM v1.8.0 - El Forum - 05-24-2012

[eluser]shenanigans01[/eluser]
@WanWizard

Thanks for the help!, I've got everything figured out now, up & running perfectly. First time using datamapper have to say it's working amazingly Smile I have an unrelated question based off of your conversation with Hakkai, is there any advantage / disadvantage to using datamapper object validation vs CI's form validation? Also I'm going to assume using both would be pointless? or am I mistaken?


DataMapper ORM v1.8.0 - El Forum - 05-25-2012

[eluser]WanWizard[/eluser]
It depends.

I always do validation in the model. The reasoning being that no matter where you use the model, or where the data comes from (not necessarily a form), I am confident that the data going into the database is valid.

Other other hand I have plenty of forms that don't have a direct mapping to a model. For example selection or search forms. In which case I validate in the controller.


DataMapper ORM v1.8.0 - El Forum - 05-29-2012

[eluser]costicanu[/eluser]
[quote author="WanWizard" date="1336382306"]I can't tell you any more. The error is generated and displayed by CI's DB driver.

Datamapper doesn't have a DB layer, it just uses $this->db, like any other application.[/quote]


I tried on that server to install a fresh Codeigniter and they told me that they don't allow persistance connections on databases. I disabled it from Codeigniter database.php config file and that fresh Codeigniter installation works fine.

After that I added my complete codeigniter + datamapper app on that server and changed the same $db['default']['pconnect'] to FALSE from database.php and datamapper still not works. How can I modify datamapper to not use those persistance connections? (because I saw that it doesn't recognize the codeigniter config variable $db['default']['pconnect'])


DataMapper ORM v1.8.0 - El Forum - 05-29-2012

[eluser]WanWizard[/eluser]
Again, Datamapper doesn't have it's own database config, nor it's own database layer.

It uses CI's database layer, including all configuration. The only thing you can control is how it uses it, through the datamapper configuration value 'db_params'.

It has three possible options:
- "" (empty string), this is the default and means it will use the CI database config as-is,
but will create a separate instance per model.
- FALSE, will re-use the same $CI->db instance in all models (this will break subqueries)
- any string, will be passed to $this->load->database() to load a specific database configuration.

This is documented here: http://datamapper.wanwizard.eu/pages/config.html


DataMapper ORM v1.8.0 - El Forum - 05-29-2012

[eluser]costicanu[/eluser]
Wow, great! You solved the problem! Thank you very much for this cool extension and for your quick response!

$config['db_params'] = FALSE; in datamapper configuration file solved the persistance problem!