Mathias Lund
Mathias Lund

Reputation: 772

Join table in Symfony 2

I just started object-oriented programming in Symfony 2, and I have a problem joining mysql tables.

I have two tables:

kommuner: id,name,capacity

Activities: id,name,kommune_id

How do I join these tables in the entity files?
This is my function in DefaultController:

public function aktiviteterAction()
{

    $em = $this->get('doctrine.orm.default_entity_manager');
    $aktiviteter = $em->getRepository(Aktiviteter::class)->findAll();

    return $this->render('default/aktiviteter.html.twig', [
        'aktiviteter' => $aktiviteter,
    ]);

}

and this is my entity, Kommune.php:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Kommune
 */
class Kommune
{
 /**
 * @var integer
 */
private $id;

/**
 * @var string
 */
private $name;

/**
 * @var integer
 */
private $capacity;


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

/**
 * Set name
 *
 * @param string $name
 * @return Kommune
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

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

/**
 * Set capacity
 *
 * @param integer $capacity
 * @return Kommune
 */
public function setCapacity($capacity)
{
    $this->capacity = $capacity;

    return $this;
}

/**
 * Get capacity
 *
 * @return integer 
 */
public function getCapacity()
{
    return $this->capacity;
}
}

and this is my Aktiviteter.php entity:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Aktiviteter
 */
class Aktiviteter
{
/**
 * @var integer
 */
private $id;

/**
 * @var string
 */
private $name;

/**
 * @var integer
 */
private $kommuneId;


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

/**
 * Set name
 *
 * @param string $name
 * @return Aktiviteter
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

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

/**
 * Set kommuneId
 *
 * @param integer $kommuneId
 * @return Aktiviteter
 */
public function setKommuneId($kommuneId)
{
    $this->kommuneId = $kommuneId;

    return $this;
}

/**
 * Get kommuneId
 *
 * @return integer 
 */
public function getKommuneId()
{
    return $this->kommuneId;
}



/**
 * @ManyToOne(targetEntity="Kommune")
 * @JoinColumn(name="kommune_id", referencedColumnName="id")
 **/
private $kommune;

}

Can someone please explain how these table joins work in Symfony 2? I appreciate all kinds of help!

Upvotes: 0

Views: 403

Answers (2)

gp_sflover
gp_sflover

Reputation: 3500

You have to choose the right Doctrine Association Mapping depending by the relationship you want between these entities.

Other references useful to read for you:

Update based on comments and post edit:

A OneToMany - ManyToOne relation could be Unidirectional:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Aktiviteter
 */
class Aktiviteter
{
/**
 * @var integer
 */
private $id;

/**
 * @var string
 */
private $name;    

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

/**
 * Set name
 *
 * @param string $name
 * @return Aktiviteter
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

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

/**
 * @ManyToMany(targetEntity="Kommune")
 * @JoinTable(name="kommunes")
 **/
private $kommunes;

}

Or Bidirectional:

Like did by @Isa Bek answer

NOTE: When mapping bidirectional associations it is important to understand the concept of the owning and inverse sides and remember that you don't need to set the entity id of the relationship because Doctrine handle this automatically as you will see after the mapping will applied with the command: $ php app/console doctrine:schema:update --force from your console.

Upvotes: 1

Isabek Tashiev
Isabek Tashiev

Reputation: 1044

I suppose, you want to use one to many relationship between Kommune and Aktiviteler entities. In Kommune.php entity:

/**
 * Kommune
 *
 * @ORM\Table()
 * @ORM\Entity
 */
 class Kommune
 {
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @ORM\OneToMany(targetEntity="Aktiviteter", mappedBy="kommune")
     */
    private $aktiviteler;

   /**
    * Other fields
    */
}

In Aktiviteter.php entity

/**
 * Aktiviteter
 *
 * @ORM\Table()
 * @ORM\Entity
 */

class Aktiviteter
{
   /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
   private $id;

   /**
    * @ORM\ManyToOne(targetEntity="Kommune", inversedBy="aktiviteler")
    * @ORM\JoinColumn(name="kommune_id", referencedColumnName="id")
    */
   private $kommune;

   /**
    * Other fields
   */
 } 

I think, these codes would be useful for you.

Upvotes: 0

Related Questions