Generate dynamically an entity

New topics about Symfony 2 should go here

Moderators: dcobalt, tiagojsag

Generate dynamically an entity

Postby noni94 » Mon Jul 30, 2012 10:10 am

Hi,

Working with S 2.0.* since ~ 1 year, and I have a question about entities.

I'm asking if it's possible to have an entity that is generated dynamically ? (ORM\Entity)

The aim goal for me is to update dynamically an User entity (with FOSUserBundle) from an admin where we can add/remove some properties (ie. the type bool, int, varchar, the getters and setters, the name, etc, of those properties)

And if it's possible, wich is the best way to do it.

Many thanks,
noni94
Junior Member
 
Posts: 9
Joined: Mon Jul 30, 2012 9:37 am

Re: Generate dynamically an entity

Postby dreipunktnull » Mon Jul 30, 2012 2:54 pm

Hi,

adding or removing properties would involve changing the database schema which is not a good idea. Why not add an array property that can hold all kind of stuff? Something like:
Code: Select all
/**
 * @var array
 * @ORM\Column(type="array", nullable=true)
 */
protected $extras;

[...]
public function setExtras(array $extras)
{
    $this->extras = $extras;
}

public function getExtras()
{
    return $this->extras;
}

public function hasExtra($extra)
{
    return array_key_exists($this->extras, $extra);
}

...
and so on. You'll get the idea.
User avatar
dreipunktnull
Faithful Member
 
Posts: 1244
Joined: Fri Jun 17, 2011 10:03 am
Location: Germany

Re: Generate dynamically an entity

Postby noni94 » Mon Jul 30, 2012 3:09 pm

dreipunktnull wrote:Hi,

adding or removing properties would involve changing the database schema which is not a good idea. Why not add an array property that can hold all kind of stuff? Something like:
Code: Select all
/**
 * @var array
 * @ORM\Column(type="array", nullable=true)
 */
protected $extras;

[...]
public function setExtras(array $extras)
{
    $this->extras = $extras;
}

public function getExtras()
{
    return $this->extras;
}

public function hasExtra($extra)
{
    return array_key_exists($this->extras, $extra);
}

...
and so on. You'll get the idea.


Actually, it's obviously the only solution (it doesn't change the entity model=>database)

But how for the form generation and submission (any key=>value of the $extras may be of different type, bool, miltiple, etc) ?

I'm asking for the best way to do it properly :)
noni94
Junior Member
 
Posts: 9
Joined: Mon Jul 30, 2012 9:37 am

Re: Generate dynamically an entity

Postby dreipunktnull » Mon Jul 30, 2012 9:15 pm

Ah, forms. Ok, haven't thought about them. Should be possible though with a clever combination of a DataTransformer and EventListener. Question is how users can choose which fields to add.
User avatar
dreipunktnull
Faithful Member
 
Posts: 1244
Joined: Fri Jun 17, 2011 10:03 am
Location: Germany

Re: Generate dynamically an entity

Postby noni94 » Tue Jul 31, 2012 7:56 am

dreipunktnull wrote:Ah, forms. Ok, haven't thought about them. Should be possible though with a clever combination of a DataTransformer and EventListener. Question is how users can choose which fields to add.


In fact, the user will be able to complete all the datas that are stored in the entity, and those that are added via the admin
Question is, how is the best way to do it :)
noni94
Junior Member
 
Posts: 9
Joined: Mon Jul 30, 2012 9:37 am


Return to General Symfony 2 discussion

Who is online

Users browsing this forum: No registered users and 2 guests