Symfony 2中的联接表 [英] Join table in Symfony 2

查看:92
本文介绍了Symfony 2中的联接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在Symfony 2中开始了面向对象的编程,但是在连接mysql表时遇到了问题.

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

我有两个表:

kommuner : ID,名称,容量

kommuner: id,name,capacity

活动: id,name,kommune_id

Activities: id,name,kommune_id

如何在实体文件中联接这些表?
这是我在DefaultController中的功能:

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,
    ]);

}

这是我的实体Kommune.php:

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;
}
}

这是我的Aktiviteter.php实体:

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;

}

有人可以解释一下这些表联接在Symfony 2中是如何工作的吗?我感谢各种帮助!

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

推荐答案

您必须选择正确的

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:

  • entity-relationships-associations from the Symfony's Book.
  • Then Recipes about Doctrine from the Symfony's Cookbook.

更新基于评论和帖子

一个OneToMany-ManyToOne关系可以是

<?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;

}

双向:

就像 @Isa Bek 回答的那样

注意:双向映射时关联,了解拥有和反面的概念很重要,并记住您不需要设置关系的实体ID,因为Doctrine会自动处理此问题,因为您将在映射与命令:$ php app/console doctrine:schema:update --force从您的控制台.

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.

这篇关于Symfony 2中的联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆