2012-03-22 32 views
6

Tôi cần di chuyển mô hình của mình từ phương thức điều khiển, vì vậy tôi được trợ giúp thay đổi mô hình đó thành dịch vụ. Bản thân dịch vụ này hoạt động, nhưng tôi cần có khả năng kết nối với giáo lý và hạt nhân từ bên trong dịch vụ này. Lúc đầu, tôi đã cố gắng để kích hoạt giáo lý, nhưng điều đó tạo ra vấn đề. Làm thế nào tôi có thể thực hiện công việc này? Tôi đã theo dõi các tài liệu và nhận được mã này. Tôi không biết tại sao tôi lại gặp lỗi dưới đây. Trước tiên xin cảm ơn sự giúp đỡ của bạn.Dịch vụ DependencyInjection trong Symfony2

cấu hình của tôi là:

CSVImport.php

namespace Tools\TFIBundle\Model; 

use Doctrine\ORM\EntityManager; 

class CSVImport { 
    protected $em; 

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

app/config/config.yml

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

hành động trong bộ điều khiển

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

LỖI CỦA TÔI

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, mã làm việc của tôi:

dịch vụ mã lớp với Kernel gắn liền với nó

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; 
} 

Trả lời

1

Hãy thử tiêm @doctrine.orm.default_entity_manager.

+0

gợi ý này đã giúp tôi tìm thấy vấn đề thực sự, cấu hình là ok, nhưng tôi đã nhận được mã điều khiển cuộc gọi sai đến dịch vụ đó đã thực hiện những lỗi đó. – nysander

+0

Điều tương tự cũng làm việc để có được kết nối DBAL. Ai đó có thể giải thích logic đằng sau nó là gì? – Robert

1

Trên web tôi đã tìm thấy cách kết nối với Doctrine DBAL để có thể tự tạo truy vấn. Nhưng khi tôi thay đổi cấu hình của tôi để này một:

app/config.yml

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

định nghĩa lớp

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; 
    } 

tôi đã nhận lỗi:

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

Bất kỳ ý tưởng?

Các vấn đề liên quan