2013-04-05 68 views
0

我的控制器的PurchaseOrder控制器存取方法通過服務--- Symfony2的

<?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->getDoctrine()->getManager(); 

    $entities = $em->getRepository('CJBusinessBundle:PurchaseOrder')->findAll(); 

    return array(
     'entities' => $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->createForm(new PurchaseOrderType(), $entity); 
    $form->bind($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($entity); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('purchaseorder_show', array('id' => $entity->getId()))); 
    } 

    return array(
     'entity' => $entity, 
     'form' => $form->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->createForm(new PurchaseOrderType(), $entity); 

    $purchase = $this->get('cj.businessbundle.purchase'); 
    $purchase->newAction(); 

    return array(
     'entity' => $entity, 
     'form' => $form->createView(), 
    ); 
} 

/** 
* Finds and displays a PurchaseOrder entity. 
* 
* @Route("/{id}", name="purchaseorder_show") 
* @Method("GET") 
* @Template() 
*/ 
public function showAction($id) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $entity = $em->getRepository('CJBusinessBundle:PurchaseOrder')->find($id); 

    if (!$entity) { 
     throw $this->createNotFoundException('Unable to find PurchaseOrder entity.'); 
    } 

    $deleteForm = $this->createDeleteForm($id); 

    return array(
     'entity'  => $entity, 
     'delete_form' => $deleteForm->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->getDoctrine()->getManager(); 

    $entity = $em->getRepository('CJBusinessBundle:PurchaseOrder')->find($id); 

    if (!$entity) { 
     throw $this->createNotFoundException('Unable to find PurchaseOrder entity.'); 
    } 

    $editForm = $this->createForm(new PurchaseOrderType(), $entity); 
    $deleteForm = $this->createDeleteForm($id); 

    return array(
     'entity'  => $entity, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->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->getDoctrine()->getManager(); 

    $entity = $em->getRepository('CJBusinessBundle:PurchaseOrder')->find($id); 

    if (!$entity) { 
     throw $this->createNotFoundException('Unable to find PurchaseOrder entity.'); 
    } 

    $deleteForm = $this->createDeleteForm($id); 
    $editForm = $this->createForm(new PurchaseOrderType(), $entity); 
    $editForm->bind($request); 

    if ($editForm->isValid()) { 
     $em->persist($entity); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('purchaseorder_edit', array('id' => $id))); 
    } 

    return array(
     'entity'  => $entity, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    ); 
} 

/** 
* Deletes a PurchaseOrder entity. 
* 
* @Route("/{id}", name="purchaseorder_delete") 
* @Method("DELETE") 
*/ 
public function deleteAction(Request $request, $id) 
{ 
    $form = $this->createDeleteForm($id); 
    $form->bind($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $entity = $em->getRepository('CJBusinessBundle:PurchaseOrder')->find($id); 

     if (!$entity) { 
      throw $this->createNotFoundException('Unable to find PurchaseOrder entity.'); 
     } 

     $em->remove($entity); 
     $em->flush(); 
    } 

    return $this->redirect($this->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->createFormBuilder(array('id' => $id)) 
     ->add('id', 'hidden') 
     ->getForm() 
    ; 
} 
} 

Serivces.yml

services: 
    cj.businessbundle.purchase: 
     class: CJ\BusinessBundle\Controller\PurchaseController 

訪問內部控制器的方法[未購買控制器]

$purchase = $this->get('cj.businessbundle.purchase'); 
$purchase->newAction(); 

獲得誤差:

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

新的行動確實存在

我認爲在定義服務

+0

可以你後類的地方,你可以調用$ purchase = $ this-> get('cj.businessbundle.purchase'); – tuffkid 2013-04-05 09:28:53

+0

這個控制器是我調用它的類。它的公共職能newAction – chirag7jain 2013-04-05 09:35:40

+0

我沒有注意到:)對不起 – tuffkid 2013-04-05 09:36:37

回答

6

當你調用$this->get("your.service")你問的依賴注入容器來加載服務,我做錯了什麼。您正在請求它加載一個擴展了Symfony控制器類的控制器,該控制器類擴展了ContainerAware。您收到的錯誤是因爲加載的控制器可能嘗試訪問使用$this->container->get("request")的「請求」服務,但未設置$this->container

ContainerAware有一個方法setContainer,當你的服務是建立在services.yml使用calls:參數,你可以實際運行:

services: 
    cj.businessbundle.purchase: 
     class: CJ\BusinessBundle\Controller\PurchaseController 
     calls: 
     - [setContainer, [@service_container]] 

的Symfony2做這行的負載控制器(PurchaseOrderController),但如果你只需自己加載一個控制器類。

從PurchaseController中提取newAction(或所需的邏輯)作爲服務本身,而不是將整個控制器設置爲服務將是更好的做法。通過這種方式,Purchase和PurchaseOrder控制器可以調用服務來創建一個新購買(或者newAction),並且每次都沒有加載整個控制器。退房FOS user bundle user manager作爲一個很好的服務例子或http://symfony.com/doc/2.1/book/service_container.html#what-is-a-service

+0

+1不錯的@Luke – Mick 2013-04-05 10:43:17

+0

@Luke謝謝一個男人。嘿,你能指出一些能讓我建立一個Ajax形式的東西。與購買訂單表單一樣,我希望添加選項以添加新項目。像添加新項按鈕將通過ajax添加字段形成 – chirag7jain 2013-04-05 10:44:23

+0

symfony2文檔有一個關於集合和使用jQuery(在他們的例子中)添加新標籤到任務的部分http://symfony.com/doc/2.1/cookbook/形式/ form_collections.html。不知道這是否意味着什麼 – Luke 2013-04-05 10:46:16