spadefoot / kohana-orm-leap

An ORM module for the Kohana PHP framework that is designed to work with all major databases.
http://spadefoot.github.io/kohana-orm-leap/
100 stars 25 forks source link

boolean support for PostgreSQL #48

Closed taai closed 12 years ago

taai commented 12 years ago

In PostgreSQL the booleans are t and f, so the

<?php
$value = $result == TRUE;

will not work.

You have to parse the result value like this:

<?php
$value = $result === 't';

Anyways, I can't find where all the values get's parsed...

bluesnowman commented 12 years ago

We can change the insertion value in leap/classes/base/db/postgresql/expression.php on lines 319-324 from:

else if ($expr === TRUE) {
    return "'1'";
}
else if ($expr === FALSE) {
    return "'0'";
}

To the following:

else if ($expr === TRUE) {
    return "'t'";
}
else if ($expr === FALSE) {
    return "'f'";
}

As for handling the return value, you can temporarily define the field as a string in your model and restrict the values using the 'enum' attribute and set a 'max_length' of 1. You can then create a field adaptor to convert the string into a boolean. I know this is not an ideal solution to handling returned boolean values but I will look into a more long term solution.

bluesnowman commented 12 years ago

Field adaptors are often the answer to handling such differences between SQL dialects. Field adaptors really come in handy when dealing with dates, where some SQL dialects store their dates as integers. With field adaptors, you can convert returned values of a certain data type into values of a different data type. Such as a string into a boolean.

In your example, you can avoid having to do that comparison every time once you set up a field adaptor to handle that comparison for you. Essentially, you retrieve the value from the field adaptor instead of from the field itself. This is all handled by the field adaptor's __get($key) method.

Likewise, you can use field adaptors to set field values. In this case, the field adaptor will convert your php TRUE value to a string of 't' and a php FALSE value to a string of 'f'. Just define that conversion in the field adaptor's __set($key, $value) method.

If you choose to create a field adaptor to handle this situation, please feel free to submit a pull request and we will add it to our commonly used field adaptors. All field adaptors for LEAP are stored in leap/classes/base/db/orm/field/adaptor

taai commented 12 years ago

Actually what I did - I made a special Field class Base_DB_PostgreSQL_ORM_Field_Boolean that is same as Base_DB_ORM_Field_Boolean, just for PostgreSQL booleans.

I could share this, just tell me if the place (in directories) for that file is ok, or it should be somewhere else?

Also I think that it would be great, if I can use DB_ORM_Field_Boolean and it had compatibility with database type it uses, so I don't have to touch my Models.

Any ideas?

bluesnowman commented 12 years ago

If you want to fork the project and commit the Boolean field class for PostgreSQL there, I can pull down the changes from your fork. I will then see what we can do to integrate it into the core project.