0

I have a problem related to Doctrine2:

1- I have two tables joining on a many-to-one relation:

Table 1 - Activity

The Schema:

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 

The Entity

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

}

Table 2 - Actionresult Related to Activity Table by Id:

The schema:

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: { } 

The Entity:

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

The Question:

With doctrine i can refer from table Activity to Actionresult with the name result.

How can i refer with doctrine from table Actionresult to Activity??

Thank you in advance.

1
  • 1
    Look at bi-directional relations. Basically add an activities property along with the mapping. Commented May 13, 2015 at 17:52

1 Answer 1

1

To be thorough, you should try and stick to one type of entity mapping in Symfony whenever possible. The @ORM* annotations are redundant if you use YAML config, and vice-versa. I'll provide the answer using YAML, and I believe you'll be able to convert to annotations if need be.

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

Reference:

Bidirectional one to many: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-bidirectional

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, within a bundle, it's not SHOULD stick to one mapping but rather MUST. The yaml files will override the annotations which will lead to confusion down the line. @Frank Bardon Jr, your answer reverses the direction of the relation. Should be Activity ManyToOne ActionResult
@Cerad : I've reversed the association for you, and removed references to one to one associations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.