1

I feel like this shouldn't be to hard, but try as I might, I keep getting errors.

What I'm wanting to do, is to have a single "add" function that will handle the basic functionality of adding records to any / all tables. Basically, the post data will contain the table name, as well as the fields / values to be inserted. The controller itself, confirms the user has access to do these things, and then verifies the fields are valid, before creating a new instance of the entity, that's where things go haywire:

$entityName = 'Products';
$row = new $entityName(); //doesn't work
$row new Products(); //works

I haven't found a way or any examples of creating a new entity using the Entity Manager, or else that might work, because i've created functions using EM to do queries, updates, and deletes, but I just can't get this to work with the add functions.

2
  • I am not sure if symfony2 is capable of this. You are trying to write your own code dinamically, you could try with php create a new file, write all the php class there and save it, and then execute with php app/console doctrine:schema:update... I am very doubtfull of the success though. Commented Dec 3, 2013 at 0:29
  • Drop the parens. $row = new $entityName; If that does not work then post the error message. Something else is going on. Commented Dec 3, 2013 at 14:51

2 Answers 2

2

1. Your problem is almost certainly namespacing (see below). Try this instead:

$entityName = 'My\Bundle\Entity\Products';
$row = new $entityName();

Should work. ;)

 

2. If you want to create new instances using an EntityManager, try this:

$entityName = 'My\Bundle\Entity\Products';
$row = $em->getClassMetadata($entityName)->newInstance();

...assuming $em is your EntityManager. :-)

 

3. To "prove" that your problem is namespacing, try this in a plain .php file that you run from the command line:

namespace Foo;
class Test {}

$class1 = 'Foo\Test';
$class2 = 'Test';

$x = new Test();    // Will work
$y = new $class1(); // Will work
$z = new $class2(); // Will trigger "Class 'Test' not found" fatal error
Sign up to request clarification or add additional context in comments.

1 Comment

I think the problem was a combination of my entity being a variable and having to escape my slashes. It works now like this $entity_name = 'Some\\bundle\\entity\\' . $entity; $row = new $entity_name(); Thanks for your help
0

Two things:

  • Try with "Products" instead of 'Products'
  • I suppose that your entity Products has a namespace, it is required (even if you declared an use statement). So try with "My\Bundle\Entity\Products".

2 Comments

I've tried using the namespace to no avail. Again new Products(); works, but new $entityName doesn't.
I've edited my answer, I wasn't clear, try with double quotes instead of simple quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.