当我尝试更新我的数据库时出现此错误:
我不确定,但我认为错误意味着表格中的列tva_id的值飞了
你正在插入表tva上不存在。当我在表Post中检查phpmyadmin tva (
id )) should be link to
(tva_id
)`但不是。我该怎么做才能解决这个错误?
[学说\ DBAL \ DBALException]
执行'ALTER TABLE post ADD CONSTRAINT时发生异常 FK_5 A8A6C8D4D79775F外键(tva_id)参考文献tva(id)':
SQLSTATE [23000]:完整性约束违规:1452无法添加或 更新子行:外键约束失败 (fly
。#sql-93c_155
,CONSTRAI NTFK_5A8A6C8D4D79775F
外国人 KEY(tva_id
)参考tva
(id
))
Tva.php
<?php
namespace FLY\BookingsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Tva
*
* @ORM\Table("tva")
* @ORM\Entity(repositoryClass="FLY\BookingsBundle\Entity\TvaRepository")
*/
class Tva
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var float
*
* @ORM\Column(name="multiplicate", type="float")
*/
private $multiplicate;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=125)
*/
private $name;
/**
* @var float
*
* @ORM\Column(name="value", type="float")
*/
private $value;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set multiplicate
*
* @param float $multiplicate
* @return Tva
*/
public function setMultiplicate($multiplicate)
{
$this->multiplicate = $multiplicate;
return $this;
}
/**
* Get multiplicate
*
* @return float
*/
public function getMultiplicate()
{
return $this->multiplicate;
}
/**
* Set name
*
* @param string $name
* @return Tva
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set value
*
* @param float $value
* @return Tva
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* @return float
*/
public function getValue()
{
return $this->value;
}
}
post.php中
<?php
namespace FLY\BookingsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Application\Sonata\UserBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use JMS\SecurityExtraBundle\Annotation\Secure;
/**
* Post
*
* @ORM\Table(name="post")
* @ORM\Entity(repositoryClass="FLY\BookingsBundle\Entity\PostRepository")
*/
class Post
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Tva", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $tva;
/**
*
*
* @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User")
* @ORM\JoinColumn(onDelete="CASCADE")
* @Security("user.getId() == post.getOwner()")
*/
private $owner;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return User
*/
public function getOwner()
{
return $this->owner;
}
/*
* @param User $owner
*/
public function setOwner(User $owner)
{
$this->owner = $owner;
return $this;
}
/**
* Set tva
*
* @param \FLY\BookingsBundle\Entity\Tva $tva
* @return Post
*/
public function setTva(\FLY\BookingsBundle\Entity\Tva $tva)
{
$this->tva = $tva;
return $this;
}
/**
* Get tva
*
* @return \FLY\BookingsBundle\Entity\Tva
*/
public function getTva()
{
return $this->tva;
}
}
user.php的
<?php
namespace Application\Sonata\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use ArrayIterator;
use Closure;
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
/**
* This file has been generated by the Sonata EasyExtends bundle ( http://sonata-project.org/easy-extends )
*
*/
/**
* @ORM\Entity(repositoryClass="FLY\UserBundle\Repository\UserRepository")
* @ORM\Table(name="fos_user_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
$this->commandes = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\Commandes", mappedBy="user", cascade={"remove"})
* @ORM\JoinColumn(nullable=true)
*/
private $commandes;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add commandes
*
* @param \FLY\BookingsBundle\Entity\Commandes $commandes
* @return User
*/
public function addCommande(\FLY\BookingsBundle\Entity\Commandes $commandes)
{
$this->commandes[] = $commandes;
return $this;
}
/**
* Remove commandes
*
* @param \FLY\BookingsBundle\Entity\Commandes $commandes
*/
public function removeCommande(\FLY\BookingsBundle\Entity\Commandes $commandes)
{
$this->commandes->removeElement($commandes);
}
/**
* Get commandes
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCommandes()
{
return $this->commandes;
}
}