Symfony call get by Name from variable

TheTom

I would like to call a getter with the stored fieldname from the database.

For example, there are some fieldnames store like ['id','email','name'].

$array=Array('id','email','name');

Normally, I will call ->getId() or ->getEmail()....

In this case, I have no chance to handle things like this. Is there any possibility to get the variable as part of the get Command like...

foreach ($array as $item){
   $value[]=$repository->get$item();
}

Can I use the magic Method in someway? this is a bit confusing....

Guillaume Fache

You can do it like this :

// For example, to get getId()
$reflectionMethod = new ReflectionMethod('AppBundle\Entity\YourEntity','get'.$soft[0]);
$i[] = $reflectionMethod->invoke($yourObject);

With $yourObject being the object of which you want to get the id from.

EDIT : Don't forget the use to add :

use ReflectionMethod;

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get a variable name from database

From Dev

Call jQuery function from variable name directly

From Dev

Get method name from lambda without call

From Dev

Get local variable from ssh call in shell

From Dev

Get name of function from loop variable

From Dev

Get product name from WMIC for a variable in batch

From Dev

How to hide get variable name from url

From Dev

Get variable name from NameError object

From Dev

Get The Class Name From an Object Variable

From Dev

How to get the array name from a variable

From Dev

Java - get variable name from double array

From Dev

How to get a field name from an instance variable

From Dev

Get variable name from value in Swift

From Dev

Get dictionary value from variable name constructed from variable values

From Dev

Get month name in Symfony

From Dev

R - Call a function from function name that is stored in a variable?

From Dev

How to call a function from a module name that is assigned to a variable

From Dev

Symfony: Send variable throught GET to handler from Ajax

From Dev

Get variable from parameter file in symfony2

From Dev

How to get file name from url without $_GET variable?

From Dev

How to get file name from url without $_GET variable?

From Dev

Get Class from string -- Call function by string name

From Dev

Can I get variable Name and Method name of @Test in After Test call?

From Dev

Perl can't get variable from module, but can call functions

From Dev

Perl can't get variable from module, but can call functions

From Dev

Call script based on variable name

From Dev

Call a generated variable name in vbScript

From Dev

Get name of a repository var in Symfony

From Dev

How to get response variable name from summary.glm

Related Related

HotTag

Archive