dbal
dbal copied to clipboard
DB numeric type should map to PHP string type
Converting lossless numeric (= decimal) to lossy float is a bad idea.
Not sure if this is a good idea. In the end, the majority will still convert it to float, manually or by php auto-coercion.
AFAIK the only reason to use numeric is when you need precise numbers (i.e. money amount). For „normal“ float numbers you should just use real / double precision type.
For „normal“ float numbers you should just use real / double precision type.
This describes exactly the problem. The normal float are just regular floats where actually almost nobody care (do something with it) about the precession loss. Dbal itself will preserve the information in string format, but programmer in the end will convert it to float manually.
I'm not strongly against this, but I'm not sure if this actually solves any real problems instead of removing some behavior. In any case, you may the normalization manually turn off, we may better implement a feature for custom column normalization turning off.
My point is that the default normalization which is done automatically by dbal should always by lossless.
Hi, any news on this topic or have something in the meantime crossed your mind about how to allow decimals to be converted to string (to use some library like phpdecimals etc.)?
The latest recent news is that there will be a setter for explicit conversion type. WIP here: https://github.com/nextras/dbal/commit/ea47124657be658775e65713e99ebaafd16d7ae2
Great, thank you for the information. Can I help somehow?
My workaround for postgres and nextras orm:
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Helpers\DbalQueryBuilderHelper;
use Nextras\Orm\Mapper\Dbal\DbalMapper;
final class ExamplePostgresMapper extends DbalMapper
{
public function builder(): QueryBuilder
{
$builder = parent::builder();
$tableName = $this->getTableName();
$alias = DbalQueryBuilderHelper::getAlias($tableName);
$builder->addSelect(sprintf('"%s"."column"::text', $alias));
return $builder;
}
}