无法在OneToOne关系中的Doctrine 2中插入值到映射列 [英] Can't insert value to mapped column in Doctrine 2 in OneToOne relation

查看:148
本文介绍了无法在OneToOne关系中的Doctrine 2中插入值到映射列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对新教徒来说是新的,在一种情况下需要帮助。我有2张简单的表专辑&类型。请检查以下结构:



相册

  id |标题| genre_id | createdon 
1 |专辑1 | 1 | 21/05/2015
2 |专辑2 | 2 | 21/05/2015

类型

  id |流派|活动
1 |流行音乐1
2 |蓝色| 1
3 |摇滚| 1
4 |国家|来自专辑表的1

genre_id 映射到 id 列的类型表。



在专辑实体中,我通过以下过程映射了这个:

  / ** 
* @ ORM\OneToOne(targetEntity =Genre,mappedBy =albums)
* @ ORM\JoinColumn(name =genre_id ,referencedColumnName =id)
** /
private $ genre;

public function getGenre(){
return $ this->类型;
}

public function setGenre($ value)
{
$ this-> genre = $ value;
return $ this;
}

我可以通过getGenre()方法,但问题是在数据库中插入值。我需要插入genre_id,这是一个整数值,但是从控制器调用 setGenre()方法后,我收到以下错误:


关联字段的类型Album \Entity\Genre的预期值
Album \Entity\Albums#$ genre,而不是string。


我还尝试通过指定另一个私有变量,如原始列名称,即genre_id,如下所示:

  / ** @ ORM\Column(type =integer)* / 
protected $ genre_id;

public function setGenre($ value)
{
$ this-> genre_id = $ value;
return $ this;
}

但是,我收到以下错误:


执行'INSERT INTO Albums(title,
createdon,genre_id)VALUES(?,?,?)'与params [nfhgfh ,
21/05/2015,null]:



SQLSTATE [23000]:完整性约束违规:1048列
'genre_id'为null


我从控制器呼叫实体,如下所示:

  $ albums = new Albums(); 
$ albums-> setTitle($ postData ['title']);
$ albums-> setGenre($ postData ['genre_id']);
$ albums-> setCreatedon(date('d / m / Y'));
$ this-> em() - > persist($ albums);

$ this-> em() - > flush();

我非常困惑于此问题,需要您的帮助。我没有任何线索,而是如何获取一个空值。请帮助。

解决方案

问题是您需要设置类型相册中成为 Album \Entity\Genre 的实例。



例如:

  $ genre = new Genre(); 
$ genre-> setId($ postData ['genre_id']);

$ albums = new Albums();
$ albums-> setTitle($ postData ['title']);
$ albums-> setGenre($ genre);
$ albums-> setCreatedon(date('d / m / Y'));

$ this-> em() - > persist($ albums);
$ this-> em() - > flush();

编辑



要修复级联错误,需要在您的映射中添加级联参数。

  / ** 
* @ ORM\OneToOne(targetEntity =Genre,mappedBy =albums,cascade = {persist})
* @ ORM\JoinColumn(name =genre_id,referencedColumnName = id)
** /
private $ genre;

您可能还需要向 $相册添加类似的东西变量在您的类型实体中,我不记得它需要的方式。


I am quite new to Doctrine and need help in one situation. I have 2 simple tables Albums & Genre. Please check the structures below:

Albums

id | title   | genre_id | createdon
1  | Album 1 | 1        | 21/05/2015
2  | Album 2 | 2        | 21/05/2015

Genre

id | genre   | active
1  | Pop     | 1
2  | Blue    | 1
3  | Rock    | 1
4  | Country | 1

genre_id from Albums table is mapped to id column of Genre table.

In Albums entity I have mapped this by the below process:

/**
 * @ORM\OneToOne(targetEntity="Genre", mappedBy="albums")
 * @ORM\JoinColumn(name="genre_id", referencedColumnName="id")
 **/
private $genre;

public function getGenre(){
    return $this->genre;    
}

public function setGenre($value)
{
    $this->genre = $value;
    return $this;
}

I can fetch genre name flawlessly by getGenre() method but the problem is while inserting value into the database. I need to insert genre_id which is an integer value but after calling setGenre() method from controller I am getting the following error:

Expected value of type "Album\Entity\Genre" for association field "Album\Entity\Albums#$genre", got "string" instead.

I have also tried by specifying another private variable like the original column name i.e genre_id like below:

/** @ORM\Column(type="integer") */
protected $genre_id;

public function setGenre($value)
{
   $this->genre_id = $value;
   return $this;
}

But then I am getting the following error:

An exception occurred while executing 'INSERT INTO Albums (title, createdon, genre_id) VALUES (?, ?, ?)' with params ["nfhgfh", "21/05/2015", null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'genre_id' cannot be null

I am calling the entity from controller like this:

$albums = new Albums();
$albums->setTitle($postData['title']);
$albums->setGenre($postData['genre_id']);
$albums->setCreatedon(date('d/m/Y'));
$this->em()->persist($albums);

$this->em()->flush();

I am badly stuck in this problem and need your help. I am not having any clue how its getting a null value instead. Please help.

解决方案

The problem is that you need to set genre in Album to be an instance of Album\Entity\Genre.

For example:

$genre = new Genre();
$genre->setId($postData['genre_id']);

$albums = new Albums();
$albums->setTitle($postData['title']);
$albums->setGenre($genre);
$albums->setCreatedon(date('d/m/Y'));

$this->em()->persist($albums);
$this->em()->flush();

EDIT

To fix cascading errors you need to add a cascade parameter to your mapping.

/**
 * @ORM\OneToOne(targetEntity="Genre", mappedBy="albums", cascade={"persist"})
 * @ORM\JoinColumn(name="genre_id", referencedColumnName="id")
 **/
private $genre;

You may also need to add a similar thing to the $album variable in your Genre entity, I can't remember exactly which way round it needs to be.

这篇关于无法在OneToOne关系中的Doctrine 2中插入值到映射列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆