Welcome Guest, Not a member yet? Register   Sign In
Datamapper Save() function
#1

[eluser]yonel[/eluser]
Hi,

I need an information about the save() function. I want to save a lot of data in a table but i want to do one request. I don't want to do this (it's bad):

Code:
foreach($listPlayer as $player){

    $playerObj = new Model_Player();
    $playerObj-> name = $player["name"];
    $playerObj->save();

}

Is there a solution to do a batch insert with datamapper?

Thanks

Have a nice day Wink

#2

[eluser]WanWizard[/eluser]
Why not use standard DB calls for batch inserts?

Datamapper (or any ORM) serves a specific purpose, and you should not try to solve every database interaction with it. Use the best tool for the job.

If you want to keep all code in one place, it's perfectly fine to add a batch insert method to your model, and put the DB calls in there...
#3

[eluser]yonel[/eluser]
thanks for your answer.

But if i have constraint key team on player ,i can't add with insert_batch because there is an error with the foreign key innodb.

Maybe i can do this:
Code:
foreach($listPlayer as $player){

    $playerObj = new Model_Player();
    $playerObj-> name = $player["name"];
    $teamObj->save($playerObj) ;

}  

or
$playerArr=array();
foreach($listPlayer as $player){

    $playerObj = new Model_Player();
    $playerObj-> name = $player["name"];
    $playerArr[]=$playerObj;

}  
$teamObj->save($playerArr) ;

is it possible to do a thing like this?

#4

[eluser]WanWizard[/eluser]
Your suggestions isn't really an option, both will do individual inserts per player.

If you have a foreign key constraint error, using Datamapper to generate the inserts doesn't really help. You're not passing the correct data to the insert (in this case the foreign key value, I assume "team_id"), just make sure you provide all data on the insert.




Theme © iAndrew 2016 - Forum software by © MyBB