doujiayuan8415 2015-06-25 22:09
浏览 58
已采纳

Symfony 2:无法将User-Entity作为其所有者添加到Item-Entity

I'm trying to add a User to the Item as it's owner

  • manually in the Controller by $item->setOwner($this->getUser());
  • in a ManyToOne-relation.

Everything defined in the FormType gets saved as it should but the owner_id is NULL. I don't get any error-message.

Any ideas? Code below.

Thanks in advance!

# src/myBundle/Controller/myController.php
class myController extends Controller {
    public function myAction(Request $request) {
        $item = new Item();
        $item->setOwner($this->getUser());

        // print_r($this->getUser());  <- prints a valid user

        $form = $this->createForm(new ItemFormType(), $item);
        if ($request->isMethod('POST')) {
            $form->handleRequest($request);

            if ($form->isValid()) {
                $em = $this->get('doctrine.orm.default_entity_manager');
                $em->persist($item); // saves everything but the owner
                $em->flush();
            }
        }

        return $this->render('myBundle:path/to:template.html.twig', array(
            'form' => $form
        )
    }
}

-

# src/myBundle/Form/ItemFormType.php
class ItemFormType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {            
        $builder->add(...);
        // ...
    }
}

public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'myBundle\Entity\Item'
    ));
}

public function getName() {
    return 'myItemForm';
}

-

# src/myBundle/Entity/Item.php
class Item {
     // ...
     /**
      * @ORM\ManyToOne(targetEntity="myBundle\Entity\User")
      * @ORM\JoinColumn(name="owner_id", referencedColumnName="id")
      */
    protected $owner;

     // ...
     // Getter & Setter
     // ...     
}

-

# src/myBundle/Entity/User.php
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }
}
  • 写回答

1条回答 默认 最新

  • dougaicha5258 2015-06-25 23:49
    关注

    Problem solved. Unfortunately I didn't change the column-name of $deletedBy after copy & pasting $owner. Result: $deletedBy overwrote $owner with an empty value.

    # src/myBundle/Entity/Item.php
    class Item {
         // ...
         /**
          * @ORM\ManyToOne(targetEntity="myBundle\Entity\User")
          * @ORM\JoinColumn(name="owner_id", referencedColumnName="id")
          */
        protected $owner;
    
        // many more
    
        /**
          * @ORM\ManyToOne(targetEntity="myBundle\Entity\User")
          * @ORM\JoinColumn(name="owner_id", referencedColumnName="id") <- "owner_id"
          */
        protected $deletedBy;
    
        // ...
        // Getter & Setter
        // ...     
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名