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

Simple property mapper for ActiveRecord. #28

Open
mj4444ru opened this issue Oct 20, 2020 · 7 comments
Open

Simple property mapper for ActiveRecord. #28

mj4444ru opened this issue Oct 20, 2020 · 7 comments
Assignees
Labels
status:ready for adoption Feel free to implement this issue. type:feature New feature

Comments

@mj4444ru
Copy link

mj4444ru commented Oct 20, 2020

It would be great if the generated ActiveRecord had a property mapper. That is, create_at was mapped to createAt.
For example like this:

public function getAttributeMap(): array
{
    return [
        'createAt' => 'create_at',
    ];
}

public function __get(string $name)
{
    return parent::__get($this->getAttributeMap()[$name] ?? $name);
}

public function __set(string $name, $value): void
{
    parent::__set($this->getAttributeMap()[$name] ?? $name, $value);
}

public function __isset(string $name): bool
{
    return parent::__isset($this->getAttributeMap()[$name] ?? $name);
}

public function __unset(string $name): void
{
    return parent::__unset($this->getAttributeMap()[$name] ?? $name);
}

@property will be appropriate.

@samdark
Copy link
Member

samdark commented Oct 21, 2020

How would that look like in queries? Would real field name be used or an alias that we have defined?

@mj4444ru
Copy link
Author

This improvement only affects magical properties. When working with properties through functions, we continue to work with real names.

For example, we have a create_at property in the database table.
When the class is generated, the getAttributeMap method is generated. and a property declaration like:

/**
 * @property int $createAt
 */

we can access the property like this:

$time = $model->getAttribute('create_at');
$time = $model->createAt;
$time = $model->create_at;

The editor will prompt us $model->createAt. It looks better.

@samdark
Copy link
Member

samdark commented Oct 24, 2020

While it looks better, it's not convenient when you have createAt in code but create_at in query conditions in the same code.

@mj4444ru
Copy link
Author

While it looks better, it's not convenient when you have createAt in code but create_at in query conditions in the same code.

  1. If in your project all properties are converted in this way, you perfectly understand where and what to expect.
  2. These are different levels of abstraction.
  3. Do ORM users have such problems?

@samdark
Copy link
Member

samdark commented Oct 24, 2020

  1. Depends on the ORM. Doctrine has a special DQL query language that allows you to use mapped properties in conditions so you have same names everywhere but that comes with its own set of drawbacks.

@mj4444ru
Copy link
Author

In any case, it is not difficult, and I have no doubt that many will like it. This can also be made optional.

@samdark samdark added status:ready for adoption Feel free to implement this issue. and removed status:under discussion labels Oct 29, 2020
@samdark
Copy link
Member

samdark commented Oct 29, 2020

Alright. Having an option to generate these is fine.

@rustamwin rustamwin self-assigned this Apr 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:feature New feature
Projects
None yet
Development

No branches or pull requests

3 participants