fabik / database

[DEPRECATED] Database layer for Nette Framework
19 stars 2 forks source link

Add Table::createOrUpdate() #3

Closed fprochazka closed 12 years ago

fprochazka commented 12 years ago

You may wanna add this method.

fabik commented 12 years ago

I was looking at the code of nette-addons and it isn't used anywhere. Do you really think this is a good idea? The generated query contains all the data twice, so I think something like this would be better:

public function createOrUpdate($uniqueKeys, $values)
{
    if ($row = $this->findOneBy($uniqueKeys)) {
        $row->update($values);
        return $this->findOneBy($uniqueKeys);
    } else {
        return $this->create($uniqueKeys + $values);
    }
}
fprochazka commented 12 years ago

It's quite handy sometimes ;) Anyways, It's just a suggestion, you can close this if you think you don't need it.

fabik commented 12 years ago

I implemented a combination of both solutions in d23bc23.