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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建