在外键返回时传递参数的Symfony2始终为null

时间:2016-08-12 13:13:02

标签: php symfony doctrine-orm

如何在外键上传递参数

Class Product{

/**
 * @var int
 *
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var Group
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Group")
 * @ORM\JoinColumn(name="group_id", referencedColumnName="id", onDelete="CASCADE")
 */
private $group;

 /**
 * @var string
 *
 * @ORM\Column(name="alt_number", type="string", length=255)
 */
private $altproducts;

 /**
 * @var string
 *
 * @ORM\Column(name="product", type="string", length=255)
 */
private $product;
}

ProductRepo:

public function GetProducts($product, Group $group){
       $results = $this->createQueryBuilder('p')
        ->where('p.product= :product')
        ->andwhere('p.group= :group')
        ->setParameter('product',$product)
        ->setParameter('group',$group)
        ->getQuery()->getResult();
        return $results;
 }

在服务中:

// Param $group is an Object of Group Class
 $ProductList= $this->entityManager->getRepository(Product::class)->GetProducts($product,$group); 

总是空的;

当我->getSQL()时,我有正确的SQL语法SELECT i0_.id AS id_0, i0_.product AS product,i0_.description AS description_2, i0_.group_id AS group_id_3, i0_.altproducts AS altproducts_4, FROM Product i0_ WHERE i0_.product = ? AND i0_.group_id = ?

我需要根据产品编号和groupID获取altProducts数组。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果您尝试获取特定组的所有产品,则不应将特定产品实体作为参数传递:

public function GetProducts(Group $group)
{
       $results = $this->createQueryBuilder('p')
        ->where('p.group= :group')
        ->setParameter('group',$group)
        ->getQuery()->getResult();

        return $results;
 }