简体   繁体   中英

Symfony - Find an entity object with relation many to many

I'm making a form that searches users by three parameters:

-Category -Subcategories (multiple choice) -Province

The user entity has a relation manyToOne with Categories, and ManyToMany with the other parameters. If I want to find only by categorie, I suppose that I have to use this code:

$users = $em->getRepository('CASUsuariosBundle:Artist')->findByCategory($category);

But how can I do it with the other parameters? Especially with subcategories, which is a multiple selection.

User entity:

/**
 * @ORM\ManyToOne(targetEntity="\CAS\EventBundle\Entity\Category", inversedBy="artists")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;

/**
 * @ORM\ManyToMany(targetEntity="\CAS\EventBundle\Entity\Subcategory", inversedBy="artists")
 * @ORM\JoinTable(name="artists_subcategories",
 *      joinColumns={@ORM\JoinColumn(name="artist_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="subcategory_id", referencedColumnName="id")}
 *      )
*/
private $subcategories;

/**
 * @ORM\ManyToMany(targetEntity="\CAS\EventBundle\Entity\Province", inversedBy="artists")
 * @ORM\JoinTable(name="artists_provinces",
 *      joinColumns={@ORM\JoinColumn(name="artist_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="province_id", referencedColumnName="id")}
 *      )
 */
 protected $provinces;

SearchController:

public function searchAction(Request $request) {
    $search = new Search();
    $em = $this->getDoctrine()->getManager();
    $form = $this->createForm(new SearchType($this->getDoctrine()->getManager()), $search);
    $form->setData($search);

    $form->handleRequest($request);

    if ($form->isValid() || $form->isSubmitted()) {
        $category = $form->get('category')->getData();
        $subcategories = $form->get('subcategories')->getData();
        $province = $form->get('province')->getData();

        return $this->showAction($category, $subcategories, $province);
    }

    return $this->render('CASUsuariosBundle:Profile:search.html.twig', array(
        'form' => $form->createView()
    ));
}

public function showAction($category, $subcategories, $province) {
    return $this->render('CASUsuariosBundle:Profile:search_results.html.twig');
}

You should be able to do

$users = $em->getRepository('CASUsuariosBundle:Artist')->findBy([
    'category' => $categories,
    'province' => $province,
    //...
]);

If you want even more control you can always create a QueryBuilder which will allow you to forge your own doctrine request:

From the symfony docs:

$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
    'SELECT p
    FROM AppBundle:Product p
    WHERE p.price > :price
    ORDER BY p.price ASC'
)->setParameter('price', 19.99);

$products = $query->getResult();

For reference: http://symfony.com/doc/current/doctrine.html#querying-for-objects-with-dql http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM