【发布时间】:2017-05-20 23:55:56
【问题描述】:
我将此类服务链接到我的实体中的一种方法:
entity_form:
class: AppBundle\Entity\EntityForm
calls:
- [setDoctrine, ['@doctrine']]
参数将(或至少应该)doctrine 注入到我的实体中,以便我可以从 db 内部方法中获取内容。
我为 getter 设置了它,因为我不得不省略在 __construct 中设置参数,因为在代码中该类永远不会真正“构造”
实体本身如下所示:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="entity_form")
*/
class EntityForm
{
protected $id;
protected $url;
protected $name;
protected $className;
protected $description;
private $doctrine;
public function getId()
{
return $this->id;
}
/* _All Getters An Setters Here.._ */
private function setDoctrine($doctrine)
{
return $doctrine;
}
public function createEntity($id)
{
$this->setDoctrine();
$entityPath = sprintf('AppBundle:%s:%s', __NAMESPACE__, $this->getClassName());
var_dump($this->doctrine);
exit;
//var_dump is temponary and only for testing the doctrine
$entity = $this->doctrine->getRepository($entityPath)->find($id);
return $entity;
}
}
该实体存储有关网站上表格的信息。有了这个,我想从给定的 Id 中快速创建表单的类实体。
很遗憾
运行这样的代码会给我这个错误:
警告:缺少参数 1 AppBundle\Entity\EntityForm::setDoctrine(),调用 C:\PHP\Repos\centaur\src\AppBundle\Entity\EntityForm.php 在第 139 行 并定义
发生此错误的原因可能是什么?我该如何解决?
任何帮助都会很棒
【问题讨论】:
标签: php symfony oop doctrine symfony-3.2