简体   繁体   中英

symfony2 edit form & doctrine issue

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...)?

  1. It's bad practice (arguably) to use the formbuilder each time instead of creating a form class.
  2. Try running php app/console generate:doctrine:form MyApp:Myentity and using the form class (after setting the data_class option -- see the manual under "Setting the data_class"
  3. var_dump each object (the debug link and the link pulled from the database) and post them, if you can.

OK,

so after some more trial-and-error I found that my find-method was causing the problem. Apparently, the findBy(array) also returns an array, in order to get that single object for editing I have to do find($id), by which I loose the functionality of SELECTing only if the user is trying to edit his "own" link...

I anyway found out that the way I was doing it (by comparing on the user name instead of ID) might not be the best way...

If anyone has comments on that, feel free to point me in the right direction, otherwise I will do some trial-and-error and post a new thread if I can't work it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM