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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀