php-crud-api
php-crud-api copied to clipboard
Access to db in handler
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.
Hi, this seems like a duplicate of #751.
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
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?
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 = ?");