2017-10-09 30 views
0

有一种方法来连接symfony中的findBy操作的两个结果?如果产品是由用户创建的,我试图将用户对象连接到产品阵列。我的代码:从getRepository连接结果symfony

public function indexAction() 
{ 
    $products = $this->getDoctrine() 
     ->getRepository('AppBundle:Product') 
     ->findById($this->getUser()->getId()); 

    if ($products[0]->getCreatedByUser() == 1){ 
     $userDetails = $this->getDoctrine()->getRepository('AppBundle:User') 
     ->findOneById($product->getUserId()); 

    //here somehow to concatenate the value from $user to the $products array 
    } 

    return $this->json($products, Response::HTTP_OK, [], [ 
     'groups' => ['products'] 
    ]); 
} 
+0

添加样本输入和输出。 – svgrafov

+0

我不认为' - > findOneById($ id)'(除非您以不同的方式实现它,它与' - > find($ id)''相同)做你认为它的作用。 – ccKep

+0

不,不是...... – IleNea

回答

0

您可以创建一个对象,该对象持有指向$ user和$ products数组的值的指针。或者,您可以使用(value,key)创建一个数组来映射依赖关系。

但是,你为什么要在控制器级别上做到这一点?

创建数据库(onetoOne)关系不是更好吗?通过这种方式,您可以查询产品,并且它也会加载userDetails。然后,您可以在twigview中访问您的userDetails,例如:{{ products.product.userDetails.username }},而无需在您的控制器中使用任何业务逻辑。