与Laravel中同一模型的多重关系

时间:2015-04-28 10:41:01

标签: php laravel eloquent laravel-5

我在Eloquent中有多对多的关系。

架构:

templates       modules         module_template
- id            - id            - module_id
- name          - content       - template_id
                                - order

但是,我需要让单个模板可以多次包含同一个模块。我该怎么做?

示例module_template

module_id | template_id | order
1         | 42          | 1
4         | 42          | 2
1         | 42          | 3

$template->modules()->detach($moduleId)detach所有与模特的关系,对吗?假设它甚至可能首先attach

2 个答案:

答案 0 :(得分:2)

你可以14:00:35,458 TRACE [org.jboss.security.auth.spi.LdapExtLoginModule] initialize 14:00:35,459 TRACE [org.jboss.security.auth.spi.LdapExtLoginModule] Security domain: seycon 14:00:35,459 TRACE [org.jboss.security.auth.spi.LdapExtLoginModule] login 14:00:35,512 DEBUG [org.jboss.security.auth.spi.LdapExtLoginModule] Bad password for username=Andreu Serra 14:00:35,520 TRACE [org.jboss.security.auth.spi.LdapExtLoginModule] abort 14:00:35,520 TRACE [org.jboss.security.plugins.auth.JaasSecurityManagerBase.seycon] Login failure javax.security.auth.login.FailedLoginException: Password Incorrect/Password Required at org.jboss.security.auth.spi.UsernamePasswordLoginModule.login(UsernamePasswordLoginModule.java:252) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at javax.security.auth.login.LoginContext.invoke(LoginContext.java:762) at javax.security.auth.login.LoginContext.access$000(LoginContext.java:203) at javax.security.auth.login.LoginContext$4.run(LoginContext.java:690) at javax.security.auth.login.LoginContext$4.run(LoginContext.java:688) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:687) at javax.security.auth.login.LoginContext.login(LoginContext.java:595) at org.jboss.security.plugins.auth.JaasSecurityManagerBase.defaultLogin(JaasSecurityManagerBase.java:552) at org.jboss.security.plugins.auth.JaasSecurityManagerBase.authenticate(JaasSecurityManagerBase.java:486) at org.jboss.security.plugins.auth.JaasSecurityManagerBase.isValid(JaasSecurityManagerBase.java:365) at org.jboss.security.plugins.JaasSecurityManager.isValid(JaasSecurityManager.java:160) at org.jboss.web.tomcat.security.JBossWebRealm.authenticate(JBossWebRealm.java:384) at org.apache.catalina.authenticator.BasicAuthenticator.authenticate(BasicAuthenticator.java:181) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92) at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126) at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:745) 14:00:35,521 TRACE [org.jboss.security.plugins.auth.JaasSecurityManagerBase.seycon] End isValid, false 14:00:35,521 TRACE [org.jboss.security.SecurityRolesAssociation] Setting threadlocal:null 14:00:35,521 TRACE [org.jboss.security.SecurityRolesAssociation] Setting threadlocal:null 多次,没问题。但是attach将删除所有关联。 (当然,您的表格不能应用detach唯一/主要约束)

但是,不要害怕:)你仍然可以手动完成:

FK1,FK2

答案 1 :(得分:1)

如果将module_template定义为自己的Eloquent Model ModuleTemplate

,您将能够这样做

通过这种方式,您可以在不依赖attach()和detach()及其检查的情况下插入和删除表中的行。