Skip to content

Commit

Permalink
feature #223 Allow easier override of the "new" action. (Pierstoval)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Discussion
----------

Allow easier override of the "new" action.

With this commit, we can easily customize the instanciation of our entities.

I'm proposing this modification because we have a specific case in which we only want to add default contents in a brand new instanciated object, but the content is retrieved via webservice, so I'm doing it in the controller.

Also, this simple workaround allows the users to override the method and create new `User` entity in the FOSUserBundle by returning a new User object using the
`$this->container->get('fos_user.user_manager')->createUser()` instruction.

I think this is useful 😃

Commits
-------

a937046 Added an "instanciateNewEntity" controller method
  • Loading branch information
javiereguiluz committed Apr 14, 2015
2 parents c9ce160 + a937046 commit 2bc0858
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ protected function showAction()
*/
protected function newAction()
{
$entityFullyQualifiedClassName = $this->entity['class'];
$item = new $entityFullyQualifiedClassName();
$item = $this->instanciateNewEntity();

$fields = $fields = $this->entity['new']['fields'];
$newForm = $this->createNewForm($item, $fields);
Expand Down Expand Up @@ -321,6 +320,18 @@ protected function ajaxEdit()

return new Response((string) $newValue);
}

/**
* Creates a new object of the current managed entity.
* This method is mostly here for override convenience, because it allows
* the user to use his own method to customize the entity instanciation.
*
* @return object
*/
protected function instanciateNewEntity() {
$entityFullyQualifiedClassName = $this->entity['class'];
return new $entityFullyQualifiedClassName();
}

/**
* Allows applications to modify the entity associated with the item being
Expand Down

0 comments on commit 2bc0858

Please sign in to comment.