I am a Symfony2 newbie running into problems with edit-forms and doctrine.
I seem to be getting a corrupt object from my database somehow.
This code works fine...
$FormDebug = new Link();
$FormDebug->setUrl('http://www.mysite.com');
$editForm = $this->createFormBuilder($FormDebug)
->add('url','url')
->add('description','text')
->getForm();
While this doesn't...
$repository = $this->getDoctrine()->getRepository('HemekonomiLinksBundle:Link');
$user = $this->container->get('security.context')->getToken()->getUser();
$userLink = $repository->findBy(
array('id' => $id, 'user' => $user->getId())
);
$editForm = $this->createFormBuilder($userLink)
->add('url','url')
->add('description','text')
->getForm();
So I guess I am getting an object that the formbuilder doesn't agree with..? No error message, just that I don't get a form filled out with the actual values of the fetched object, but rather an empty form.
When I var_dump() the object returned from the DB I can see that the correct values are there (along with all the properties of the user object - is this what's causing my problem? The object containing more variables than the form?). The reason that the user is in there is of course to sort out only those link rows from the DB belonging to this particular user.
UPDATE: I added two var_dump()s here, first the kind of object I expected to retrieve from the DB (just an example to describe the object) second what is actually being retrieved, I can see that repository-find action returns an array instead of an object, but my object seems to be included... why I do not know...
What I expected
object(Company\LinksBundle\Entity\Link)#556 (4) { ["id":protected]=> NULL ["user":protected]=> NULL ["description":protected]=> NULL ["url":protected]=> string(25) "http://www.mysite.com" }
What DB returns
array(1) { [0]=> object(Company\LinksBundle\Entity\Link)#553 (4) { ["id":protected]=> int(9) ["user":protected]=> object(Company\UserBundle\Entity\User)#145 (19) { ["id":protected]=> int(3) ["username":protected]=> string(5) "user1" ["usernameCanonical":protected]=> string(5) "user1" ["email":protected]=> string(6) "1@1.se" ["emailCanonical":protected]=> string(6) "1@1.se" ["enabled":protected]=> bool(true) ["salt":protected]=> string(31) "oltkauxmgw000w8wgw84ckggg8sw880" ["password":protected]=> string(88) "AFKlCO774d/4D8DHD3P/sXYYApS32jzdLm5GlZEICnOq8xyKT/xVjbnAziMUadecN0yBlxiH5QZK09s5KJxbsA==" ["plainPassword":protected]=> NULL ["lastLogin":protected]=> object(DateTime)#149 (3) { ["date"]=> string(19) "2012-06-27 07:04:24" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Stockholm" } ["confirmationToken":protected]=> NULL ["passwordRequestedAt":protected]=> NULL ["groups":protected]=> NULL ["locked":protected]=> bool(false) ["expired":protected]=> bool(false) ["expiresAt":protected]=> NULL ["roles":protected]=> array(0) { } ["credentialsExpired":protected]=> bool(false) ["credentialsExpireAt":protected]=> NULL } ["description":protected]=> string(22) "Beskrivning av länken" ["url":protected]=> string(16) "http://testlink.se" } }
Is what happens here that the DB returns the actual link object, and the user object connected to it, and it will be my job to weed out the one of the two that I want?
If so, is there a way to, already in the repository->find to specify that although I WHERE on the user field in my find statement, I am not interested in SELECTing the user object, I only want what's in the Links TABLE? (Link object in symfony2 terms, I guess...)?