dpoh61610 2012-06-26 17:26 采纳率: 100%
浏览 33
已采纳

symfony2编辑表格和原则问题

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

  • 写回答

2条回答 默认 最新

  • dsgft1486 2012-06-27 05:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助