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

[RFC] additional fields in select query #83

Closed
parsfanavar opened this issue Nov 29, 2019 · 5 comments · Fixed by #106
Closed

[RFC] additional fields in select query #83

parsfanavar opened this issue Nov 29, 2019 · 5 comments · Fixed by #106

Comments

@parsfanavar
Copy link

How can add additional field in select query which include its value to object properties (_rest)

select table.*, concat(firstname," ",lastname) as fullname

@parsfanavar parsfanavar added the new feature For new features label Nov 29, 2019
@jcheron
Copy link
Contributor

jcheron commented Nov 30, 2019

Thank you for this contribution.
I will have two reticences to make this change:

  • Database and ORM operations are often more performance expensive than PHP code execution.
  • I think it is better to keep an object or business approach.

Since ORM uses setters to assign values to object members, why not go through such a solution:

class User {
    ...
	private function updateFullname(){
		$this->fullname = $this->firstname . ' ' . $this->lastname;
		$this->_rest['fullname'] = $this->fullname;
	}
	
	public function setFirstname($firstname) {
		$this->firstname = $firstname;
		$this->updateFullname();
	}

	public function setLastname($lastname) {
		$this->lastname = $lastname;
		$this->updateFullname();
	}
}

or we would need to add a Transformer on the models, but this doesn't exist yet...

@parsfanavar
Copy link
Author

What I mentioned in previous post was a simple example query of what I meant. What a bout formula field, or case when ... else ... field, etc. In addition I think the above fields would be better calculated in database engine instead of foreach in php, isn't it?

@jcheron
Copy link
Contributor

jcheron commented Nov 30, 2019

I think the above fields would be better calculated in database engine instead of foreach in php, isn't it

If we compare the query to php processing, in a raw query context: yes
But the introduction of this feature requires additional processing at the ORM level:

  • new mapping to be registered
  • new processing to build the request

It would be less time-consuming if it was done on UQueries, such as Doctrine with DQL

I'll do some tests (not right now I'm working on something else), and I'll keep you informed.

@jcheron jcheron added this to Soon in Roadmap Dec 15, 2019
@UlasSAYGINIM
Copy link
Contributor

@parsfanavar hi, i hope you are doing well.
can you give examples for your usage?
i wonder it. I am new for ubiquity. trying to get what you want completely?

@jcheron
Copy link
Contributor

jcheron commented Feb 15, 2020

@UlasSAYGINIM
For me, the requirement consists in giving the possibility to add fields (calculated or not) to the SELECT part of the SQL query, and to associate this result to a member created at runtime of the loaded instances.

I think the DAOPreparedQueries should do that.
These are queries to prepare the ORM processing for future loading of instances, via getById, getOne or getAll, which is especially interesting with the Swoole or Workerman platforms.

Since these objects are stored, it is possible to add mapping information to them.

In practice

DAO::prepareGetAll('users',\models\User::class);
DAO::getPrepared('users')->addMember('CONCAT(firstname," ",lastname)','fullname');
$users=DAO::executePrepared('users');//each user will have a fullname additional member

Concerned classes:

Classes Role
DAOPreparedTrait Defines the methods accessible from the DAO class.
DAOPreparedQuery PreparedQueries base class.

jcheron added a commit that referenced this issue Feb 15, 2020
jcheron added a commit that referenced this issue Feb 25, 2020
@jcheron jcheron mentioned this issue Mar 10, 2020
1 task
jcheron added a commit that referenced this issue Mar 10, 2020
* Prepares to add new data to the load

see #83

* update comment

* [skip ci] add additional fields in select query

see #83
@jcheron jcheron moved this from Soon to Done in Roadmap Mar 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging a pull request may close this issue.

3 participants