Symfony4 setter where getter match id route?











up vote
0
down vote

favorite












I'm kindly new on Symfony



I'm doing a vote system but i guess this should work for like,



At the moment my controller function is this, this only create a new row with 1vote, but not update any $id created before.



/**
* @Route("/public/{id}/vote", name="poll_vote", methods="GET|POST")
*/
public function vote(Request $request, Poll $poll): Response
{
$inc = 1;
$em = $this->getDoctrine()->getManager();
$entity = new Poll();
$entity->setVotes($inc++);
$em->persist($entity);
$em->flush();
}
return $this->redirectToRoute('poll_public');
}


This is my button from twig template



<a href="{{ path('poll_vote', {'id': poll.id}) }}">


An this is my entity



  class Poll
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="string", length=255)
*/
private $name;

/**
* @ORMColumn(type="integer", nullable=true)
*/
private $votes;

public function getId(): ?int
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getVotes(): ?int
{
return $this->votes;
}

public function setVotes(?int $votes): self
{
$this->votes = $votes;

return $this;
}
}


I have no idea about how can match my getID from my entity and match for the $id from the @Route.



Any guide or suggestion would be really appreciate.



Thanks



EDIT:



Updated with the correct function after Arne answer:



/**
* @Route("/public/{id}", name="poll_vote", methods="GET|POST")
*/
public function vote($id)
{
$entityManager = $this->getDoctrine()->getManager();
$poll = $entityManager->getRepository(Poll::class)->find($id);

if (!$poll) {
throw $this->createNotFoundException(
'No polls found for id '.$id
);
}

$poll->setVotes($poll->getVotes()+1);
$entityManager->flush();

return $this->redirectToRoute('poll_public', [
'id' => $poll->getId()
]);
}









share|improve this question
























  • $request->query->get('id')
    – u_mulder
    Nov 11 at 9:07















up vote
0
down vote

favorite












I'm kindly new on Symfony



I'm doing a vote system but i guess this should work for like,



At the moment my controller function is this, this only create a new row with 1vote, but not update any $id created before.



/**
* @Route("/public/{id}/vote", name="poll_vote", methods="GET|POST")
*/
public function vote(Request $request, Poll $poll): Response
{
$inc = 1;
$em = $this->getDoctrine()->getManager();
$entity = new Poll();
$entity->setVotes($inc++);
$em->persist($entity);
$em->flush();
}
return $this->redirectToRoute('poll_public');
}


This is my button from twig template



<a href="{{ path('poll_vote', {'id': poll.id}) }}">


An this is my entity



  class Poll
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="string", length=255)
*/
private $name;

/**
* @ORMColumn(type="integer", nullable=true)
*/
private $votes;

public function getId(): ?int
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getVotes(): ?int
{
return $this->votes;
}

public function setVotes(?int $votes): self
{
$this->votes = $votes;

return $this;
}
}


I have no idea about how can match my getID from my entity and match for the $id from the @Route.



Any guide or suggestion would be really appreciate.



Thanks



EDIT:



Updated with the correct function after Arne answer:



/**
* @Route("/public/{id}", name="poll_vote", methods="GET|POST")
*/
public function vote($id)
{
$entityManager = $this->getDoctrine()->getManager();
$poll = $entityManager->getRepository(Poll::class)->find($id);

if (!$poll) {
throw $this->createNotFoundException(
'No polls found for id '.$id
);
}

$poll->setVotes($poll->getVotes()+1);
$entityManager->flush();

return $this->redirectToRoute('poll_public', [
'id' => $poll->getId()
]);
}









share|improve this question
























  • $request->query->get('id')
    – u_mulder
    Nov 11 at 9:07













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm kindly new on Symfony



I'm doing a vote system but i guess this should work for like,



At the moment my controller function is this, this only create a new row with 1vote, but not update any $id created before.



/**
* @Route("/public/{id}/vote", name="poll_vote", methods="GET|POST")
*/
public function vote(Request $request, Poll $poll): Response
{
$inc = 1;
$em = $this->getDoctrine()->getManager();
$entity = new Poll();
$entity->setVotes($inc++);
$em->persist($entity);
$em->flush();
}
return $this->redirectToRoute('poll_public');
}


This is my button from twig template



<a href="{{ path('poll_vote', {'id': poll.id}) }}">


An this is my entity



  class Poll
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="string", length=255)
*/
private $name;

/**
* @ORMColumn(type="integer", nullable=true)
*/
private $votes;

public function getId(): ?int
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getVotes(): ?int
{
return $this->votes;
}

public function setVotes(?int $votes): self
{
$this->votes = $votes;

return $this;
}
}


I have no idea about how can match my getID from my entity and match for the $id from the @Route.



Any guide or suggestion would be really appreciate.



Thanks



EDIT:



Updated with the correct function after Arne answer:



/**
* @Route("/public/{id}", name="poll_vote", methods="GET|POST")
*/
public function vote($id)
{
$entityManager = $this->getDoctrine()->getManager();
$poll = $entityManager->getRepository(Poll::class)->find($id);

if (!$poll) {
throw $this->createNotFoundException(
'No polls found for id '.$id
);
}

$poll->setVotes($poll->getVotes()+1);
$entityManager->flush();

return $this->redirectToRoute('poll_public', [
'id' => $poll->getId()
]);
}









share|improve this question















I'm kindly new on Symfony



I'm doing a vote system but i guess this should work for like,



At the moment my controller function is this, this only create a new row with 1vote, but not update any $id created before.



/**
* @Route("/public/{id}/vote", name="poll_vote", methods="GET|POST")
*/
public function vote(Request $request, Poll $poll): Response
{
$inc = 1;
$em = $this->getDoctrine()->getManager();
$entity = new Poll();
$entity->setVotes($inc++);
$em->persist($entity);
$em->flush();
}
return $this->redirectToRoute('poll_public');
}


This is my button from twig template



<a href="{{ path('poll_vote', {'id': poll.id}) }}">


An this is my entity



  class Poll
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="string", length=255)
*/
private $name;

/**
* @ORMColumn(type="integer", nullable=true)
*/
private $votes;

public function getId(): ?int
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getVotes(): ?int
{
return $this->votes;
}

public function setVotes(?int $votes): self
{
$this->votes = $votes;

return $this;
}
}


I have no idea about how can match my getID from my entity and match for the $id from the @Route.



Any guide or suggestion would be really appreciate.



Thanks



EDIT:



Updated with the correct function after Arne answer:



/**
* @Route("/public/{id}", name="poll_vote", methods="GET|POST")
*/
public function vote($id)
{
$entityManager = $this->getDoctrine()->getManager();
$poll = $entityManager->getRepository(Poll::class)->find($id);

if (!$poll) {
throw $this->createNotFoundException(
'No polls found for id '.$id
);
}

$poll->setVotes($poll->getVotes()+1);
$entityManager->flush();

return $this->redirectToRoute('poll_public', [
'id' => $poll->getId()
]);
}






php symfony symfony4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 10:00

























asked Nov 11 at 8:47









CitizenG1

306




306












  • $request->query->get('id')
    – u_mulder
    Nov 11 at 9:07


















  • $request->query->get('id')
    – u_mulder
    Nov 11 at 9:07
















$request->query->get('id')
– u_mulder
Nov 11 at 9:07




$request->query->get('id')
– u_mulder
Nov 11 at 9:07












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










basically you have to get the ID from your request, query the Entitty Repository for your Poll Entity, update the votes and persist it back to your database.





  1. Get the ID from your request



    $id = $request->query->get('id');




  2. Query the repository:



    $entityManager = $this->getDoctrine()->getManager();



    $poll= $entityManager->getRepository(Poll::class)->find($id);




  3. Update the votes:



    $poll->setVotes($poll->getVotes()+1);




  4. Persist to the DB:



    $entityManager->persist($poll);



    $entityManager->flush();




Alternatively you could also use the ParamConverter to let Symfony get the Poll object for you. More information about updating objects can be found in the Doctrine Guide.



Note that yor route will only match existing polls, since id is a required parameter in the URL. You might add another route without an ID which is being used for creating new Poll entities.






share|improve this answer





















  • Thanks Arne, that is just what i looking for, now is working as i expected. thanks!.
    – CitizenG1
    Nov 11 at 9:59











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247131%2fsymfony4-setter-where-getter-match-id-route%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










basically you have to get the ID from your request, query the Entitty Repository for your Poll Entity, update the votes and persist it back to your database.





  1. Get the ID from your request



    $id = $request->query->get('id');




  2. Query the repository:



    $entityManager = $this->getDoctrine()->getManager();



    $poll= $entityManager->getRepository(Poll::class)->find($id);




  3. Update the votes:



    $poll->setVotes($poll->getVotes()+1);




  4. Persist to the DB:



    $entityManager->persist($poll);



    $entityManager->flush();




Alternatively you could also use the ParamConverter to let Symfony get the Poll object for you. More information about updating objects can be found in the Doctrine Guide.



Note that yor route will only match existing polls, since id is a required parameter in the URL. You might add another route without an ID which is being used for creating new Poll entities.






share|improve this answer





















  • Thanks Arne, that is just what i looking for, now is working as i expected. thanks!.
    – CitizenG1
    Nov 11 at 9:59















up vote
1
down vote



accepted










basically you have to get the ID from your request, query the Entitty Repository for your Poll Entity, update the votes and persist it back to your database.





  1. Get the ID from your request



    $id = $request->query->get('id');




  2. Query the repository:



    $entityManager = $this->getDoctrine()->getManager();



    $poll= $entityManager->getRepository(Poll::class)->find($id);




  3. Update the votes:



    $poll->setVotes($poll->getVotes()+1);




  4. Persist to the DB:



    $entityManager->persist($poll);



    $entityManager->flush();




Alternatively you could also use the ParamConverter to let Symfony get the Poll object for you. More information about updating objects can be found in the Doctrine Guide.



Note that yor route will only match existing polls, since id is a required parameter in the URL. You might add another route without an ID which is being used for creating new Poll entities.






share|improve this answer





















  • Thanks Arne, that is just what i looking for, now is working as i expected. thanks!.
    – CitizenG1
    Nov 11 at 9:59













up vote
1
down vote



accepted







up vote
1
down vote



accepted






basically you have to get the ID from your request, query the Entitty Repository for your Poll Entity, update the votes and persist it back to your database.





  1. Get the ID from your request



    $id = $request->query->get('id');




  2. Query the repository:



    $entityManager = $this->getDoctrine()->getManager();



    $poll= $entityManager->getRepository(Poll::class)->find($id);




  3. Update the votes:



    $poll->setVotes($poll->getVotes()+1);




  4. Persist to the DB:



    $entityManager->persist($poll);



    $entityManager->flush();




Alternatively you could also use the ParamConverter to let Symfony get the Poll object for you. More information about updating objects can be found in the Doctrine Guide.



Note that yor route will only match existing polls, since id is a required parameter in the URL. You might add another route without an ID which is being used for creating new Poll entities.






share|improve this answer












basically you have to get the ID from your request, query the Entitty Repository for your Poll Entity, update the votes and persist it back to your database.





  1. Get the ID from your request



    $id = $request->query->get('id');




  2. Query the repository:



    $entityManager = $this->getDoctrine()->getManager();



    $poll= $entityManager->getRepository(Poll::class)->find($id);




  3. Update the votes:



    $poll->setVotes($poll->getVotes()+1);




  4. Persist to the DB:



    $entityManager->persist($poll);



    $entityManager->flush();




Alternatively you could also use the ParamConverter to let Symfony get the Poll object for you. More information about updating objects can be found in the Doctrine Guide.



Note that yor route will only match existing polls, since id is a required parameter in the URL. You might add another route without an ID which is being used for creating new Poll entities.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 9:49









Arne

684




684












  • Thanks Arne, that is just what i looking for, now is working as i expected. thanks!.
    – CitizenG1
    Nov 11 at 9:59


















  • Thanks Arne, that is just what i looking for, now is working as i expected. thanks!.
    – CitizenG1
    Nov 11 at 9:59
















Thanks Arne, that is just what i looking for, now is working as i expected. thanks!.
– CitizenG1
Nov 11 at 9:59




Thanks Arne, that is just what i looking for, now is working as i expected. thanks!.
– CitizenG1
Nov 11 at 9:59


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247131%2fsymfony4-setter-where-getter-match-id-route%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation