-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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: Entity can't handle column named attributes
#5762
Comments
Until the update comes out, I offer my temporary solution. Wrap over "Entity" <?php
namespace App\Helpers;
use CodeIgniter\Entity\Entity;
class EntityModify extends Entity
{
private $clearFields = ["attributes","casts","datamap"];
//Фикс метода fill дабы все не ебнулось, когда в данных пришло что-то из списка ;)
public function fill(?array $data = null)
{
if(is_array($data))
{
foreach (array_keys($data) as $key) {
if (in_array($key, $this->clearFields, true)) {
unset($data[$key]);
}
}
}
return parent::fill($data);
}
//Добавляем поля которые не должны быть в объекте описывающем таблицу но очень хочется их видеть в ответе ;)
public function addMoreInfo($moreInfo)
{
foreach ($moreInfo as $key => $value)
{
$this->attributes[$key] = $value;
}
}
} |
What happened in Laravel 9? |
Damn, I wish we could just rename the I am wondering if that can be considered a bug, though. If others think so then I say we do that. That is preferable to any additional complexity in the logic that we would have to maintain, and it's the only method that would be affected by this. |
attributes
Renaming the It seems it is undocumented. |
@kenjis In version 9, the getter and setter is the method equivalent to the attribute name. (using php8 functionality) protected function firstName(): Attribute
{
return Attribute::make(
get: fn ($value) => ucfirst($value),
set: fn ($value) => strtolower($value),
);
} There is a possibility that there is an application that has its own Entity implementation with the appropriate setAttributes() method. I do not consider it correct to rename the setAttributes() method, since the name fully reflects the essence of this method. I also think that the toRawArray() method should be called getAttributes(). It would be more correct to change the format of the names of setters and getters. |
@the-gt99 Your proposal is a special case. |
It is a much larger breaking change to update the format of the getters and setters and that's not something that could happen right now, I don't think. I'm a little leery of changing the name, honestly, but it's a pretty simple fix for people to change and I think it can legitimately be called a bug. Naming it something like I am very interested to from @MGatner @paulbalandan and @samsonasik before we proceed with any changes though. |
The current |
The problem is that no matter how the setAttributes() method is renamed, there will be a chance that the method name will match the table field. |
See my PR: #5763 |
For example, we add the |
@iRedds Probably I got you want to say. |
Removing Personally I can't say specifics right now but I'm pretty sure some of my libraries extend |
I still think that for separate setters (mutators) it is necessary to change the name format. Like Laravel. |
I think we will have to table this for the future. If this were a personal/company application I'd say we fix it right now, but it's not, and we have a contract with thousands of developers to not break their apps on a non major release. This rules out changing the naming scheme of the setters, or of the In the future, changing the naming scheme would be appropriate. The two options on the table at the moment are |
I'll also point out that this is precisely how Rails sets all model properties in bulk: user = User.find params[:user_id]
user.attributes = filtered_params
user.save! I have no need to mimic Rails but I mention it because I don't think it is wildly problematic to "reserve" |
@MGatner Do you mean RoR can't use |
Correct. It is included in the (extensive) reserved word list. |
I thought that the inability to use I sent a PR to update documentation: #5809 |
I'm not opposed to "deeper" solutions but I think this is the most straightforward for now. |
Revisiting... @kenjis updated the docs so |
As for collision of methods that need to be public: I would still like to find a better solution in the future. I tend to name my entity methods without "get/set" because of this, which always feels like an undesirable concession: https://github.com/tattersoftware/codeigniter4-firebase/blob/develop/src/Firestore/Entity.php |
Fixed by #7208 |
CodeIgniter4 Version
4.1.9
What happened?
When using
attributes
, as the property/attribute name of the Entity class, theEntity::setAttributes()
method is called as a setter.Names collision.
Steps to Reproduce
Expected Output
Anything else?
In Laravel prior to version 9, the custom setter and getter had the following format:
set/get + AttributeName + Attribute.
The text was updated successfully, but these errors were encountered: