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
        // ...     
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 C#点击按钮的时候的循环次数就是最后一次,如何是循环第几次的值?
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写
  • ¥15 graphpad prism 三因素重复测定报错