php-crud-api icon indicating copy to clipboard operation
php-crud-api copied to clipboard

Access to db in handler

Open kkrell2016 opened this issue 2 years ago • 4 comments

Hi, I'm not really a PHP guru. I would like to check if a field called "oid" is currently null while updating. If the actual value in the DB is null, an update should be possible, otherwise no update should be made.

I think the validation handler would be the right place for this. My problem is how to access the database from the handler? Can I reuse any function without opening a complete new connection to the db?

Or should I put it in another place? If so where?

Hope you can help me.

kkrell2016 avatar Oct 03 '22 19:10 kkrell2016

Hi, this seems like a duplicate of #751.

mevdschee avatar Oct 04 '22 06:10 mevdschee

Hi, this seems like a duplicate of #751.

Thank you for the hint, will have a look at the solution. I think the idea from pat123456 is a good one, to define the handlers outside of the config section. But it may be a huge rewrite for you? Would just be a clean solution without the need to other modifications. Yesterday I just made a quick&dirty workaround with a get_file_content to the same object I want to modify :)

Anyway, thanks for the quick response

kkrell2016 avatar Oct 04 '22 10:10 kkrell2016

But it may be a huge rewrite for you?

Yeah, that will be some work... and there is another solution with the VariableStore... did you try that?

mevdschee avatar Oct 04 '22 10:10 mevdschee

I tried it and it works great. I tried it this way, do you think its okey or is there a better option?

$config = VariableStore::get('config');
$db = new GenericDB(
    $config->getDriver(),
    $config->getAddress(),
    $config->getPort(),
    $config->getDatabase(),
    $config->getTables(),
    $config->getUsername(),
    $config->getPassword()
);

$pdo = $db->pdo();
stm = $pdo->prepare("SELECT oid from $tableName where ID = ?");

kkrell2016 avatar Oct 04 '22 23:10 kkrell2016