Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing Method Through Service --- Symfony2
    text
    copied!<p>My Controller PurchaseOrder Controller</p> <pre><code>&lt;?php namespace CJ\BusinessBundle\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use CJ\BusinessBundle\Entity\PurchaseOrder; use CJ\BusinessBundle\Form\PurchaseOrderType; /** * PurchaseOrder controller. * * @Route("/purchaseorder") */ class PurchaseOrderController extends Controller { /** * Lists all PurchaseOrder entities. * * @Route("/", name="purchaseorder") * @Method("GET") * @Template() */ public function indexAction() { $em = $this-&gt;getDoctrine()-&gt;getManager(); $entities = $em-&gt;getRepository('CJBusinessBundle:PurchaseOrder')-&gt;findAll(); return array( 'entities' =&gt; $entities, ); } /** * Creates a new PurchaseOrder entity. * * @Route("/", name="purchaseorder_create") * @Method("POST") * @Template("CJBusinessBundle:PurchaseOrder:new.html.twig") */ public function createAction(Request $request) { $entity = new PurchaseOrder(); $form = $this-&gt;createForm(new PurchaseOrderType(), $entity); $form-&gt;bind($request); if ($form-&gt;isValid()) { $em = $this-&gt;getDoctrine()-&gt;getManager(); $em-&gt;persist($entity); $em-&gt;flush(); return $this-&gt;redirect($this-&gt;generateUrl('purchaseorder_show', array('id' =&gt; $entity-&gt;getId()))); } return array( 'entity' =&gt; $entity, 'form' =&gt; $form-&gt;createView(), ); } /** * Displays a form to create a new PurchaseOrder entity. * * @Route("/new", name="purchaseorder_new") * @Method("GET") * @Template() */ public function newAction() { $entity = new PurchaseOrder(); $form = $this-&gt;createForm(new PurchaseOrderType(), $entity); $purchase = $this-&gt;get('cj.businessbundle.purchase'); $purchase-&gt;newAction(); return array( 'entity' =&gt; $entity, 'form' =&gt; $form-&gt;createView(), ); } /** * Finds and displays a PurchaseOrder entity. * * @Route("/{id}", name="purchaseorder_show") * @Method("GET") * @Template() */ public function showAction($id) { $em = $this-&gt;getDoctrine()-&gt;getManager(); $entity = $em-&gt;getRepository('CJBusinessBundle:PurchaseOrder')-&gt;find($id); if (!$entity) { throw $this-&gt;createNotFoundException('Unable to find PurchaseOrder entity.'); } $deleteForm = $this-&gt;createDeleteForm($id); return array( 'entity' =&gt; $entity, 'delete_form' =&gt; $deleteForm-&gt;createView(), ); } /** * Displays a form to edit an existing PurchaseOrder entity. * * @Route("/{id}/edit", name="purchaseorder_edit") * @Method("GET") * @Template() */ public function editAction($id) { $em = $this-&gt;getDoctrine()-&gt;getManager(); $entity = $em-&gt;getRepository('CJBusinessBundle:PurchaseOrder')-&gt;find($id); if (!$entity) { throw $this-&gt;createNotFoundException('Unable to find PurchaseOrder entity.'); } $editForm = $this-&gt;createForm(new PurchaseOrderType(), $entity); $deleteForm = $this-&gt;createDeleteForm($id); return array( 'entity' =&gt; $entity, 'edit_form' =&gt; $editForm-&gt;createView(), 'delete_form' =&gt; $deleteForm-&gt;createView(), ); } /** * Edits an existing PurchaseOrder entity. * * @Route("/{id}", name="purchaseorder_update") * @Method("PUT") * @Template("CJBusinessBundle:PurchaseOrder:edit.html.twig") */ public function updateAction(Request $request, $id) { $em = $this-&gt;getDoctrine()-&gt;getManager(); $entity = $em-&gt;getRepository('CJBusinessBundle:PurchaseOrder')-&gt;find($id); if (!$entity) { throw $this-&gt;createNotFoundException('Unable to find PurchaseOrder entity.'); } $deleteForm = $this-&gt;createDeleteForm($id); $editForm = $this-&gt;createForm(new PurchaseOrderType(), $entity); $editForm-&gt;bind($request); if ($editForm-&gt;isValid()) { $em-&gt;persist($entity); $em-&gt;flush(); return $this-&gt;redirect($this-&gt;generateUrl('purchaseorder_edit', array('id' =&gt; $id))); } return array( 'entity' =&gt; $entity, 'edit_form' =&gt; $editForm-&gt;createView(), 'delete_form' =&gt; $deleteForm-&gt;createView(), ); } /** * Deletes a PurchaseOrder entity. * * @Route("/{id}", name="purchaseorder_delete") * @Method("DELETE") */ public function deleteAction(Request $request, $id) { $form = $this-&gt;createDeleteForm($id); $form-&gt;bind($request); if ($form-&gt;isValid()) { $em = $this-&gt;getDoctrine()-&gt;getManager(); $entity = $em-&gt;getRepository('CJBusinessBundle:PurchaseOrder')-&gt;find($id); if (!$entity) { throw $this-&gt;createNotFoundException('Unable to find PurchaseOrder entity.'); } $em-&gt;remove($entity); $em-&gt;flush(); } return $this-&gt;redirect($this-&gt;generateUrl('purchaseorder')); } /** * Creates a form to delete a PurchaseOrder entity by id. * * @param mixed $id The entity id * * @return Symfony\Component\Form\Form The form */ private function createDeleteForm($id) { return $this-&gt;createFormBuilder(array('id' =&gt; $id)) -&gt;add('id', 'hidden') -&gt;getForm() ; } } </code></pre> <p>Serivces.yml</p> <pre><code>services: cj.businessbundle.purchase: class: CJ\BusinessBundle\Controller\PurchaseController </code></pre> <p>Accessing method inside controller [Not Purchase Controller]</p> <pre><code>$purchase = $this-&gt;get('cj.businessbundle.purchase'); $purchase-&gt;newAction(); </code></pre> <p>Getting error:</p> <blockquote> <p>FatalErrorException: Error: Call to a member function get() on a non-object in /home/cj/public_html/Symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 163</p> </blockquote> <p>The new Action does exist</p> <p>I think I am doing something wrong while defining the service</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload