- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 Symdony2 项目中,我有两个相关的实体:Service 和 ServiceGroup。这应该是多对多的关系,因为每个组可以有多个服务,每个服务又可以属于多个组。此外,我需要一个用户界面来管理服务和组。因此,在编辑服务时,用户应该能够选择它所属的组。类似地,在编辑 ServiceGroup 时,用户应该能够选择哪些服务属于该组。我已经通过在我的 Doctrine 实体中设置多对多关系来实现这一点。一切都像魅力一样工作,包括基于 Symfony2 中自定义表单类型构建的用户界面(我使用“实体”表单字段类型来允许用户在 ServiceGroup 编辑器中选择服务和在服务编辑器中选择组)。我唯一的问题是我不能再使用 Doctrine 命令行来更新数据库架构。
这是我的服务实体源代码的一部分:
class Service
{
/**
* @var ArrayCollection $groups
* @ORM\ManyToMany(targetEntity="ServiceGroup")
* @ORM\JoinTable(
* name="service_servicegroup",
* joinColumns={@ORM\JoinColumn(name="service_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="servicegroup_id", referencedColumnName="id")}
* )
*/
private $groups;
}
这是我的 ServiceGroup 实体源代码的一部分:
class ServiceGroup
{
/**
* @var ArrayCollection $services
* @ORM\ManyToMany(targetEntity="Service")
* @ORM\JoinTable(
* name="service_servicegroup",
* joinColumns={@ORM\JoinColumn(name="servicegroup_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="service_id", referencedColumnName="id")}
* )
*/
private $services;
}
我在这两种情况下都使用 JoinTable,因为这是我发现在用户界面编辑器中保存关系的唯一方法,如下所示:
Service editor
Name: [ Service 1 ]
Groups to which this service belongs:
[x] Group A
[ ] Group B
[ ] Group C
[ SAVE ]
Group editor
Name: [ Group A ]
Services belongs to this group:
[x] Service 1
[ ] Service 2
[ ] Service 3
[ SAVE ]
php app/console doctrine:schema:update --dump-sql
我收到此异常:
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'service_servicegroup' already exists.
看起来 Doctrine 试图为每个 JoinTable 语句创建“service_servicegroup”表。因此,它正在处理当前模式,我使用相同的命令在数据库中构建了该模式,但逐步进行,首先没有定义多对多关系,然后只有一个多对多关系定义(为服务实体)。当我将多对多关系添加到第二个实体 (ServiceGroup) 时,从用户的角度来看,我的应用程序可以正常工作,但我无法再使用“doctrine:schema:update”命令.
class Service
{
/**
* @var ArrayCollection $groups
* @ORM\ManyToMany(targetEntity="ServiceGroup", inversedBy="services", cascade={"all"})
* @ORM\JoinTable(
* name="service_to_group_assoc",
* joinColumns={@ORM\JoinColumn(name="service_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
private $groups;
}
而 ServiceGroup 实体是反面:
class ServiceGroup
{
/**
* @var ArrayCollection $services
* @ORM\ManyToMany(targetEntity="Service", mappedBy="groups", cascade={"all"})
*/
private $services;
}
不幸的是,使用这种配置,关系仅在更新服务实体时更新。当我更改 ServiceGroup 对象中的 $services 并将其持久化时,关系将保持不变。因此,我更改了我的 Controller 类,并在小型解决方案的帮助下,达到了预期的结果。这是我的 Controller 代码的一部分,它负责更新 ServiceGroup 实体(使用自定义表单类型):
// before update - get copy of currently related services:
$services = clone $group->getServices();
// ...
// when $form->isValid() etc. updating the ServiceGroup entity:
// add selected services to group
foreach($group->getServices() as $service)
{
$service->addServiceGroup($group);
$services->removeElement($service);
}
// remove unselected services from group
foreach($services as $service)
{
$service->removeServiceGroup($group);
}
这是 Service 实体类的 addServiceGroup 和 removeServiceGroup 方法的实现:
/**
* Add group
*
* @param ServiceGroup $groups
*/
public function addServiceGroup(ServiceGroup $groups)
{
if(!in_array($groups, $this->groups->toArray()))
{
$this->groups[] = $groups;
}
}
/**
* Remove group
*
* @param ServiceGroup $groups
*/
public function removeServiceGroup(ServiceGroup $groups)
{
$key = $this->groups->indexOf($groups);
if($key!==FALSE)
{
$this->groups->remove($key);
}
}
现在我与拥有 (Service) 和反向 (ServiceGroup) 端建立了多对多关系,以及在保存时更新实体和关系的表单(服务实体的默认表单就足够了,但对于我在上面提供的 ServiceGroup)修改)。 Symfony/Doctrine 控制台工具就像一个魅力。这可能可以以更好(更简单?)的方式解决,但对我来说这现在就足够了。
最佳答案
多对多关联的默认行为将有一个拥有方和一个反向方。如果您正在处理关联的拥有方,那么关联将由 ORM 处理。但是,如果您正在处理反面,则由您明确处理此问题。
在你的情况下,你有 mappedBy="groups"
在 $services
ServiceGroup
的属性(property)类(class)。这意味着,Service 和ServiceGroup 之间的这种关联由$groups
维护。 Service
的属性(property)实体。所以,Service
成为该协会的拥有方。
现在,假设您正在创建或更新 Service
实体。在这里添加时ServiceGroup
实体到此 Service
实体,并坚持 Service
实体,所有实体和相关关联都是由 ORM 自动构建的。
但是,如果您正在创建或更新 ServiceGroup
实体,当您添加 Service
时实体,并坚持 ServiceGroup
实体,关联是不是 由 ORM 构建。作为一种解决方法,您必须明确添加 ServiceGroup
实体到 Service
实体。
// in ServiceGroup.php
public function addService(Service $service)
{
$service->addServiceGroup($this);
$this->services->add($service);
}
// in Service.php
public function addServiceGroup(ServiceGroup $group)
{
if (!$this->serviceGroups->contains($group)) {
$this->serviceGroups->add($group);
}
}
$services
和 $serviceGroups
到实体创建时的学说集合,即在构造函数中。 'by_reference' => false
在您的 services
ServiceGroup
的 FormType 中的字段. 关于Symfony2 Doctrine 多对多关系与两个拥有方和 Doctrine cmd 行工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9582547/
我开始从事一个用 Symfony 2.8 编写的大型项目。将整个项目升级到 SF 3 需要数百小时,现在还不可能。我想到了一个想法,将 symfony/symfony 包解压到它替换的单个包中(com
我在提交表单后使用 FOSUserEvents,但订阅者调用了两次。 这样我的验证码第一次有效第二次无效 这是我的代码 router = $router; $this->request
我有以下路线: blog_show: path: /test/123 defaults: { _controller: TotalcanBravofillBundle:Te
我是测试新手。我想测试我的功能。我已经成功安装了 phpUnit。我在互联网上查看了许多教程。但我无法获得有关测试的正确信息。这是我的功能代码: public function loginAction
我正在尝试重现 facebook batch requests 的行为在他们的图形 api 上运行。 所以我认为最简单的解决方案是在 Controller 上向我的应用程序发出几个请求,例如: pub
在 Symfony Progress Bar documentation有一个超酷酒吧的示例图像。不幸的是,看起来文档的其余部分没有解释如何获得这样的结果。 这是图片,以防您错过: 我怎么才能得到它?
我使用Finder发送假脱机电子邮件,但是自动名称生成器将点放在文件名中,有时它们出现在文件的开头。 查找程序似乎无法获取具有该名称的文件-那些文件被隐藏了……有人经历过这种行为吗?有什么建议如何使用
我正在尝试进行 LDAP 身份验证,我目前遇到此类错误: ServiceNotFoundException: The service "security.firewall.map.context.ma
有没有办法验证和检查集合数组是否为空。我已经尝试过: /** * @Assert\NotBlank() * @Assert\Length( min = 1) */ protected $work
使用Smyfony2和Doctrin2,可以使用以下示例创建数据固定装置:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/i
我看到在大多数Symfony 2示例中,例如,不存在记录时,Symfony 2会引发异常。我认为这种方法对最终用户不友好。为什么有人更喜欢引发异常而不在Flashbag上添加一些错误消息? 最佳答案
我对项目中的以下服务有疑问: app.security.guardAuthenticatorLoginPassword: class: AppBundle\Security\LoginPa
symfony缓存和登录Docker容器存在问题。 Web服务器从www-data用户和组执行,当我使用docker上安装的php从docker容器中清除symfony缓存时,它从root执行。 因此
我想了解 symfony 中的服务 我已阅读http://symfony.com/doc/2.3/book/service_container.html#creating-configuring-se
因为我对 Symfony 和 Doctrine 还很陌生,所以我有一个可能很愚蠢的问题;-) 有人可以用简单的词语向我解释集合(尤其是实体中的ArrayCollections)吗?它是什么以及何时以及
我收到了这个表格: {{ form_start(form) }} {{ form_end(form) }} 我想检查用户是否登录,我这样做了: {% if is_g
我的网站已准备好部署,我正在尝试将其设置为在线。 一些信息: 主持人是 OVH。 它不允许 SSH,我必须使用 FTP 发送文件。也没有命令行。 我现在希望能够在子目录中设置网站:/www/test(
过去几个月以来,我一直在尝试与symfony合作。昨晚我自动删除了不需要的存储库。之后,我无法使用symfony命令创建新的symfony项目。当我在终端中运行Symfony new Security
In the environnement variable, then in system variable, I edited the path and added在环境变量中,然后在系统变量
我们有一个 Symfony 1.4 应用程序,想升级到 Symfony 4。是否有可能或者我们必须重新编程该应用程序? 我们询问了我们附近的一家软件公司,他们告诉我们必须重新编写应用程序。 最佳答案
我是一名优秀的程序员,十分优秀!