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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动