0

I tried to not repeat myself so I had checked so many references but none of them touch my situation. I have three tables with columns below:

location T1         location_amenity T2        location_location_amenity T3

id  |  name        id  | name       id |  location_id  | location_amenity_id 
1   |  loc1        1   | amm1       1  |  loc1         |  amm1
2   |  loc2        2   | amm2       2  |  loc1         |  amm2
                                    3  |  loc2         |  amm1
                                    4  |  loc2         |  amm2

Here i am trying to save data from T1 and T2 in T3. Like in above table structure. First I have to list all the data of T2 will display in loop of checkbox. So in entity i have done :

/** Location Entity
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="LocationLocationAmenity", mappedBy="location", cascade={"remove"})
*/
private $amenities;

/**  Getter and setter
 *
 * Get location amenities
 *
 * @return ArrayCollection
 */
public function getAmenities() {
    return $this->amenities;
}

/**
 * Set amenities
 *
 * @param array $amenities
 * @return Location
 */
public function setAmenities(array $amenities) {
    $this->amenities = new ArrayCollection($amenities);
    return $this;
}
                

/** Location Amenity Entity
 * @var ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="LocationLocationAmenity", mappedBy="locationLocationAmenity", cascade={"remove"})
 */
private $locationAmenities;


/** Getter and Setter
 * Get locationAmenities
 *
 * @return ArrayCollection
 */
public function getLocationAmenities() {
    return $this->locationAmenities;
}

/**
 * Set locationAmenities
 *
 * @param array $locationAmenities
 * @return LocationAmenity
 */
public function setLocationAmenities(array $locationAmenities) {
    $this->locationAmenities = new ArrayCollection($locationAmenities);
    return $this;
}

/** Location Location Amenity
 * @var \AppBundle\Entity\Location
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Location", inversedBy="amenities")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="location_id", referencedColumnName="id")
 * })
 */
protected $location;

/**
 * @var \AppBundle\Entity\LocationAmenity
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\LocationAmenity", inversedBy="locationAmenities")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="location_amenity_id", referencedColumnName="id")
 * })
 */
private $amenities;

 
/** Getter and Setter
 * Get location
 *
 * @return Location
 */
public function getLocation(){
    return $this->location;
}

/**
 * Get location
 *
 * @param Location $location
 * @return $this
 */
public function setLocation(Location $location){
    $this->location = $location;
    return $this;
}

/**
 * Get amenities
 *
 * @return LocationAmenity
 */
public function getAmenities(){
    return $this->amenities;
}

/**
 * Get amenities
 *
 * @param LocationAmenity $amenities
 * @return $this
 */
public function setAmenities(LocationAmenity $amenities){
    $this->amenities = $amenities;
    return $this;
}

I have done these steps to mapping the tables but i didn't get the list of amenities in my twig template form. Here is my Controller code:

$em = $this->getDoctrine()->getManager();
    /* @var $location Location */
    $location = $em->getRepository('AppBundle:Location')->find($id);
 $locationAmenities = $location->getAmenities();
 return $this->render('location/locationAmenity.html.twig', array(
'locationAmenities' => $locationAmenities));

Here is my locationform.php

 public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('name')
->add('amenities', 'checkbox');
}

Here is my code for locationAmenity.html.twig

<div class="form-group">
                {{ form_label(form.amenities, '', {'label_attr': {'class': 'col-md-4 control-label amenities', 'data-location-id': location.id}}) }}
                <div class="col-md-8">
                    {{ form_widget(form.amenities, {'attr': {'class': 'form-control'}}) }}
                    {{ form_errors(form.amenities) }}
                </div>
            </div>       

Please guide/suggest me where ever I am wrong to get the list of all amenities in loop of check-boxes so that i can further submit those amenities which are checked true for the particular location in location location amenity table (t3).

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Suhail Saud Khan
  • 83
  • 1
  • 1
  • 12
  • You should use a ManyToMany relationship between amenities & location. It would create & handle on it's own the third table. – Aurelien Apr 30 '18 at 06:29
  • Do I need to use ManyToMany relationship in "Location Location Amenity" entity or the other entity (Location entity) or (Location Amenity enity). Please clerify. – Suhail Saud Khan Apr 30 '18 at 09:11
  • You won't need such entity, let doctrine handle it for you. See Many-to-Many bidirectional here : https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#many-to-many-bidirectional – Aurelien Apr 30 '18 at 09:14
  • A little 'symfony many to many' search will do the rest, for more info & examples. – Aurelien Apr 30 '18 at 09:23
  • But here three tables involve. T3 handle the record for T1 and T2. All the examples available are of two tables. – Suhail Saud Khan Apr 30 '18 at 09:38
  • Yes, because doctrine creates the third one ( the one associating your amenities & location entities ) without you having to code it manually. Test it, you will see it being created on doctrine update. – Aurelien Apr 30 '18 at 09:40
  • Did you mean that , when I will create ManyToMany relation between Location and LocationAmenity entity then it will auto create relation with LocationLocationAmenity entity? – Suhail Saud Khan Apr 30 '18 at 09:45
  • It will create a relation between Location entity & Amenity entity, thus creating the association table (LocationAmenity), yes, as soon as you will do a doctrine:schema:update. Follow online tutorials, you can even find ones with form usage examples I believe. – Aurelien Apr 30 '18 at 09:50
  • ok i will do the same as you guide. Thanks. – Suhail Saud Khan Apr 30 '18 at 10:26
  • When i got the list of all amenities, then how i will show these records in form of checkbox. So that on check any amenity i will save a new record in LocationLocationAmenity with the selected amenity for the respected location. Please guide – Suhail Saud Khan Apr 30 '18 at 11:33

0 Answers0