K
K
Konstantin Zhikhor2019-07-03 09:45:54
symfony
Konstantin Zhikhor, 2019-07-03 09:45:54

How can I make checkbox fields in symfony 4?

Hello, please help me to make checkbox fields in symfony 4?
The essence of the problem.
I have checkboxes with products that can be ticked
5d1c4e11373ba999735240.jpeg
. These products are tied to a company in a many-to-many relationship. everything would seem fine, well, apply the Entity type and there are no problems, but I also have the fields Connect by default and Forced connection which are stored in the same table as the id of the company and products (the problem is that the table already exists and cannot be changed. in this is the whole complexity.) Please tell me what can be done?
Maybe this will help?
this is a link to AdditionalCompany.php in entity Company.php

/**
     * @ORM\OneToMany(targetEntity="App\Entity\AdditionalCompany", mappedBy="company", cascade={"persist"})
     */
    private $additionalCompanies;
    public function __construct()
    {
        $this->additionalCompanies = new ArrayCollection();
    }
/**
     * @return Collection|AdditionalCompany[]
     */
    public function getAdditionalCompanies(): Collection
    {
        return $this->additionalCompanies;
    }

    public function addAdditionalCompany(AdditionalCompany $additionalCompany): self
    {
        if (!$this->additionalCompanies->contains($additionalCompany)) {
            $this->additionalCompanies[] = $additionalCompany;
            $additionalCompany->setCompany($this);
        }

        return $this;
    }

    public function removeAdditionalCompany(AdditionalCompany $additionalCompany): self
    {
        if ($this->additionalCompanies->contains($additionalCompany)) {
            $this->additionalCompanies->removeElement($additionalCompany);
            // set the owning side to null (unless already changed)
            if ($additionalCompany->getCompany() === $this) {
                $additionalCompany->setCompany(null);
            }
        }

        return $this;
    }

This is the very essence of the Additional company

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\AdditionalCompaniesRepository")
 * @ORM\Table(name="additional_companies", uniqueConstraints={
 *      @ORM\UniqueConstraint(name="id", columns={"idcompany", "idproduct"})
 * })
 */
class AdditionalCompany
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\CompanyCard", inversedBy="additionalCompanies")
     * @ORM\JoinColumn(name="idcompany", referencedColumnName="idcomp_card")
     */
    private $company;
    
    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $is_default;

    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $is_forced;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\AdditionalProduct", inversedBy="additionalCompanies")
     */
    private $product;

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

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

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

        return $this;
    }

    public function getIsDefault(): ?bool
    {
        return $this->is_default;
    }

    public function setIsDefault(?bool $is_default): self
    {
        $this->is_default = $is_default;

        return $this;
    }

    public function getIsForced(): ?bool
    {
        return $this->is_forced;
    }

    public function setIsForced(?bool $is_forced): self
    {
        $this->is_forced = $is_forced;

        return $this;
    }

    public function getProduct(): ?AdditionalProduct
    {
        return $this->product;
    }

    public function setProduct(?AdditionalProduct $product): self
    {
        $this->product = $product;

        return $this;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kafkiansky, 2019-07-03
@mad_maximus

Мапить форму можно не только на сущность, но и на собственные структуры данных. Другими словами:

$resolver->setDefaults([
            'data_class' => // ссылка на класс
        ]);

Это вам поможет построить ту структуру так, как вам удобно.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question