1

I am working on some new project and the project is nearly done using Symfony framework, but the problem that i am used to CodeIgnitor Framework and basically as a Java developer/Android a lot of stuff i got confused with when working on Web development so here is the situation: The website have a user end and an admin end (i am working on the Admin end), so there are these tables in the database which i really don't understand why they are built like this but this is not the problem  the tables in doctrine database

what i would like to know is how to add a service_category field with the corresponding translations in the service_category_translation using forms or any other way

this is the ServiceCategory Entity

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use JMS\Serializer\Annotation as Serializer;

/**
 * class ServiceCategory
 *
 * @ORM\Table(name="service_category")
 * @ORM\Entity
 *
 * @Serializer\ExclusionPolicy("all")
 */
class ServiceCategory
{

    use ORMBehaviors\Timestampable\Timestampable;
    use ORMBehaviors\SoftDeletable\SoftDeletable;
    use ORMBehaviors\Translatable\Translatable;

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @Serializer\Expose
     * @Serializer\Groups({"general-information", "service-offer"})
     */
    private $id;

    /**
     * @var ServiceGroup
     *
     * @ORM\OneToMany(targetEntity="ServiceGroup", mappedBy="serviceCategory")
     *
     * @Serializer\Expose
     * @Serializer\Groups({"service-offer"})
     */
    private $serviceGroup;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->serviceGroup = new ArrayCollection();
    }

    /**
     * {@inheritdoc}
     */
    public function __toString()
    {
        return $this->getName() ? $this->getName() : '';
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get translated name
     *
     * @return string
     *
     * @Serializer\VirtualProperty
     * @Serializer\SerializedName("name")
     * @Serializer\Groups({"invoice-list", "service-offer"})
     */
    public function getName()
    {
        if($this->getTranslations()->get($this->getCurrentLocale()) == null){
            return 'sorry';
        }
        return $this->getTranslations()->get($this->getCurrentLocale())->getName();
    }

    /**
     * Add serviceGroup
     *
     * @param ServiceGroup $serviceGroup
     *
     * @return ServiceCategory
     */
    public function addServiceGroup(ServiceGroup $serviceGroup)
    {
        $this->serviceGroup[] = $serviceGroup;

        return $this;
    }

    /**
     * Remove serviceGroup
     *
     * @param ServiceGroup $serviceGroup
     */
    public function removeServiceGroup(ServiceGroup $serviceGroup)
    {
        $this->serviceGroup->removeElement($serviceGroup);
    }

    /**
     * Get serviceGroup
     *
     * @return Collection
     */
    public function getServiceGroup()
    {
        return $this->serviceGroup;
    }
}

and this is the ServiceCategoryTranslation Entity

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * Class ServiceCategoryTranslation
 *
 * @package CoreBundle\Entity
 *
 * @ORM\Entity
 * @ORM\Table(name="service_category_translation")
 */
class ServiceCategoryTranslation
{
    use ORMBehaviors\Translatable\Translation;
    use ORMBehaviors\Timestampable\Timestampable;
    use ORMBehaviors\SoftDeletable\SoftDeletable;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $name;

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param  string
     * @return null
     */
    public function setName($name)
    {
        $this->name = $name;
    }
    public function __toString() {
        return $this->name;
    }
}

how can i achieve this ? please don't guide me to symfony or doctrine documentation i have been lost there for two days now and i am running late on the schedule

Thanks in advance

6
  • You should check symfony and doctrine documentation: symfony.com/doc/current/book/doctrine.html Commented Jun 3, 2016 at 16:23
  • i am not sure if you read what i just wrote but even though all the documentations out there don't answer the question btw i took a look here so don't forward me to more documentations please github.com/Atlantic18/DoctrineExtensions/blob/master/doc/… and yet its not answering how to fill this tables all the documentations are around getting data from DB or setting data to one table with setters but if you take a look at the code i have none of that Commented Jun 3, 2016 at 19:41
  • it does answer your question, part Relationship Mapping Metadata: symfony.com/doc/current/book/…, just replace entity "Product" in documentation with your "ServiceCategoryTranslation" and "Category" in documentation with your "ServiceCategory", of course first you need to add column category_id in table service_category_translation Commented Jun 3, 2016 at 21:33
  • What is the reference field in service_category_translations? Is it missing? is it translatable_id? Is it an M to N Relationship? Commented Jun 3, 2016 at 22:59
  • @AndreasDyballa from the dialog it says its translatable_id linked to id in service category but in the class its not specified Commented Jun 4, 2016 at 13:21

1 Answer 1

1

You have a one-to-many-association from ServiceCategory (1) to ServiceCategoryTranslations (many) since I assume you will manage the transaltions from the category. This have to be a bidirectional association, have a look here You have to add a property to manage the entities and describe the association. I will do it with annotations.

use Doctrine\Common\Collections\ArrayCollection;

class ServiceCategory
{
    /**
     * @OneToMany(targetEntity="ServiceCategoryTranslation", mappedBy="serviceCategory")
     **/
    private $translations;

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

    /**
     * @return ServiceCategoryTranslation[]
     */
    public function getStandort(){
        return $this->translations;
    }

    /**
     * @param ArrayCollection $translations
     * @return ServiceCategory
     */
    public function setTranslations(ArrayCollection $translations)
    {
        $this->translations->clear();
        foreach ($translations as $translation){
            $this->addTranslation($translation);
        }
        return $this;
    }

    /**
     * @param ServiceCategoryTranslation $translation
     * @return ServiceCategory
     */
    public function addTranslation(ServiceCategoryTranslation $translation){
        /* this is a way to keep the integerity */
        $translation->setServiceCategory($this);
        if(!$this->translation){
            $this->translations = new ArrayCollection();
        }
        $this->translations->add($translation);
        return $this;
    }

    /**
     * @param ServiceCategoryTranslation $translation
     * @return ServiceCategory
     */
    public function removeStandort(ServiceCategoryTranslation $translation){
        $this->translations->removeElement($translation);
        return $this;
    }
}    


class ServiceCategoryTranslation
{
    /**
     * @ManyToOne(targetEntity="ServiceCategory", inversedBy="translations")
     * @JoinColumn(name="translatable_id", referencedColumnName="id")
     **/
    private $serviceCategory;

    /**
     * @param ServiceCategoryTranslation $translation
     * @return ServiceCategoryTranslation
     */
    public function setServiceCategory(ServiceCategory $serviceCategory){
        $this->serviceCategory = $serviceCategory;
        return $this;
    }

    /* getter analog */
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.