我已经创建了自己的服务,我需要注入doctrine EntityManager,但是我没有看到__construct()在我的服务上调用它,并且注入不起作用.
这是代码和配置:
<?php
namespace Test\CommonBundle\Services;
use Doctrine\ORM\EntityManager;
class UserService {
/**
*
* @var EntityManager
*/
protected $em;
public function __constructor(EntityManager $entityManager)
{
var_dump($entityManager);
exit(); // I've never saw it happen, looks like constructor never called
$this->em = $entityManager;
}
public function getUser($userId){
var_dump($this->em ); // outputs null
}
}
Run Code Online (Sandbox Code Playgroud)
这是services.yml我的捆绑
services:
test.common.userservice:
class: Test\CommonBundle\Services\UserService
arguments:
entityManager: "@doctrine.orm.entity_manager"
Run Code Online (Sandbox Code Playgroud)
我已经config.yml在我的应用程序中导入了.yml
imports:
# a few lines skipped, not relevant here, i think
- { resource: …Run Code Online (Sandbox Code Playgroud) 我正在使用嵌入的 Symfony 表单Tag从文章编辑器中添加和删除实体。Article是协会的拥有方:
class Article
{
/**
* @ManyToMany(targetEntity="Tags", inversedBy="articles", cascade={"persist"})
*/
private $tags;
public function addTag(Tag $tags)
{
if (!$this->tags->contains($tags)) // It is always true.
$this->tags[] = $tags;
}
}
Run Code Online (Sandbox Code Playgroud)
条件在这里没有帮助,因为它始终为真,如果不是,则根本不会将新标签持久化到数据库中。这是Tag实体:
class Tag
{
/**
* @Column(unique=true)
*/
private $name
/**
* @ManyToMany(targetEntity="Articles", mappedBy="tags")
*/
private $articles;
public function addArticle(Article $articles)
{
$this->articles[] = $articles;
}
}
Run Code Online (Sandbox Code Playgroud)
我已设置$name为唯一,因为我想每次在表单中输入相同的名称时都使用相同的标签。但它不能这样工作,我得到了例外:
违反完整性约束:1062 重复条目
我需要更改什么才能使用article_tag提交标记名称时的默认连接表,该Tag表已在表中?