原则 2.4 不能保留/插入多个关联实体



人们你能帮我吗? 我在教义 2.4 上持久化实体(Solicitud)时遇到麻烦,当 IM 将对象传递给实体管理器进行持久化和刷新时,该实体的所有相关对象都已存储在其属性中。

完全描述的关系应该是...

  1. Usuario-> Solicitudes(一对多)
  2. Solicitud-> Servicios (多对多)
  3. 类别->服务(一对多)

我确实从我的 XML 映射中生成了实体和数据库结构......

在我的"solicitud"XML上,我已经定义了关系,如下所示...当我执行命令时,中间表得到了有效的创建:

vendorbindoctrine orm:schema-tool:update --force

有两列,一切都好。

这是在定义多对多关系的字段上的"Solicitud">

<many-to-many field="servicios" target-entity="Servicio">
<cascade>
<cascade-persist/>
</cascade>
<join-table name="solicitud_servicio">
<join-columns>
<join-column name="id_solicitud" referenced-column-name="id"/>
</join-columns>
<inverse-join-columns>
<join-column name="id_servicio" referenced-column-name="id"/>
</inverse-join-columns>
</join-table>

而这个在服务上

<doctrine-mapping xmlns="http://doctrine-project.org..." 
xmlns:xsi="http://www.w3.org/2001/XMLS..." 
xsi:schemalocation="http://doctrine-project.org... http://doctrine-
project.org...">
<entity name="Servicio" table="servicio">
<indexes>
<index name="id_categoria" columns="id_categoria"/>
</indexes>
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="codigo" type="string" column="codigo" length="15" 
nullable="false"/>
<field name="nombre" type="string" column="nombre" length="70" 
nullable="false"/>
<field name="detalle" type="string" column="detalle" length="250" 
nullable="false"/>
<field name="precioPublico" type="integer" column="precio_publico" 
nullable="false"/>
<field name="precioPrivado" type="integer" column="precio_privado" 
nullable="false"/>
<field name="medida" type="string" column="medida" length="15" 
nullable="false"/>
<field name="planificacion" type="integer" column="planificacion" 
nullable="false"/>
<field name="fabricacion" type="integer" column="fabricacion" 
nullable="false"/>
<field name="fechaCreacion" type="datetime" column="fecha_creacion" 
nullable="true"/>
<field name="ultimaModificacion" type="datetime" column="ultima_modificacion" 
nullable="false"/>
<many-to-one field="idCategoria" target-entity="Categoria">
<join-columns>
<join-column name="id_categoria" referenced-column-name="id"/>
</join-columns>
</many-to-one>
</entity>
</doctrine-mapping>

我错过了什么吗? 也许另一个句子呼吁坚持另一个涉及/相关的类?

我试图通过实体经理句子中的"solicitud"实体坚持......我将它们作为DAO在不同的层上分开,但这没有问题,当数据到达这一层时,我尝试了var_dump,一切都很好。

在我的"SolicitudService"中,我编写了这个函数来收集持久所需的所有相关对象,我使用带有内部函数的 VO(虚拟对象)来构建从对象到 Json 或从 Json 到对象和实体模式(从 VO 到所需类的对象)的数据。

我在这个函数中做什么:

首先,我在
  1. 尝试后找到我在 SolicitudServicioVO 上拥有的对象以下三句话,然后我创建新对象 Solicitud。
  2. 然后,我将通过DAO实体建立的对象设置为"Solicitud"新对象("//set Solicitud Properties"注释后的5行)。
  3. 之后,我通过listadoServicio(来自前端的服务列表)迭代($solicitudServicioVO->listadoServicio),并为每个服务创建VO,这些VO能够在下一句中将自己构建为实体"或所需类型的类",在这种情况下,我在这个问题开头的关系定义中提到的那些。
  4. 我送去坚持,关于这句话

    $this->solicitudDAO->create($solicitud);
    

这导致 DAO 有...

function create($solicitud){
$this->em->persist($solicitud);
$this->em->flush();
return $solicitud;
}

这个函数可以更描述它的名字,也许可以称它为 createSolicitud 作为它的目的是创建一个新的,但没关系......它还必须添加与用户相关的 Solicitud。所以我坚持,现在没关系。

function addSolicitud($solicitudServicioVO){
try{
$usuario = $this->usuarioDAO->find($solicitudServicioVO->getIdUsuario()->id);
$direccion = $this->direccionDAO->find($solicitudServicioVO-
>getIdDireccion()->id);
$facturacion = $this->datoFacturacionDAO->find($solicitudServicioVO-
>getIdFacturacion()->id);
$solicitud = new Solicitud();
//set Solicitud Properties
$solicitud->setFechaCreacion(new DateTime());
$solicitud->setEstado("solicitado");
$solicitud->setIdFacturacion($facturacion);
$solicitud->setIdDireccion($direccion);
$solicitud->setIdUsuario($usuario);
foreach($solicitudServicioVO->listadoServicio as $servicioVO)
{ 
$categoriaServicioVO = $this->categoriaService->getCategoria($servicioVO-
>getCategoriaId());
$servicio = $servicioVO->toEntity();
$servicio->setIdCategoria($categoriaServicioVO->toEntity());
$solicitud->addServicio($servicio);
}
$resp = $this->solicitudDAO->create($solicitud);
$response = Utils::getInstancia()->httpResponseSuccess('001');
$response['data'] = $resp;
return $response; 
}
catch(Exception $e){
$response = $e->getMessage();
}
}

所以,我是我能做的最具描述性的,我无法持久化关于SOlicitud实体的数据......是否有人已经有类似的问题或任何额外的想法......也许在XML定义中?,它确实将实体作为注释,似乎没有任何问题。

请帮助我,这么多到很多癌症已经花了将近一周的时间没有解决 jajaja

更新故障状态 jaja:

"通过关系'Solicitud#servicios'找到一个新实体,该实体未配置为级联实体:Servicio@00000000044008fd0000000000ad7488的持久操作。要解决此问题:在此未知实体上显式调用 EntityManager#persist(),或者配置级联在映射中保留此关联,例如 @ManyToOne(..,cascade={\"persist\"})。如果无法找出导致问题的实体,请实施

实际上,实体 Solicitud 是一个新的,因为我试图创建它,创建这个新 Solicitud 的数据是从前端发送的,我已经从数据库中提取了服务......我想我即将解决它。

您如何看待这个循环...,我是否应该为每个服务要求查找,因为我有各自的ID?

foreach($solicitudServicioVO->listadoServicio as $servicioVO)
{   
$categoria = $this->categoriaDAO->find($servicioVO->getCategoriaId());
//maybe change this next line
$servicio = $servicioVO->toEntity(); 
// for this next one?? ill try and tell you...
$servicio = $this->servicioDAO->find($servicioVO->getId());
$servicio->setIdCategoria($categoria);
$solicitud->getServicios();
$solicitud->addServicio($servicio);
}

正如我在这里显示的,我在$servicioVO中有服务数据,也许我可以在数据库中找到它,在同一个循环上。

当 im 做 Solicitud-> addServicio($servicio) 时,我添加了一个 fron-end sendsend服务(servicio),它可以在 toEntity 模式函数上将自己构造为 Servicio 类的对象。

所以,我知道 Doctrine 可能会抱怨,因为它认为添加到 arrayCollection 的服务与它已经从其数据库中知道的服务不同,即使我使用各自的 ID 设置它们,也会导致错误消息更多地指导我验证服务实体......比新的Solicitud。

。我确实以这种方式解决了这个问题,我现在正在测试它,它工作正常,所以......如果别人掉进这个坑里,不要相信你自己构造的对象Jajaja,教义想要他们的方式,你应该服从,做找或找,得到正确的对象,而不是构建它们,即使里面的数据是一样的,持久函数也搞糊涂了,这种情况最好使用直接查询的DB对象。

最新更新