我有两张表backup_policy
和backup_settings
。 backup_policy
有一个名为backup_settings_id
的外键字段,其中backup_settings
id
字段已映射。这是为了创建两个表之间的关系而执行的查询
ALTER TABLE `backup_policy_scheduler`
ADD FOREIGN KEY (`backup_policy_id`) REFERENCES `backup_policy` (`id`)
现在的问题是,当我尝试这段代码时,屏幕会继续加载并给我最大的执行错误。
$repository=$this->getDoctrine()>getRepository('AppBundle:BackupPolicy');
$settings = $repository->findAll();
当我准备一个sql语句并执行fetchAll()时,它会获取结果。
当我尝试插入记录时,它会给我以下错误:
类型错误:传递给AppBundle \ Entity \ BackupPolicy :: setBackupSettings()的参数1必须是AppBundle \ Entity \ BackupSettings的实例,给定字符串,在/ var / www / symfonyproject / src / AppBundle / Controller / BackupPolicyController中调用第46行的.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* BackupPolicy
*
* @ORM\Table(name="backup_policy")
*@ORM\Entity(repositoryClass="AppBundle\Entity\Repository\BackupPolicyRepository")
*/
class BackupPolicy
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \BackupSettings
*
* @ORM\ManyToOne(targetEntity="BackupSettings")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="backup_settings_id", referencedColumnName="id")
* })
*/
private $backupSettings;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set backupSettings
*
* @param \AppBundle\Entity\BackupSettings $backupSettings
*
* @return BackupPolicy
*/
public function setBackupSettings(\AppBundle\Entity\BackupSettings $backupSettings = null)
{
$this->backupSettings = $backupSettings;
return $this;
}
/**
* Get backupSettings
*
* @return \AppBundle\Entity\BackupSettings
*/
public function getBackupSettings()
{
return $this->backupSettings;
}
}
答案 0 :(得分:2)
您只能插入BackupSettings实体。
在您的控制器行46中。
不是:
$backupPolicy->setBackupSettings('setting1');
但是像:
$repo=$this->getDoctrine()>getRepository('AppBundle:BackupSetting');
$backupPolicy->setBackupSettings($repo->findOneBy(array('name'=>'setting1'));