Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom boolean value casting #242

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Add custom boolean value casting #242

wants to merge 7 commits into from

Conversation

leightonshank
Copy link

I ran into an issue where the default PHP casting of false boolean values (PHP casts false to an empty string) didn't work with a PostgreSQL database which doesn't accept an empty string for a boolean value.

To get around this, I added a new BOOLEAN column type, updated the cast() method in the Column class to delegate to the Connection class for converting a boolean to string values. By default it still uses the standard PHP casting (i.e. (string)$value), but can be overridden in an adapter connection class for custom handling of boolean conversion.

@simonxy
Copy link

simonxy commented Mar 18, 2013

I'm glad finding this. It's a must-have, at least for PgSQL. Thanks! Why above commits are not in nightly build?

@al-the-x
Copy link
Collaborator

@jpfuentes2 take a look at this PR when you have a chance. Other ORMs I have worked with behave similarly, delegating the conversion of types to the adapters. Not every vendor accepts the same values for booleans, dates, etc.

@jpfuentes2
Copy link
Owner

I'm in favor of this strategy. That being said, do you think you could fix the indentation formatting? It's not in harmony with the existing code. Also, would you add a specific test to PgsqlAdapterTest.php for your changes?

Thanks

@leightonshank
Copy link
Author

@jpfuentes2 sorry to have taken so long to get back on this, I think it should be good if it looks good to you. Thanks for looking at this PR!

I've fixed the formatting and added a specific test to PgsqlAdapterTest.php. I also modified the PgsqlAdapter boolean_to_string method to take into account values that PostgreSQL thinks are boolean, but PHP does not (e.g., 'f', 'false', 'y', etc...).

Thanks again for checking this out, you have a great project here - glad to be able to contribute back to it. I have also put in another PR #243 for a feature that I've found very handy in handing virtual attributes if you care to take a look.

@Rican7
Copy link
Collaborator

Rican7 commented Apr 30, 2013

I personally like this one 👍

@al-the-x
Copy link
Collaborator

al-the-x commented May 1, 2013

Absolutely essential for PgSQL support. 👍

@@ -30,6 +31,8 @@ class Column
'date' => self::DATE,
'time' => self::TIME,

'boolean' => self::BOOLEAN,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extraneous whitespace.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What whitespace are you referring to?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an associative array and all other keys => values are on subsequent lines except for your addition. Perhaps this was a previous issue, but could you tighten it up?

Thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably is because the array is somewhat divided into types, and boolean is its own 'type', see here

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stand corrected. Sorry!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. It does look a bit odd in the diff!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to remove those blank lines separating the different types? I thought it sounded like we'd leave it as is, but maybe I misunderstood. Are you waiting on me for anything to tidy up to merge this PR?

@al-the-x al-the-x mentioned this pull request Jul 29, 2013
@koenpunt koenpunt mentioned this pull request Feb 27, 2014
@koenpunt
Copy link
Collaborator

@leightonshank Could you rebase your branch on master?

The default PHP boolean-to-string casting does not work for some
database connection types (e.g. PostgreSQL).  Added a BOOLEAN
type to the Column class, and then the cast() method delegates
to the a new boolean_to_string() method in the Connection class.

By default the boolean_to_string() method returns the standard
PHP string cast for the boolean, retaining the original behavior.
This method can be overridden in adapter classes to provides custom
casting of boolean values.

Added an overridden boolean_to_string() method into the
adapters/PgsqlAdapter.php class which casts the boolean values
false and true to the strings "0" and "1" respectively.  These
string values are then recognized by the Postgres database as
boolean values, as opposed to the values "" and "1" generated
by the standard PHP cast.
Updated the ColumnTest::assert_cast() to test that boolean values are
being casted using the Connection boolean_to_string() method.

Added a PostgreSQL specific test to ensure that boolean values are
being casted as the correct string value.
Modified the boolean_to_string() method in the PgsqlAdapter class to
properly convert values that PHP considers a boolean, as well as
what PostgreSQL considers a boolean value.

E.g., PostgreSQL considers the values 'f', 'false', 'n', 'no', and
'off' as valid values for `false`, but PHP does not.  The original
implementation of this method would return `true` for one of these
values which is incorrect for a PostgreSQL database.

Also modified the base Connection class boolean_to_string() method
to first cast its `$value` parameter as a boolean before casting
it to a string.  This will account for PHP's loose type-casting
and make sure the method returns a consistent casted value.
While the previous version was explicit and descriptive, it wasn't
that elegant.  This is much nicer.
@leightonshank
Copy link
Author

@koenpunt Sorry for the delay on this. This branch has been rebased on master.

Another thing that I just noticed -- The Travis CI build failed so I went to investigate why. Travis is reporting an error:

PHP Warning:  require_once(PHPUnit/Framework/TestCase.php): failed to open stream: No such file or directory in /home/travis/build/jpfuentes2/php-activerecord/test/helpers/config.php on line 17

After investigating, it looks like Travis CI updated phpunit to v4.0.19 from v3.7.31. Seems that between v3 and v4 of PHPUnit, things changed and PHPUnit/Framework/TestCase.php either doesn't exist anymore or has changed names.

I ran the tests on my local machine with phpunit v3 and they passed. Have you guys seen this issue with the updated version of PHPUnit on Travis? Any plans to address it?

@koenpunt
Copy link
Collaborator

Don't know about an updated PHPUnit but we can probably lock it to a specific version. Currently on mobile so I'm not able to investigate further

@jpfuentes2
Copy link
Owner

Thanks to @koenpunt this should be resolved. Can we get master merged to rebuild with the changes?

@leightonshank
Copy link
Author

Yup I'll merge the master in this afternoon for ya.

Sent from my iPhone

On May 19, 2014, at 10:27 AM, Jacques Fuentes notifications@github.com wrote:

Thanks to @koenpunt this should be resolved. Can we get master merged to rebuild with the changes?


Reply to this email directly or view it on GitHub.

@leightonshank
Copy link
Author

Alright, master is merged in and the build is green. Nice fix @koenpunt -- I had just started looking at doing the same thing Friday afternoon 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants