2012-08-24 3 views
0

현재 모델을 작성 중입니다. 아직 만족스럽지 않습니다. 그건 -Doctrine2 + Symfony2 (기본 상속 포함)

class Category 
{ 
    /** @MongoDB\Id(strategy="auto") */ 
    protected $id; 

    /** @MongoDB\Int */ 
    protected $categoryId; 

    /** @MongoDB\String */ 
    protected $title; 
} 

class ProductTypeOne extends BaseProductType 
{ 
    /** @MongoDB\Id(strategy="auto") */ 
    protected $id; 

    /** @MongoDb\ReferenceOne(targetDocument="Category") */ 
    private $category;  

} 
내가 객체 ProductTypeOne를 만들 때 나는 순간에 직면하고있어 문제는, 내가 실제로 참조되는 카테고리 알고

이 : 나는 다른 객체를 참조하는 단일 상속과 객체의 집합을 가지고 이 ProductType에 대해 항상 동일합니다.

category_id = 1과 같은 수정 매개 변수를 설정할 수 있지만 Sf2 내의 아키텍처 & Doctrine2는 MongoDB를 사용함에 따라 내 개체 (문서)에서 범주 개체를 쿼리 할 수 ​​없습니다.

class ProductTypeOne 
{ 

    private $category_id = 5; 

    public method getCategory() 
    { 
     /** how to query the CategoryObject with ID=5? */ 
    } 
} 

미리 입력 해 주시기 바랍니다.

[컨트롤러에서] 삽입시
class ProductTypeOne extends BaseProductType 
{ 

    static $DEFAULT_CATEGORY = 5; 

    // rest of the code 

} 

, 당신은 카테고리 설정 :

+1

알려진 클래스를 하위 클래스의 생성자에 주입합니다. – moonwave99

+0

생성자도 시도했지만 문제가 완전히 해결되지 않았습니다. 이렇게하면 생성자에 전달해야하지만, 컨트롤러에서 Subclass를 사용할 때마다 관련 항목을 가져오고 싶지는 않습니다. Symfony의 DI/Services 아키텍처에서 읽었습니다. 나중에 쐈어. – weyandch

답변

0

그냥 다음이 시도하는 또 다른 방법은 사용자 정의 ProductTypeOneRepository 당신을 쓰는 것

$product = new ProductTypeOne; 

// do whatever you need to fill the instance 

$product -> setCategory(
    $this -> getDoctrine() -> getRepository('Category') -> findOneById(ProductTypeOne::$DEFAULT_CATEGORY) 
); 

// persist $product, beer time 

을하고 카테고리를 주입 그것의 방법에서.