Reputation: 33996
I have following code. I'm trying to make it simpler and shorter. I created $data1
array and added relevant data with array_merge
and saved it to my model. But note that $data1
and $exists
has same code inside. Is it possible to pass $data1
array to MyModel's find method without rewriting same code ?
Because of I need to create multiple rows in these snippet, I used MyModel->create
. In this code I pasted two blocks, but originally I have 6 blocks like this. So shortening is important for me.
As summary: I need to shorten this code snippet, I don't want to rewrite same data in each block.
$usersNew=array("mike", "john");
$usersLost=array("anna", "maria");
$data1 = array('userid' => $userid,
'date' => date('Y-m-d')
);
foreach ($usersNew as $f) {
$data2 = array_merge($data1, array("users_new" => $f));
$exists=$this->MyModel->find('first',
array('conditions' => array(
'MyModel.userid' => $userid,
'MyModel.date' => date('Y-m-d'),
'MyModel.users_new' => $f
) ));
if ($exists == FALSE) {
$this->MyModel->create();
$this->MyModel->save($data2);
}
}
foreach ($usersLost as $f) {
$data2 = array_merge($data1, array("users_lost" => $f));
$exists=$this->MyModel->find('first',
array('conditions' => array(
'MyModel.userid' => $userid,
'MyModel.date' => date('Y-m-d'),
'MyModel.users_lost' => $f
) ));
if ($exists == FALSE) {
$this->MyModel->create();
$this->MyModel->save($data2);
}
}
Upvotes: 0
Views: 50
Reputation: 4522
For optimization of code, maybe this is not the best place to ask. There are other "siblings" of SO for that, like Code Review. I'm just suggesting that because maybe you'll find better or more dedicated answers for that in there.
But, to the point at hand. What I normally do in a case like this is to have an extra array like this
$helper = array('usersNew'=>'users_new',
'usersLost'=>'users_lost',
/* name of array you want to loop => name of column in db */ );
It's like having an array of the variable part of your code. I'm using your exact code, but in case you need something more complicated in the future, a multidimensional array will be better.
So, you do just one foreach
and go through all the variables in the $helper
array
$usersNew=array("mike", "john");
$usersLost=array("anna", "maria");
//I prefer to avoid merge performance, so I'm deleting this, though it works
/*$data1 = array('userid' => $userid,
'date' => date('Y-m-d')
);*/
foreach ($helper as $arrayName => $dbCondition) {
foreach ($$arrayName as $f) {
$data = array('userid' => $userid,
'date' => date('Y-m-d'),
$dbCondition => $f
);
$exists = $this->MyModel->find('first',
array('conditions' => array(
'MyModel.userid' => $userid,
'MyModel.date' => date('Y-m-d'),
'MyModel.'.$dbCondition => $f
) ));
if ($exists == FALSE) {
$this->MyModel->create();
$this->MyModel->save($data);
}
}
}
I haven't test the code, but it should work without mayor modifications, maybe a closing } I'm missing or something like that.
If for some reason, this piece of code gives you problems (happens to me sometimes)
$data = array('userid' => $userid,
'date' => date('Y-m-d'),
$dbCondition => $f
);
do it in two parts
$data = array('userid' => $userid,
'date' => date('Y-m-d'),
);
$data[$dbCondition] = $f;
The trick is the double $$
(variable variables) and organize the $helper
array in a way that works for you. I hope I made this clear enough.
Upvotes: 1