首页 > 解决方案 > Symfony - 用另一个实体的 id 重载实体集函数

问题描述

根据另一个实体的字段重载一个实体的集合函数是否可行且可行。

我在表 1(E 列)中有一个字段,它将由 C 列或 D 列组成(它搞砸了 2 个字段具有相同的字段,但它会在另一个时间,我只是试图使这个工作)。字段 E 将取决于另一个表 (2)。

示例:如果在表 2 中,字段“crit1”或“crit2”是“localisation”,在表 1 中,“tri_id”列(F 列)将取 D 列的值,否则取 E 列的值。

这些表的结构示例

=> adapte_gaucher 的意思是“左撇子适应”

Excel 上的表格示例

我在我的实体中这样做:

/**
 * @param int $triId
 */
public function setTriId(int $shopId, int $triId): void
{
    $entityManager = $this->getDoctrine()->getManager();
    $products = $this->getDoctrine()
        ->getRepository(Table1::class)
        ->find($shopId)
 
    if(Table1->crit1 != "localisation" || Table1->crit2 != "localisation" ) {
        $triId = $this->tri21 ;
}   else {
    $triId = $this->tri12 ;
}
    $this->triId = $triId;
    $entityManager->persist($triId);
    $entityManager->flush();
}

这会是一个好的解决方案吗?我太新了,不知道这是否是一个好方法,或者我是否不应该这样做。这是我的实习项目(我只是一名大三学生,在我身后只进行了一年的编程,没有任何框架课程)。对不起,“悲怆”的台词,但它真的会帮助我成功。

不管怎么说,还是要谢谢你。

标签: phpsymfonyentitysymfony4

解决方案


尽管“IF”的想法并不明智,但我想尝试一个简单的连接,但是:

public function setTri12()
{
    $this->tri12 = $shop_id . $pack_id . $adapte_gaucher_id;
    return $this;
}

或者 :

public function setTri12()
{
    $this->tri12 = $this->shop_id . $this->pack_id . $this->adapte_gaucher_id;
    return $this;
}

两者都返回一个空值,因为这 3 个值都是空的。

我可以强迫它吗?谢谢


推荐阅读