Nicolas Castro
Nicolas Castro

Reputation: 11

Doctrine2: Getting error with the association between these entities. What is wrong?

I have a problem, with Symfony 2.8. I have been trying to solve this form a week now. I do not know what am I doing wrong.

I have two entities, vectors and vectorData. For each record in vectors there is at least 6 records in vectorData. Foreing keys are set well in the DB (MySQL).

Relevant part of my Vectors.php entity:

/**
 * @ORM\OneToMany(targetEntity="VectorData", mappedBy="vector")
 */
private $vectorData;

public function __construct() {
    $this->vectorData = new ArrayCollection();
}

public function getVectorData()
{
    return $this->vectorData;
}

public function setVectorData(ArrayCollection $vectorData)
{
    $this->vectorData = $vectorData;
}

And relevant part of my VectorData.php entity:

/**
 * @ORM\ManyToOne(targetEntity="Vector", inversedBy="vectorData")
 */
protected $vector;

As far as I know, there is no need for setter or getter on the ManyToOne side. I have no interest in having reverse mapping.

The error I am getting is:

The following exception is thrown during the rendering of a template:

[Semantical Error] line 0, col 61 near 'vd WHERE v.id=:idVectorAND': Error: Class AppBundle\Entity\Vectors has no association named vectorData

Any idea of what could be wrong?

Upvotes: 0

Views: 46

Answers (3)

Wilt
Wilt

Reputation: 44326

First: @Zaltex his answer is totally correct. Your setter is wrong. Check in the Doctrine2 documentation here on how to manage your associations

Second: You use targetEntity="Vector" while your entity is named AppBundle\Entity\Vectors.

If this is another typo then you should more carefully check your question before posting. People are trying to help you but in fact wasting their time staring at an incomplete and unverifiable example.

Read the How to ask and How to create a Minimal, Complete, and Verifiable example for more details.


Note: If you are not interested in "reversed mapping" in other words not interested in a bidirectional relationship you should consider changing it to a unidirectional relationship. Simply omitting the setters and getters is not the way to go.

Upvotes: 0

zalex
zalex

Reputation: 799

At least your setter is wrong.

public function setVectorData(ArrayCollection $vectorData)
{
    $this->vectorData = $vectorData;
}

This is correct:

public function addVectorData(VectorData $vectorData)
{
    $this->vectorData->add($vectorData);
}

As far as I know, there is no need for setter or getter on the ManyToOne side. I have no interest in having reverse mapping.

It is doubtful

Upvotes: 1

Alvin Bunk
Alvin Bunk

Reputation: 7764

Can you change your Entity VectorData to add the JoinColumn, you need that:

/**
 * @ORM\ManyToOne(targetEntity="Vector", inversedBy="vectorData")
 * @ORM\JoinColumn(name="vectorData_id", referencedColumnName="id")
 */
protected $vector;

I don't know what the name of the Id column is; in the above example I use id but you might have something different.

Can you try that. I think it might work. Unless there's a different error.

Upvotes: 0

Related Questions