K
K
Konstantin Zhikhor2020-07-13 15:06:22
symfony
Konstantin Zhikhor, 2020-07-13 15:06:22

How to save checkbox list in Symfony?

Hello, please help me with the task.
The essence of the problem:
There is an entity CompanyService in it two columns company_id and service_id.
And there is the Service entity, services are stored in it.
I need to fill the CompanyService entity with what lies in the Service And there is the Company entity in which the CompanyService CompanyService
collection is stored

<?php

namespace App\Entity;

use App\Repository\CompanyServiceRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=CompanyServiceRepository::class)
 */
class CompanyService
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="companyServices")
     */
    private $Company;

    /**
     * @ORM\ManyToOne(targetEntity=Service::class, inversedBy="companyServices")
     */
    private $service;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getCompany(): ?Company
    {
        return $this->Company;
    }

    public function setCompany(?Company $Company): self
    {
        $this->Company = $Company;

        return $this;
    }

    public function getService(): ?Service
    {
        return $this->service;
    }

    public function setService(?Service $service): self
    {
        $this->service = $service;

        return $this;
    }
}


Company.php
/**
     * @ORM\OneToMany(targetEntity=CompanyService::class, mappedBy="Company", orphanRemoval=true,cascade={"persist"})
     */
    private $companyServices;

public function __construct()
    {
        $this->companyServices = new ArrayCollection();
    }

/**
     * @return Collection|CompanyService[]
     */
    public function getCompanyServices(): Collection
    {
        return $this->companyServices;
    }

    public function addCompanyService(CompanyService $companyService): self
    {
        if (!$this->companyServices->contains($companyService)) {
            $this->companyServices[] = $companyService;
            $companyService->setCompany($this);
        }

        return $this;
    }

public function removeCompanyService(CompanyService $companyService): self
    {
        if ($this->companyServices->contains($companyService)) {
            $this->companyServices->removeElement($companyService);
            // set the owning side to null (unless already changed)
            if ($companyService->getCompany() === $this) {
                $companyService->setCompany(null);
            }
        }

        return $this;
    }


CompanyType.php
->add('companyServices', EntityType::class,[
                'class'=>Service::class,
                'attr'=>['class'=>'company_service form-control'],
                'query_builder'=>function(ServiceRepository $sr){
                    return $sr->findOwnerServices();
                },
                'choice_label' => 'service',
                'multiple' => true,
                'expanded' => true,
                'by_reference' => false,
            ])


Please tell me what is the problem?
The essence of the task is this: When creating a company, there is a checkbox list that is selected from Service and added to CompanyService

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question