-
Notifications
You must be signed in to change notification settings - Fork 9
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
Bug fixation of ODM\MongoDB\Types\CurrencyType #21
Changes from all commits
6ae2098
57c6d31
1e9774e
659e434
4330d3e
51aeb14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,10 @@ | |
|
||
namespace ZFBrasil\DoctrineMoneyModule\ODM\MongoDB\Types; | ||
|
||
use Doctrine\ODM\MongoDB\Types\StringType; | ||
use Doctrine\ODM\MongoDB\Types\Type; | ||
use Money\Currency; | ||
|
||
class CurrencyType extends StringType | ||
class CurrencyType extends Type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you revert this one? We still can inherit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sry no, this is an falsely behavior Currency isn't a string type, BTW doctrine2 and proxy manager (we used) doesn't automatically cast to string. Not useabale with u proposed __toString There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, in DB side it is a string, so I think it's ok to inherit VO -> string conversions from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. almost all data mapped to string on database side, is more interesting PHP side. because we speak from ODM (ORM) here. Look at times http://doctrine-dbal.readthedocs.org/en/lates/reference/types.html#custom-mapping-types for u should be a float type :). (may better way ObjectType -> serializing of money inclusive currency as one field) |
||
{ | ||
const NAME = 'currency'; | ||
|
||
|
@@ -14,6 +14,15 @@ public function getName() | |
return self::NAME; | ||
} | ||
|
||
public function convertToDatabaseValue($value) | ||
{ | ||
if ($value) { | ||
return (string) $value; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function convertToPHPValue($value) | ||
{ | ||
if ($value === null || $value instanceof Currency) { | ||
|
@@ -22,4 +31,9 @@ public function convertToPHPValue($value) | |
|
||
return new Currency($value); | ||
} | ||
|
||
public function closureToPHP() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @olekhy Can you explain the purpose of this method? I'm not familiarized with this conversion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
return '$return = new \Money\Currency($value);'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should probably consider cases when return '$value !== null ? new \Money\Currency($value) : null'; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is never null because money w/o currency is a nonsense! :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be null! Currency can be used outside of money context and could be null. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree in this abstraction |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of
mathiasverraes/money
is to always store and work with money represented in the smallest unit (because of rouding erros).Yeah, of course we don't want to display it in cents for our user, that's why the
$amount / 100
is a presentation concern, so that you must handle it in view layer. I think a custom form element does the trick.Thoughts? @olekhy @prolic @fabiocarneiro @gabrielsch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A user will always enter "1.25 USD". We have to have 125 as amount value. So putting this behaviour in a custom form element, which translates (in zf2 terms: filters) the input (1.25) to 125. The hydrator then does not need to divide by 100. Good catch @danizord !
About the closureToPhp and closureToMongo methods: They are indeed needed for doctrine odm mongodb adapter to work. They do not exist in doctrine orm.
We tested the current mongo implementation (which I contributed to, too), but the automatic string casting does not happen. Additionally we have to concider the null checks. So this in needed in order to work cleanly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@prolic @danizord