I'm developing an sale system with an iPad front-end and a Symfony2 based server. The comunication between them is made by POST method in jSon format.
I have seen that there is a posibility of binding a form into an entity but I don't know if there would be possible to do that if I recive a jSon object.
For example this is what I have in the front end(for simplicity is in Javascript):
var sale=new Sale();
sale.client=10;
sale.user=1;
sale.product=11;
sale.quantity=100;
var jSon={"client": sale.client,
"user":sale.user,
"product":sale.product,
"quantity":sale.quantity}
$.post("http://examplepath.com/new_sale", jSon,
function (data) {
if (data) {
alert(data);
}
else {
alert("Not working :-(");
}
}
);
Now when I recive the jSon in the I do something like this:
$sale=new Sale(); //This is my entity :)
$sale->setUser($request->request->get("user"));
$sale->setClient($request->request->get("client"));
$sale->setProduct($request->request->get("product"));
$sale->setDate($date);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($sale);
$em->flush();
Is there any way to bind the jSon recieved by POST with my Sale entity without doing all those nasty setters ?
What you are looking for is called Serialization
.
You can use the default Serializer component provided by Symfony 2, but a more handy approach would be by using JMSSerializerBundle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With