dpa55065 2015-09-06 16:04
浏览 64
已采纳

如何在symfony中插入带有ManyToOne关系的user_id?

Really new with symfony, coming from yii.

I can't insert user_id into another table with a ManyToOne relationship. When I get entity of the table that I want to insert of the id, I used: $en->setUser($user_id);

The outcome is: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null

but when I do var_dump($user_id); there is an id. So how can I insert user_id with a many to one relationship?

[update] TableUser Entity

<?php
namespace User\Bundle\RegisterBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
 * User
 *
 * @ORM\Table(name="user", indexes={@ORM\Index(name="user_id", columns={"user_id"})})
 * @ORM\Entity
 */

class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="user_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $userId;

    public function getUserId()
    {
        return $this->userId;
    }
}
?>

TableSomeModel Entity that has ManytoOne relationship with user_id

<?php
namespace \Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * SomeModel
 *
 * @ORM\Table(name="SomeModel", indexes={@ORM\Index(name="user_id", columns={"user_id"})})
 * @ORM\Entity
 */
class SomeModel
{
    /**
     * @var \User
     *
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     * })
     */
    private $user;

    /**
     * Set user
     *
     * @param \Entity\User $user
     * @return App
     */
    public function setUser(\Entity\User $user)
    {
        return $this->user;
    }

    /**
     * Get user
     *
     * @return \Entity\User 
     */
    public function getUser()
    {
        return $this->user;
    }
}

In the controller

$en = new SomeModel();
var_dump($user_id); //not null
$en->setUser($user_id); //always null;
  • 写回答

2条回答 默认 最新

  • duanbi2003 2015-09-06 16:18
    关注

    Make sure that you have defined the relation properly. If you are using yml configuration files for defining entities, many to one relations are configured as given below.

    manyToOne:
    user:
      targetEntity: Your\NameSpace\Entity\User
      joinColumn:
        name: user_id
        referencedColumnName: id
    

    If you are using annotation to define the relation,

    /**
     * @ManyToOne(targetEntity="Your\NameSpace\Entity\User")
     * @JoinColumn(name="user_id", referencedColumnName="id")
     **/
    private $user;
    

    And the setUser method in your entity

     /**
         * Set user
         *
         * @param Blsk\CoreBundle\Entity\User $user
         */
        public function setUser(Your\NameSpace\Entity\User $user = null)
        {
            $this->user = $user;
        }
    

    EDIT In your code, setUser method is not setting the user but returning the user.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器