-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Product Attributes are not typed correctly #39886
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
Comments
Hi @gwharton. Thank you for your report.
Join Magento Community Engineering Slack and ask your questions in #github channel. |
Also note, this is not just specific to product attributes. If you create a database table with a series of colums of type int, then use your model factory to create and load an instance of your model from the database, the resulting model object will have values set to strings, not ints, as per the database. |
OK, the fix to the DB Model not typing things correctly, is to add the following PDO option to your driver options in env.php PDO::ATTR_STRINGIFY_FETCHES = false (Option 17) This option was added in 2.4.4-p1 to default to true, which means all model loads from that point on, load all model data as strings!!! I have no idea why this change would be introduced, but for me, I want my model types to match my database types. This doesn't fix the typing of product attributes which are loaded by a different mechanism. EDIT : If you set this, other modules in Magento break. e.g module-security is expecting the admin user ID to be a string of the numerical user id. |
Hi @engcom-Hotel. Thank you for working on this issue.
|
Hello @gwharton, Thanks for the report and collaboration! We have tried to reproduce the issue in 2.4-develop branch, and it is working as the actual result mentioned above. But we have some observation here, even if we add in
But when we add
the issue seems to be solved. Thanks |
I've mixed two problems here The first is that product attributes are not typed correctly. The second is that model types in general (but not product) are not typed. Adding the PDO option 17 resolves the second issue, that general model values are not typed correctly, but this then breaks other things in core, that have been written to expect things to be broken. Ignore this second issue for now, and concentrate on product attributes. Product attributes are obtained differently than general model values. I used product->getStatus as an example that should return an int, but if you xdebug step and inspect any product and it's attributes, you will see that any attribute with the type int, is represented as a string. In my opinion, the fix for this is to fix the underlying product attribute mechanism to respect the attribute type without treating everything as a string, and not hard code cast the string to an int in the getStatus call which is just masking the underlying problem. |
Preconditions and environment
Tested on
Steps to reproduce
Expected result
Return type of getStatus() is : int
Actual result
Return type of getStatus() is : string
Additional information
The Status Product Attribute is defined in core Magento as an integer.
When products are created, the Attribute "status" is stored in the
catalog_product_entity_int
table confirming it is typed as integer.The Magento ProductInterface defines the call getStatus() as returning an int or null.
magento2/app/code/Magento/Catalog/Api/Data/ProductInterface.php
Lines 133 to 138 in c8ba4ab
Yet calls to $product->getStatus() return a string.
The same thing happens when retrieving float values, for example $product->getPrice() returns a string despite the Interface mandating float or null.
magento2/app/code/Magento/Catalog/Api/Data/ProductInterface.php
Lines 118 to 123 in c8ba4ab
EDIT : I'm willing to concede on getPrice as Price is stored as a DECIMAL type in MySQL which is a fixed precision string representation of a floating point number, and not a float.
Release note
No response
Triage and priority
The text was updated successfully, but these errors were encountered: