Creating a new object in Symfony2 using Doctrine using a "namespace"

You know that in Symfony2 a new object can be defined as in the following example:

use Acme\StoreBundle\Entity\Product;

public function defaultController() {
    $product = new Product();
    $product->setName('Pippo');
    $product->setPrice(19.99);
    ....
    // Use Doctrine EntityManager to store the Product object
}

Suppose you know that the Product class has the following namespace: " AcmeHomeBundle: Product ". It would be nice to create a $ product object using a namespace (e.g. using EntityManager or something similar).

public function defaultController() {
    $item = createObjectFromNamespace("AcmeHomeBundle:Product");
    $item->setName('Pippo');
    $item->setPrice(19.99);
    ....
    // Use Doctrine EntityManager to store the Item object
}

Do you know if this is possible?

Suppose you have a string that provides an entity type

+3
source share
3 answers

You have to do it ...

$entityInfo = $this->em->getClassMetadata("entityNameSpace:entityName");
$entityMember = $entityInfo->newInstance();

If you want to use the setter method line by line:

$entitySetMethod = "set".\ucfirst("entityDataMemberName");
\call_user_func(array($entityMember, $entitySetMethod), $parameter);
+9
source

, :

$product = new Acme\JournalBundle\Entity\Product();
$article = new Acme\JournalBundle\Entity\Article();

, . :

use Acme\JournalBundle\Entity\Product,
    Acme\JournalBundle\Entity\Article;

:

$product = new Product();
$article = new Article();

.

+2

Acme\StoreBundle\Entity\Product IS . AcmeStoreBundle: , DQL .

? , - factory , , . ?

: http://www.doctrine-project.org/api/orm/2.2/source-class-Doctrine.ORM.Configuration.html#153

, , , , Configuration.

- Doctrine. Symfony docs :

alias - Doctrine , , DQL- . .

0

All Articles