Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用条令和符号插入数据库-外键不';t插入_Php_Mysql_Sql_Symfony_Doctrine Orm - Fatal编程技术网

Php 使用条令和符号插入数据库-外键不';t插入

Php 使用条令和符号插入数据库-外键不';t插入,php,mysql,sql,symfony,doctrine-orm,Php,Mysql,Sql,Symfony,Doctrine Orm,我将Symfony与条令一起使用,并尝试将数据插入数据库。在我的表messages中有一列user\u id,它被设置为users.id的外键。 在我的消息实体中,有一个针对用户id的设置器 /** * @ORM\Entity * @ORM\Table(name="Messages") */ class Message { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValu

我将Symfony与条令一起使用,并尝试将数据插入数据库。在我的表
messages
中有一列
user\u id
,它被设置为
users.id
的外键。 在我的
消息
实体中,有一个针对
用户id的设置器

/**
 * @ORM\Entity
 * @ORM\Table(name="Messages")
 */
class Message {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="user_id", type="integer")
     */
    protected $user_id;

    /**
     * @ORM\Column(name="text", type="text")
     */
    protected $text;

    /**
     * @ORM\ManyToOne(targetEntity="User")
     * */
    protected $user;

    public function setText($text) {
        $this->text = $text;
    }

    public function getText() {
        return $this->text;
    }

    public function setUserId($user_id) {
        $this->user_id = $user_id;
    }

    public function getUser_id() {
        return $this->user_id;
    }

    public function getUser() {
        return $this->user;
    }
}
但是,它不会插入传递给setter的变量,而是
NULL
。每隔一列都填充了正确的数据,而仅插入这一列是不正确的。我想它与外键有关(表
users
中有适当的数据)

控制器代码的一部分:

if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {
                if ($isLogged) {
                    $user = $this->get('security.context')->getToken()->getUser();
                    $message->setUserId($user->getId());
                }
                $em->persist($message);
                $em->flush();
当我将setter更改为此时,它将正确插入:

    public function setUserId($user_id) {
         $this->random_column_which_is_not_foreignkey = $user_id;
    }
怎么了?谢谢

您需要使用

试试这个:

/**
 * @ORM\Entity
 * @ORM\Table(name="Messages")
 */
class Message {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="text", type="text")
     */
    protected $text;

    /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id")
     */
    protected $user;

    public function setText($text) {
        $this->text = $text;
    }

    public function getText() {
        return $this->text;
    }

    public function setUser($user) {
        $this->user = $user;
    }

    public function getUser() {
        return $this->user;
    }
}
在控制器中:

if ($request->getMethod() == 'POST') {
    $form->bind($request);
    if ($form->isValid()) {
        if ($isLogged) {
            $user = $this->get('security.context')->getToken()->getUser();
            $message->setUser($user);
        }
        $em->persist($message);
        $em->flush();
        // ...

请使用保留实体的部分代码进行更新我添加了一些其他代码