2012-03-22 1 views
6

コントローラメソッドからモデルを移動する必要があったため、サービスに変更するのに手伝ってもらいました。サービス自体は機能しますが、私はこのサービスの内部から教義とカーネルに接続できる必要があります。最初は教義を有効にしようとしましたが、それは問題を作り出しました。どうすればこの作品を作れますか?私はドキュメントに従って、このコードを手に入れました。私はなぜ以下のエラーが出るのか分かりません。事前にあなたの助けをありがとう。symfony2のService DependencyInjection

私の設定です:コントローラ

CSVImport.php

namespace Tools\TFIBundle\Model; 

use Doctrine\ORM\EntityManager; 

class CSVImport { 
    protected $em; 

    public function __construct(EntityManager $em) { 
     $this->em = $em; 
    } 

アプリ/設定/ config.yml

services: 
    csvimport: 
     class: Tools\TFIBundle\Model\CSVImport 
     arguments: [ @doctrine.orm.entity_manager ] 

アクション

$cvsimport = $this->get('csvimport'); 

MY ERROR

Catchable Fatal Error: Argument 1 passed to 
Tools\TFIBundle\Model\CSVImport::__construct() must be an instance of 
Doctrine\ORM\EntityManager, none given, called in 
.../Tools/TFIBundle/Controller/DefaultController.php on line 58 and defined in 
.../Tools/TFIBundle/Model/CSVImport.php line 12 

EDIT、私の作業コード:それに接続されているカーネルと

サービスクラスコード

namespace Tools\TFIBundle\Model; 

use Doctrine\ORM\EntityManager, 
    AppKernel; 

class CSVImport { 
    protected $em; 
    protected $kernel; 
    protected $cacheDir; 

    public function __construct(EntityManager $em, AppKernel $k) { 
     $this->em = $em; 
     $this->kernel = $k; 
} 

答えて

1

@doctrine.orm.default_entity_managerを注入してください。

+0

このヒントは実際の問題を見つけるのを助けましたが、設定は問題ありませんでしたが、私はそのエラーを引き起こしたそのサービスへのコントローラコードの誤った呼び出しを受けました。 – nysander

+0

同じことがDBAL接続を取得するために機能しました。誰かがそれの背後にある論理を説明することができますか? – Robert

1

私はDoctrine DBALに接続して自分でクエリを作成する方法を見つけました。しかし、私はこの1つに私の設定を変更する場合:

アプリ/ config.yml

services: 
    csvimport: 
     class: Tools\TFIBundle\Model\CSVImport 
     arguments: [ @doctrine.dbal.connection, @doctrine.orm.entity_manager, @kernel ] 

クラス定義

namespace Tools\TFIBundle\Model; 

use Doctrine\ORM\EntityManager, 
    Doctrine\DBAL\Connection, 
    AppKernel; 

class CSVImport { 
    protected $c; 
    protected $em; 
    protected $kernel; 

    public function __construct(Connection $c, EntityManager $em, AppKernel $k) { 
     $this->c = $c; 
     $this->em = $em; 
     $this->kernel = $k; 
    } 

Iましたエラー:

RuntimeException: The definition "csvimport" has a reference to an abstract definition "doctrine.dbal.connection". Abstract definitions cannot be the target of references. 

何か案は?

関連する問題