如何在yaml symfony2中定义变量
我不太熟悉如何在yaml symfony2中定义变量,symfony,doctrine-orm,yaml,Symfony,Doctrine Orm,Yaml,我不太熟悉YAML,所以我打开parameters.yml和config.yml文件,查看如何在YAML中使用参数或变量的示例 参数。yml: parameters: database_driver: pdo_mysql database_host: 127.0.0.1 database_port: 3306 database_name: homlist doctrine: dbal: driver: "%database_driver%" ho
YAML
,所以我打开parameters.yml和config.yml文件,查看如何在YAML
中使用参数或变量的示例
参数。yml:
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: 3306
database_name: homlist
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
config.yml:
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: 3306
database_name: homlist
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
但当我尝试使用yaml文件时,如下所示:
parameters:
table_name: test
Mockizart\Bundle\BlogBundle\Entity\MockblogTag:
type: entity
table: "%table_name%"
services:
your_user.manager:
class: YourNamespace\UserBundle\Manager\UserManager
arguments: ['@doctrine.orm.entity_manager', 'YourNamespace\UserBundle\Entity\User']
$userManager = $this->get('your_user.manager');
$user = $userManager->createInstance();
是这样的错误:
parameters:
table_name: test
Mockizart\Bundle\BlogBundle\Entity\MockblogTag:
type: entity
table: "%table_name%"
services:
your_user.manager:
class: YourNamespace\UserBundle\Manager\UserManager
arguments: ['@doctrine.orm.entity_manager', 'YourNamespace\UserBundle\Entity\User']
$userManager = $this->get('your_user.manager');
$user = $userManager->createInstance();
从%table\u name%%0\u执行“选择计数(不同的%0\u.id)作为sclr0”时发生异常:
这是我的映射文件Resources\Config\Entity\MockblogTag
Mockizart\Bundle\BlogBundle\Entity\MockblogTag:
type: entity
table: mockblog_tag
indexes:
user_id:
columns:
- user_id
name:
columns:
- name
slug:
columns:
- slug
id:
id:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
generator:
strategy: IDENTITY
fields:
dateCreated:
type: integer
nullable: false
unsigned: false
comment: ''
column: date_created
name:
type: string
nullable: false
length: 60
fixed: false
comment: ''
slug:
type: string
nullable: false
length: 100
fixed: false
comment: ''
totalPost:
type: integer
nullable: false
unsigned: false
comment: ''
column: total_post
manyToOne:
user:
targetEntity: ORD\UserBundle\Entity\User
joinColumn:
referencedColumnName: id
type: integer
nullable: false
unsigned: false
lifecycleCallbacks:
如何在yaml symfony2中定义变量?定义参数的方法是正确的,但是我从评论中看到,您的目的是配置用于用户对象的类: 就像Cerad说的,你不能那样做。但是,如果要为用户配置使用的类,可以使用manager服务类
<?php
namespace YourNamespace\UserBundle\Manager;
use Doctrine\Common\Persistence\ObjectManager;
class UserManager
{
/**
* @var ObjectManager
*/
protected $em;
/**
* Your user class
*
* @var string
*/
protected $className;
public function __construct(ObjectManager $em, $class)
{
$this->em = $em;
$this->className = $class;
}
public function createInstance()
{
return new $this->className;
}
public function getRepository()
{
return $this->em->getRepository($this->className);
}
}
在控制器中,您可以像这样使用此管理器类:
parameters:
table_name: test
Mockizart\Bundle\BlogBundle\Entity\MockblogTag:
type: entity
table: "%table_name%"
services:
your_user.manager:
class: YourNamespace\UserBundle\Manager\UserManager
arguments: ['@doctrine.orm.entity_manager', 'YourNamespace\UserBundle\Entity\User']
$userManager = $this->get('your_user.manager');
$user = $userManager->createInstance();
我认为这是在处理用户对象时有一个中心点的好方法。如果有一天出于任何原因决定为用户使用不同的类,只需修改参数“YourNamespace\UserBundle\Entity\user”
同样,您可以使用“YourNamespace\UserBundle\Entity\User”参数作为参数,因此定义将更改为:
services:
your_user.manager:
class: Moneytablet\UserBundle\Manager\UserManager
arguments: ['@doctrine.orm.entity_manager', '%user_class%']
在您的parameters.yml中,您可以有:
parameters:
user_class: YouNamespace\UserBundle\Entity\User
我非常喜欢这种工作方式,您可以在管理器类上创建save()、remove()方法等等。此外,在以后创建新服务时,如果此管理器是依赖项,则可以将其作为常规服务注入
如果您想为不同的实体创建一个新的管理器,可以使用不同的构造参数创建一个新的服务定义,但是使用相同的服务类。您尝试过在parameters.yml文件中定义表名吗?@A.L通过如何将其导入
app/config/parameters.yml
到MyVendor/Bundle/BlogBundle/entity/Tag.orm.yml
中的我的实体中的方式来定义表名。映射文件似乎是由条令读取的,而且Symfony2配置无法通过条令访问。对不起,我再也帮不了你了。您希望表名可配置是有原因的吗?表名只是一个示例,但事实上我想将其用于关系targetEntity:ORD\UserBundle\Entity\User
我希望将ORD\UserBundle\Entity\User
集中到一个位置。因此,如果有一天我想更改ORD\UserBundle\Entity\User
,我只需要在一个地方更改它。怎么做?正如@A.L.所说,你不能。整个参数是Symfony 2框架的一部分。除非您想尝试重写某些解析器,否则条令没有这种功能。我没有对条令使用注释映射,因此我需要ORD\UserBundle\Entity\User
在映射文件Resources\Entity\Tag.orm.yml
中定义关系manytone。据我所知,上面的代码正在创建服务,并没有解决我的问题。@zuzuleinen-只需将用户存储库定义为服务,就可以节省大量的精力。@Cerad如果您只需要存储库,是的,这是个好主意。但我也遇到过这样的情况,用户经理真的很有帮助,当我使用用户类时需要改变一些事情。此外,FOSUser捆绑包在与用户合作时也有类似的方法。