[英]Symfony 4.1 and Doctrine asossiation Problems
我在Stackoverflow和理论文档上读了很多文章,但是在此代码中找不到错误。 也许有人可以引导我朝正确的方向前进,因为我确定我对这个概念没有把握。 这是我的第一个带有理论的项目。
我有三个实体,数据库生成正确,但是在执行过程中我总是会收到这些错误
PHP ./bin/控制台学说:模式:验证
实体如下(简称):
[失败]实体类App \\ Entity \\ DeployedTrap映射无效:*关联App \\ Entity \\ DeployedTrap#trapId引用未定义为关联的反字段App \\ Entity \\ TrapDefinition#id。 *关联App \\ Entity \\ DeployedTrap#trapId表示不存在的反向字段App \\ Entity \\ TrapDefinition#id。 *关联App \\ Entity \\ DeployedTrap#customer是指未定义为关联的反字段App \\ Entity \\ Customer#customerId。 *关联App \\ Entity \\ DeployedTrap#customer指不存在的反面字段App \\ Entity \\ Customer#customerId。
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
*
* @ORM\Table(name="traps_deployed")
* @ORM\Entity(repositoryClass="App\Repository\DeployedTrapRepository")
*/
class DeployedTrap
{
/**
* @ORM\Column(name="id", type="integer", options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* Owning side
* @ORM\JoinColumn(name="trap_id", referencedColumnName="id")
* @ORM\ManyToOne(targetEntity="TrapDefinition", inversedBy="id")
*/
public $trapId;
/**
* @ORM\JoinColumn(name="customer", referencedColumnName="customerId")
* @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="customerId")
*/
private $customer;
}
类陷阱定义:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="traps_definition")
* @ORM\Entity(repositoryClass="App\Repository\TrapDefinitionRepository")
*/
class TrapDefinition
{
/**
* @ORM\Column(name="id",type="integer", options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\OneToMany(targetEntity="DeployedTrap", mappedBy="trapId")
*/
public $id;
public function __construct()
{
$this->id = new ArrayCollection();
}
}
类客户:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Events;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="customers")
* @ORM\Entity(repositoryClass="Doctrine\ORM\EntityRepository")
* @ORM\HasLifecycleCallbacks
*/
class Customer
{
/**
* @ORM\Column(type="integer",name="customerId",length=11, nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\OneToMany(targetEntity="DeployedTrap", mappedBy="customer")
*/
public $customerId;
public function __construct()
{
$this->customerId = new ArrayCollection();
}
}
第一:忘记通过$trapId
中的Id(请参阅DeployerTrap
$trapId
)链接模型的本机数据库方法。 您将始终将对象/集合彼此链接,因此$trapId
应该是$trapDefinition
。
这是您的主要问题
只需在您的TrapDefinition
类中添加字段$deployerTraps
/**
* @ORM\OneToMany(targetEntity="DeployedTrap", mappedBy="trapDefinition")
*/
public $deployerTraps;
并在DeployedTrap
类$trapId
$trapDefinition
重命名为$trapId
/**
* @ORM\ManyToOne(targetEntity="TrapDefinition", inversedBy="deployerTraps")
*/
public $trapDefinition;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.