Q:

Doctrine2 + Symfony2 with default Inheritance

I'm currently working on a model, which doesn't quite satisfy me yet. I got a set of Objects with single-inheritance, which reference another object:
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;     

}

The problem I'm facing at the moment, when I create an object ProductTypeOne, I actually know which category it will reference - it's always the same for this ProductType.
I could set a fix parameter, like category_id = 1 - but the architecture within Sf2 & Doctrine2 doesn't allow me to query for the Category Object from within my Entity (Document, as i'm using MongoDB).
class ProductTypeOne 
{

    private $category_id = 5;

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

Open for any input, thank's in advance!

A:

Just try this then:
class ProductTypeOne extends BaseProductType
{

    static $DEFAULT_CATEGORY = 5;

    // rest of the code

}

At insertion time [in the controller], you set the category:
$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

Another approach would be writing you custom ProductTypeOneRepository, and inject the category in its methods.

