Symfony中的多对一关系2

时间:2015-05-13 15:44:30

标签: symfony doctrine-orm mapping schema many-to-one

我遇到与Doctrine2相关的问题:

1-我有两个表加入多对一关系:

表1 - 活动

架构:

Backend\adminBundle\Entity\Activity:
type: entity
table: activity
indexes:
    result_id:
        columns:
            - result_id
id:
    id:
        type: integer
        nullable: false
        unsigned: false
        comment: ''
        id: true
        generator:
            strategy: IDENTITY
fields:
    ......
manyToOne:
    result:
        targetEntity: Actionresult
        cascade: {  }
        mappedBy: null
        inversedBy: null
        joinColumns:
            result_id:
                referencedColumnName: id
        orphanRemoval: false

实体

<?php

namespace Backend\adminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

class Activity {
/**
 * @var \Backend\adminBundle\Entity\Actionresult
 *
 * @ORM\ManyToOne(targetEntity="Backend\adminBundle\Entity\Actionresult")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="result_id", referencedColumnName="id")
 * })
 */

private $result;

/**
 * @var \Backend\adminBundle\Entity\SfGuardUser
 *
 * @ORM\ManyToOne(targetEntity="Backend\adminBundle\Entity\SfGuardUser")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 * })
 */

/* There are other Properties */



/**
 * Set result
 *
 * @param \Backend\adminBundle\Entity\Actionresult $result
 * @return Activity
 */
public function setResult(\Backend\adminBundle\Entity\Actionresult $result = null)
{
    $this->result = $result;

    return $this;
}

/**
 * Get result
 *
 * @return \Backend\adminBundle\Entity\Actionresult 
 */
public function getResult()
{
    return $this->result;
}

}

表2 - 与Id相关的活动表的 Actionresult

架构:

Backend\adminBundle\Entity\Actionresult:
type: entity
table: actionresult
id:
    id:
        type: integer
        nullable: false
        unsigned: false
        comment: ''
        id: true
        generator:
            strategy: IDENTITY
fields:
    name:
        type: string
        nullable: false
        length: 255
        fixed: false
        comment: ''
lifecycleCallbacks: {  }

实体:

<?php

namespace Backend\adminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    private $name;

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

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

        return $this;
    }

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

问题

有了学说,我可以从表活动转到 Actionresult ,名称为结果

我怎样才能将表格 Actionresult 中的学说引用到活动 ??

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

要彻底,您应该尽可能坚持使用Symfony中的一种实体映射。如果使用YAML配置,@ ORM *注释是多余的,反之亦然。我将使用YAML提供答案,我相信如果需要,您将能够转换为注释。

# Activity.yml

Activity:
    type: entity
    ...
    manyToOne:
        result:
            targetEntity: ActionResult
            inversedBy: activities

# ActionResult.yml

Result:
    type: entity
    oneToMany:
        activities:
            targetEntity: Activity
            mappedBy: result


# ActionResult.php

class Result {
  protected $activities;

  public function __construct()
  {
    $this->activities = new Doctrine\Common\Collections\ArrayCollection();
  }

  public function getActivities()
  {
    return $this->activities;
  }

  public function addActivity(Activity $activity)
  {
    $activity->setResult($this);
    $this->activities->add($activity);
  }

  public function removeActivity(Activity $activity)
  {
    $activity->setResult(null);
    $this->activities->removeElement($activity);
  }
}

# Activity.php
class Activity {
  protected $result;

  public function getResult()
  {
    return $this->result;
  }

  public function setResult(ActionResult $result = null)
  {
    $this->result = $result;
  }
}

<强>参考

双向一对多:http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-bidirectional