【发布时间】:2014-06-15 17:23:33
【问题描述】:
我正在创建一个自定义类,我在我的应用程序中用作服务以将数据库数据提供到不同的区域,所以我有一个表单构建器,在创建之前我需要传递一个用户列表,所以我' m 创建服务:
站点/捆绑包/服务/用户服务:
<?php
namespace Mangocele\coreBundle\Services;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class UserService {
protected $entities;
public function __construct($em){
$this->entities = $em->getRepository('MangoceleUserBundle:User')->findAll();
}
public function getEntities(){
return $this->entities;
}
}
然后我在我的 bundle service.yml 中注册:
services:
my.custom.service.id:
class: Mangocele\coreBundle\Services\UserService
arguments: ["@doctrine.orm.entity_manager"]
在 app/config 中导入:
imports:
...
- { resource: "@MangocelecoreBundle/Resources/config/services.yml" }
并尝试在我的表单构建器调用中调用它[Mangocele\coreBundle\Form]:
<?php
namespace Mangocele\coreBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Mangocele\coreBundle\Services\UserService;
use Mangocele\UserBundle\Entity\User;
use Mangocele\UserBundle\Form\UserType;
use Doctrine\ORM\EntityManager;
class EmpresasType extends AbstractType
{
private function getUserData(){
$userEntities = $this->get('my.custom.service.id');
$entities = $userEntities->getEntities();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$userEntities = $this->get('my.custom.service.id');
$entities = $userEntities->getEntities();
print_r($entities);
die();
}
}
但最后当我尝试得到结果时,我得到了这个错误:
UndefinedMethodException: Attempted to call method "get" on class "Mangocele\coreBundle\Form\EmpresasType" in /Applications/XAMPP/xamppfiles/htdocs/mangocele/site/src/Mangocele/coreBundle/Form/EmpresasType.php line 29. Did you mean to call: "getName", "getParent"?
不知道我做错了什么。提前致谢。
【问题讨论】:
标签: php symfony service doctrine-orm entity