当我尝试执行下面的操作时,我会收到以下错误:
SQLSTATE[23000]:完整性约束冲突:1048列"category_id"不能为null(500内部服务器错误)
正如您在操作中看到的,我正在设置字段category_id(setCategoryId()),那么错误的原因是什么?
注意:正如你所看到的,我正试图在数据库中保存一个提案(propuesta)和与之相关的标签
public function savePropuestaAction() {
$em = $this->get('doctrine.orm.entity_manager');
$propuesta = new Propuestas();
//retrieve the values inserted in the form.
$form = $this->getRequest()->request->get('form');
$propuestaContent = $form['propuesta'];
$propuestaCategory = $form['Categoría'][0];
$propuesta->setTitulo('$propuestaCategory');
$propuesta->setContenido($propuestaContent);
$propuesta->setCategoryId(1);
//retrieve the tags inserted in the form...
$tags = array();
foreach($form as $key => $field)
{
if(substr($key, 0, 3) == 'tag')
{
$tagsName[] = $field;
}
}
// ...and then store them and associate them to the proposal.
if(count($tagsName))
{
foreach($tagsName as $tagName)
{
$tag = new TagPropuesta();
$tag->setName($tagName);
$em->persist($tag);
$em->flush();
$propuesta->addTagPropuesta($tag);
$em->persist($propuesta);
$em->flush();
}
}
return new Response();
}
编辑:在回答了几个问题后,我尝试用它代替行
$propuesta->setCategoryId(1);
带有
$repository = $this->getDoctrine()->getRepository('JanderJanderBundle:PropuestasCategory');
$category = $repository->find(1);
//die(get_class($category));
$propuesta->setCategory($category);
但是错误消息是相同的。。
这里还有.yml文件和实体Propuesta(我根本没有修改Propue斯塔类,我只是使用generate:entities任务生成它):
JanderJanderBundleEntityPropuestas:
type: entity
table: propuestas
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
category_id:
type: integer
nullable: true
user_id:
type: integer
nullable: true
titulo:
type: string
length: 230
fixed: false
contenido:
type: string
length: 230
fixed: false
tema:
type: string
length: 40
fixed: false
nullable: true
eliminado:
type: boolean
nullable: true
created:
type: date
gedmo:
timestampable:
on: create
votes_up:
type: integer
nullable: true
votes_down:
type: integer
nullable: true
manyToOne:
category:
targetEntity: PropuestasCategory
inversedBy: propuestas
joinColumn:
name: category_id
referencedColumnName: id
usuario:
targetEntity: Usuario
inversedBy: propuestas
joinColumn:
name: user_id
referencedColumnName: id
manyToMany:
tags:
targetEntity: TagPropuesta
inversedBy: propuestas
lifecycleCallbacks: { }
<?php
namespace JanderJanderBundleEntity;
use DoctrineORMMapping as ORM;
/**
* JanderJanderBundleEntityPropuestas
*/
class Propuestas
{
/**
* @var integer $id
*/
private $id;
/**
* @var string $contenido
*/
private $contenido;
/**
* @var string $tema
*/
private $tema;
/**
* @var boolean $eliminado
*/
private $eliminado;
/**
* @var date $created
*/
private $created;
/**
* @var integer $votes
*/
private $votes;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set contenido
*
* @param string $contenido
*/
public function setContenido($contenido)
{
$this->contenido = $contenido;
}
/**
* Get contenido
*
* @return string
*/
public function getContenido()
{
return $this->contenido;
}
/**
* Set tema
*
* @param string $tema
*/
public function setTema($tema)
{
$this->tema = $tema;
}
/**
* Get tema
*
* @return string
*/
public function getTema()
{
return $this->tema;
}
/**
* Set eliminado
*
* @param boolean $eliminado
*/
public function setEliminado($eliminado)
{
$this->eliminado = $eliminado;
}
/**
* Get eliminado
*
* @return boolean
*/
public function getEliminado()
{
return $this->eliminado;
}
/**
* Set created
*
* @param date $created
*/
public function setCreated($created)
{
$this->created = $created;
}
/**
* Get created
*
* @return date
*/
public function getCreated()
{
return $this->created;
}
/**
* Set votes
*
* @param integer $votes
*/
public function setVotes($votes)
{
$this->votes = $votes;
}
/**
* Get votes
*
* @return integer
*/
public function getVotes()
{
return $this->votes;
}
/**
* @var integer $category_id
*/
private $category_id;
/**
* @var JanderJanderBundleEntityPropuestasCategory
*/
private $category;
/**
* Set category_id
*
* @param integer $categoryId
*/
public function setCategoryId($categoryId)
{
$this->category_id = $categoryId;
}
/**
* Get category_id
*
* @return integer
*/
public function getCategoryId()
{
return $this->category_id;
}
/**
* Set category
*
* @param JanderJanderBundleEntityPropuestasCategory $category
*/
public function setCategory(JanderJanderBundleEntityPropuestasCategory $category)
{
$this->category = $category;
}
/**
* Get category
*
* @return JanderJanderBundleEntityPropuestasCategory
*/
public function getCategory()
{
return $this->category;
}
/**
* @var JanderJanderBundleEntityTagPropuesta
*/
private $tags;
public function __construct()
{
$this->tags = new DoctrineCommonCollectionsArrayCollection();
}
/**
* Add tags
*
* @param JanderJanderBundleEntityTagPropuesta $tags
*/
public function addTagPropuesta(JanderJanderBundleEntityTagPropuesta $tags)
{
$this->tags[] = $tags;
}
/**
* Get tags
*
* @return DoctrineCommonCollectionsCollection
*/
public function getTags()
{
return $this->tags;
}
/**
* @var integer $user_id
*/
private $user_id;
/**
* @var JanderJanderBundleEntityUsuario
*/
private $usuario;
/**
* Set user_id
*
* @param integer $userId
*/
public function setUserId($userId)
{
$this->user_id = $userId;
}
/**
* Get user_id
*
* @return integer
*/
public function getUserId()
{
return $this->user_id;
}
/**
* Set usuario
*
* @param JanderJanderBundleEntityUsuario $usuario
*/
public function setUsuario(JanderJanderBundleEntityUsuario $usuario)
{
$this->usuario = $usuario;
}
/**
* Get usuario
*
* @return JanderJanderBundleEntityUsuario
*/
public function getUsuario()
{
if($this->usuario == null)
{
return "anonimo";
}else{
return $this->usuario;
}
}
/**
* @var integer $jander
*/
private $jander;
/**
* Set jander
*
* @param integer $jander
*/
public function setJander($jander)
{
$this->jander = $jander;
}
/**
* Get jander
*
* @return integer
*/
public function getJander()
{
return $this->jander;
}
/**
* @var integer $votes_up
*/
private $votes_up;
/**
* @var integer $votes_down
*/
private $votes_down;
/**
* Set votes_up
*
* @param integer $votesUp
*/
public function setVotesUp($votesUp)
{
$this->votes_up = $votesUp;
}
/**
* Get votes_up
*
* @return integer
*/
public function getVotesUp()
{
if($this->votes_up == null)
{
return 0;
}
else
{
return $this->votes_up;
}
}
/**
* Set votes_down
*
* @param integer $votesDown
*/
public function setVotesDown($votesDown)
{
$this->votes_down = $votesDown;
}
/**
* Get votes_down
*
* @return integer
*/
public function getVotesDown()
{
if($this->votes_down == null)
{
return 0;
}
else
{
return $this->votes_down;
}
}
public function getTotalVotes()
{
return ($this->getVotesDown()+$this->getVotesUp());
}
/**
* @var string $titulo
*/
private $titulo;
/**
* Set titulo
*
* @param string $titulo
*/
public function setTitulo($titulo)
{
$this->titulo = $titulo;
}
/**
* Get titulo
*
* @return string
*/
public function getTitulo()
{
return $this->titulo;
}
}
Javi
我认为在名为"propuesta"的数据库表中有一个foreignkey category_id,对吧?然后,在实体中必须有一个字段类别,而不是category_id,其set和get函数是setCategory()和getCategory(,而不是setCategoryId()。对于外键相关字段,必须预期其对象。所以在你的情况下
$category=$em->getDoctrine()->getEnitityManager()->getRepository('YourBundleName:Category')->find($id);
$propuesta->setCategory($category);//here category is an object.
因此,首先检查您的propuesta实体及其yml文件。
更新
1. Change your yml like this
JanderJanderBundleEntityPropuestas:
type: entity
table: propuestas
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
user_id:
type: integer
nullable: true
titulo:
type: string
length: 230
fixed: false
contenido:
type: string
length: 230
fixed: false
tema:
type: string
length: 40
fixed: false
nullable: true
eliminado:
type: boolean
nullable: true
created:
type: date
gedmo:
timestampable:
on: create
votes_up:
type: integer
nullable: true
votes_down:
type: integer
nullable: true
manyToOne:
category:
targetEntity: PropuestasCategory
inversedBy: propuestas
joinColumn:
name: category_id
referencedColumnName: id
usuario:
targetEntity: Usuario
inversedBy: propuestas
joinColumn:
name: user_id
referencedColumnName: id
manyToMany:
tags:
targetEntity: TagPropuesta
inversedBy: propuestas
lifecycleCallbacks: { }
您不需要在yml文件中指定category_id,只需指定关系即可。
更改您的实体文件也
删除字段categoryid及其setCategoryId()和getCategoryId)函数。
您必须更改其他表的所有yml文件,并给出如上所述的关系。同时更改其实体文件。
只需了解如何编写yml文件及其关系,然后使用symfony2的generate:enitites命令。
如果你不知道如何编写yml及其实体另一个好方法是反向处理1.首先创建数据库及其表,在msql中根据需要提供其外键关系。
在项目的parameters.ini文件中设置数据库连接。(希望您知道,只需提供数据库名称、用户名和密码(如果有的话))。
删除res/config/protice/和Entity文件中的所有yml文件。
4.打开你的终端,只需发出以下命令。下面的这些命令会自动生成所有实体文件和yml文件,它们具有您在数据库中指定的正确关系。
a) .php应用程序/控制台原则:映射:转换yml/src/Acme/BlogBundle/Resources/config/protrine--来自数据库--force//Acme/BogBundle/is命名空间
Symfony2从数据库生成实体
b) .php app/console原则:映射:import AcmeBlogBundle yml//AcmeBlogBundle是捆绑包名称c) .php应用程序/控制台原则:生成:实体AcmeBlogBundle
如果您有任何疑问,请参阅此链接单击此处
希望这能帮助您
需要考虑对象,而不是id;
$category = $em=>getReference('Category',1);
$propuesta->setCategory($category);
我假设你确实定义了命题和范畴之间的关系。