Skip to content

Instantly share code, notes, and snippets.

@jblanche
Created November 24, 2011 11:41
Show Gist options
  • Save jblanche/1391164 to your computer and use it in GitHub Desktop.
Save jblanche/1391164 to your computer and use it in GitHub Desktop.
# Avec Rails on écrirait :
def create
@product = Product.create({name => 'A Foo Bar', price => 19.99, description => 'Lorem ipsum dolor'})
end
# ou plutôt (pour gérer les validations et redirections, non gérées dans l'exemple Symfony)
def create
@product = Product.new({name => 'A Foo Bar', price => 19.99, description => 'Lorem ipsum dolor'})
if @product.save
redirect_to @product
else
render :action => "new"
end
end
// src/Acme/StoreBundle/Controller/DefaultController.php
use Acme\StoreBundle\Entity\Product;
use Symfony\Component\HttpFoundation\Response;
// ...
public function createAction()
{
$product = new Product();
$product->setName('A Foo Bar');
$product->setPrice('19.99');
$product->setDescription('Lorem ipsum dolor');
$em = $this->getDoctrine()->getEntityManager();
$em->persist($product);
$em->flush();
return new Response('Created product id '.$product->getId());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment