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

Apply camelCase to assumed column name attrib if it contains relation #123

Open
wants to merge 1 commit into
base: 4.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Html/Options/HasColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public function columnDefs(array $value)
return $this;
}

private function camelRelation($column)
{

$parts = explode('.', $column);
$columnName = array_pop($parts);
$relation = Str::camel(implode('.', $parts));

return $relation . ($relation ? '.' : '') . $columnName;
}
/**
* Set columns option value.
*
Expand All @@ -43,14 +52,14 @@ public function columns(array $columns)
if (is_array($value)) {
$attributes = array_merge(
[
'name' => $value['name'] ?? $value['data'] ?? $key,
'name' => $value['name'] ?? $this->camelRelation($value['data'] ?? $key),
Copy link
Owner

Choose a reason for hiding this comment

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

I think this is not needed on this part since not all name are on relation. This might fail if the column is indeed have underscores on it? I think it's better to do a camelCase on the part where eagerLoading is being checked.

Copy link
Owner

Choose a reason for hiding this comment

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

Or this might also fail if the query uses join statements? Anyways, will also try to check on actual projects when I got chance. Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

Well, the camelRelation function only applies the camel case to the relation part of the name. For example, camelRelation('last_name') -> 'last_name', but camelRelation('job_contacts.last_name') -> 'jobContacts.last_name'

And of course if the 'name' is actually specified in the columns array to begin with, it will not be altered.

I will see if I can write some test classes for this when I get a chance, as I think it would really help to show things like the query join situations you mention.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, a test would be great. For join queries, it is advised to include table.column when setting the name which is why this might become a potential breaking change.

'data' => $value['data'] ?? $key,
],
$this->setTitle($key, $value)
);
} else {
$attributes = [
'name' => $value,
'name' => $this->camelRelation($value),
'data' => $value,
'title' => $this->getQualifiedTitle($value),
];
Expand Down