2015-04-23 66 views
2

我試圖創建一個服務堅持/從JSON供稿學說Symfony2的服務不註冊的依賴

刷新數據服務依賴關係:

  • 主義的EntityManager
  • 的API,它拉數據

我的服務配置:

services: 
    fantasyapi: 
     class:  FantasyDataAPI\Client 
     arguments: ["%fantasyapi.key%"] 
    stadium_parser: 
     class:  FantasyPro\DataBundle\DataManager\StadiumParser 
     arguments: ["@doctrine.orm.entity_manager", "@fantasyapi"] 

我的服務:

<?php 

namespace FantasyPro\DataBundle\DataManager; 

use Doctrine\ORM\EntityManager; 
use FantasyDataAPI\Client; 
use FantasyPro\DataBundle\Entity\Stadium; 

class StadiumParser { 
    /** 
    * @var EntityManager $em 
    */ 
    private $em; 
    /** 
    * @var Client $client 
    */ 
    private $client; 

    public function __constuct(EntityManager $em, Client $client) { 
     $this->em = $em; 
     $this->client = $client; 
    } 

    /** 
    * @return array 
    */ 
    public Function parseData(){ 
     $stadiumData = $this->client->Stadiums(); 
     //var_dump($stadiumData); 
     //get the Repo 
     $repo = $this->em->getRepository('DataBundle:Stadium'); 

     $log = array(); 

     foreach ($stadiumData as $stadium) { 
      // Get the current stadium in the list 
      $criteria = array('stadiumID' => $stadium['StadiumID']); 
      $currentStadium = $repo->FindOneBy($criteria); 

      if (! $currentStadium) { 
       $currentStadium = new Stadium(); //no stadium with the StadiumID exists so create a new stadium 

       $logData = [ 
        'action' => 'Added Stadium', 
        'itemID' => $stadium['StadiumID'], 
        'itemName' => $stadium['Name'] 
       ]; 
       $log[] = $logData; 
      } else { 
       $logData = [ 
        'action' => 'Updated '.$logTitle, 
        'itemID' => $stadium['PlayerID'], 
        'itemName' => $stadium['Name'] 
       ]; 
       $log[] = $logData; 
      } 
      $currentStadium->setStadiumID($stadium['StadiumID']); 
      $currentStadium->setName($stadium['Name']); 
      $currentStadium->setCity($stadium['City']); 
      $currentStadium->setState($stadium['State']); 
      $currentStadium->setCountry($stadium['Country']); 
      $currentStadium->setCapacity($stadium['Capacity']); 
      $currentStadium->setPlayingSurface($stadium['PlayingSurface']); 
      $this->em->persist($currentStadium); 
     } 
     $this->em->flush(); 
     return $log; 
    } 

我把這種服務從我的控制器:

public function StadiumServiceTestAction(){ 
     $parser = $this->container->get('stadium_parser'); 
     $log = $parser->parseData(); 
     return $this->render('DataBundle:Default:index.html.twig', array('log' => $log)); 

    } 

當我打電話 $stadiumData = $this->client->Stadiums();

我得到以下錯誤:Call to a member function Stadiums() on a non-object

如果我在開始時做了var_dump($this);我得到的方法:

object(FantasyPro\DataBundle\DataManager\StadiumParser)[5133] 
private 'em' => null 
private 'client' => null 

爲什麼不注入依賴項? 如果我做我的app/console container:debug服務被列爲

[container] Information for service stadium_parser 
Service Id  stadium_parser 
Class   FantasyPro\DataBundle\DataManager\StadiumParser 
Tags    - 
Scope   container 
Public   yes 
Synthetic  no 
Required File - 

我執行PHP app/console cache:clear --env=dev也不過PHP無濟於事

+0

你是否真的在'StadiumParser'類的構造函數中使用了'Stadium $ stadium'?你在服務定義中傳遞一個字符串 - 「:: DataBundle.Entity.Stadium」 - 但你期望得到一個'Stadium'實體(這是一個錯誤的方法)。 –

+0

它檢查數據庫中是否存在條目,如果不存在,它創建一個新的Stadium Entity,添加數據並將新實體pershing /刷新到數據庫。 什麼是通過球場實體的正確方法? –

+0

如果我完全從服務和構造函數中刪除體育場實體,我仍然獲得$ em和$ client爲空 –

回答

2

由於所有PARAMS都假設在costructor被注入你可以考慮重命名方法__constuct__construct

+0

賓果,不敢相信我錯過了那個錯字,盯着它太久了! –

+0

是的,下一步,現在在PHPUnit上進行搜索和閱讀,以及如何在控制器之外測試並嘲笑我的數據,這麼多要學習! –