0教义实体设定者无效

0教义实体设定者无效

本文介绍了Symfony 2.0教义实体设定者无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表的数据库缺少字段:id,date,duration,user_id,typ_id ..我还有2个表User和AbsenceTyp。一切与* _id被定义为一个FK到相应的表。



我生成了getter / setters,并用以下命令更新了我的数据库:

  php app / console doctrine:generate:entities MyTestBundle 
php app / console doctrine:schema:update --force

(没有产生任何错误...)



保存缺席:

  $ ab = new Absence(); 
$ ab-> setUserId(1);
$ ab-> setDate($ date);
$ ab-> setDuration('0.5');
$ ab-> setTypId(2);

$ em-> persist($ ab);
$ em-> flush();

...导致这个sql-statment:

 'INSERT INTO缺少(日期,持续时间,user_id,typ_id)与params相关的值(?,?,?,?)'[2014-07-07, 0.5,null,null]:

所有FK的设置器都不工作...但是Getter完成他们的事情(例如getTypId())
任何想法?在这里我的缺席实体:

 <?php 
命名空间MyTest\Bundle \MainBundle\Entity;

使用Doctrine\ORM\Mapping作为ORM;

/ **
* @ ORM\Entity(repositoryClass =AbsenceRepository)
* @ ORM\Table(name =absent,indexes = {@ ORM \Index(name =fk_absence_user,columns = {user_id}),@ ORM\Index(name =fk_absence_absencetyp,columns = {typ_id})})
* /
class Absence
{
/ **
* @ ORM\Id
* @ ORM\Column(type =integer)
* @ ORM\\ \\GeneratedValue(strategy =AUTO)
* /
protected $ id;

/ **
* @ ORM\Column(type =date,nullable = false)
* /
protected $ date;

/ **
* @ ORM\Column(type =decimal,precision = 5,scale = 2,nullable = false)
* /
保护$持续时间;

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

/ **
* @ ORM\ManyToOne(targetEntity =MyTest\Bundle\UserBundle\Entity\User,inversedBy =缺席)
* @ ORM\JoinColumn(name =user_id,referencedColumnName =id)
* /
protected $ user;

/ **
* @ ORM\Column(type =integer,nullable = false)
* /
protected $ typ_id;

/ **
* @ ORM\ManyToOne(targetEntity =MyTest\Bundle\MainBundle\Entity\AbsenceTyp,inversedBy =缺席)
* @ ORM\JoinColumn(name =typ_id,referencedColumnName =id)
* /
protected $ absentTyp;

... //生成Getter / Setters


解决方案

当您有JOIN关系时,必须使用对象设置器来设置连接列的值。所以例如,要设置user_id字段的值,您需要使用 - > setUser($ userObject)setter而不是setUserId()。



不需要在实体中指定$ user_id字段。有时,如果您需要在数据库的SELECTs期间直接访问user_id值,而不必获取用户对象,那么这样做有用。



您的代码可以看起来像这样:

  $ user = new User(); //假的用户或真正的用户对象,如果你从数据库已经获取了
$ user-> setId(5);

$ ab = new Absence();
$ ab-> setUser($ user);
// ...设置其他字段
$ em-> persist($ ab);
$ em-> flush();


I have a database with a table Absence with fields: id, date, duration, user_id, typ_id.. I also have 2 tables User and AbsenceTyp. Everything with *_id is definied as a FK to the according table.

I generated getters/setters and updated my DB with the following commands:

php app/console doctrine:generate:entities MyTestBundle
php app/console doctrine:schema:update --force

(Didn't generate any errors ...)

Now by saving an absence:

$ab = new Absence();
$ab->setUserId(1);
$ab->setDate($date);
$ab->setDuration('0.5');
$ab->setTypId(2);

$em->persist($ab);
$em->flush();

...it lead to this sql-statment:

'INSERT INTO absence (date, duration, user_id, typ_id) VALUES (?, ?, ?, ?)' with params ["2014-07-07", "0.5", null, null]:

Setters for all the FKs aren't working... But Getters do their thing perfectly (e.g. getTypId())Any ideas? Here my Absence-Entity:

<?php
namespace MyTest\Bundle\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="AbsenceRepository")
 * @ORM\Table(name="absence", indexes={@ORM\Index(name="fk_absence_user", columns=        {"user_id"}), @ORM\Index(name="fk_absence_absencetyp", columns={"typ_id"})})
 */
class Absence
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="date", nullable=false)
 */
protected $date;

/**
 * @ORM\Column(type="decimal", precision=5, scale=2, nullable=false)
 */
protected $duration;

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

/**
 * @ORM\ManyToOne(targetEntity="MyTest\Bundle\UserBundle\Entity\User", inversedBy="absences")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
protected $user;

/**
 * @ORM\Column(type="integer", nullable=false)
 */
protected $typ_id;

/**
 * @ORM\ManyToOne(targetEntity="MyTest\Bundle\MainBundle\Entity\AbsenceTyp", inversedBy="absences")
 * @ORM\JoinColumn(name="typ_id", referencedColumnName="id")
 */
protected $absenceTyp;

... // generated Getter / Setters
解决方案

When you have a JOIN relationship you must use the object setter to set the value of joined column. So for example, to set the value for user_id field you need to use ->setUser($userObject) setter instead of the setUserId().

Actually specifying the $user_id field in your entity is not required. It can be useful sometimes to do so, if you need to get access directly the user_id value during SELECTs from the database without having to fetch the User Object.

Your code could look something like this:

$user = new User(); //fake user or real user object if you have it fetched from db already
$user->setId(5);

$ab = new Absence();
$ab->setUser($user);
// ... set other fields
$em->persist($ab);
$em->flush();

这篇关于Symfony 2.0教义实体设定者无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:37