2
votes

I am quite new to Doctrine and need help in one situation. I have 2 simple tables Albums & Genre. Please check the structures below:

Albums

id | title   | genre_id | createdon
1  | Album 1 | 1        | 21/05/2015
2  | Album 2 | 2        | 21/05/2015

Genre

id | genre   | active
1  | Pop     | 1
2  | Blue    | 1
3  | Rock    | 1
4  | Country | 1

genre_id from Albums table is mapped to id column of Genre table.

In Albums entity I have mapped this by the below process:

/**
 * @ORM\OneToOne(targetEntity="Genre", mappedBy="albums")
 * @ORM\JoinColumn(name="genre_id", referencedColumnName="id")
 **/
private $genre;

public function getGenre(){
    return $this->genre;    
}

public function setGenre($value)
{
    $this->genre = $value;
    return $this;
}

I can fetch genre name flawlessly by getGenre() method but the problem is while inserting value into the database. I need to insert genre_id which is an integer value but after calling setGenre() method from controller I am getting the following error:

Expected value of type "Album\Entity\Genre" for association field "Album\Entity\Albums#$genre", got "string" instead.

I have also tried by specifying another private variable like the original column name i.e genre_id like below:

/** @ORM\Column(type="integer") */
protected $genre_id;

public function setGenre($value)
{
   $this->genre_id = $value;
   return $this;
}

But then I am getting the following error:

An exception occurred while executing 'INSERT INTO Albums (title, createdon, genre_id) VALUES (?, ?, ?)' with params ["nfhgfh", "21/05/2015", null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'genre_id' cannot be null

I am calling the entity from controller like this:

$albums = new Albums();
$albums->setTitle($postData['title']);
$albums->setGenre($postData['genre_id']);
$albums->setCreatedon(date('d/m/Y'));
$this->em()->persist($albums);

$this->em()->flush();

I am badly stuck in this problem and need your help. I am not having any clue how its getting a null value instead. Please help.

2
Could you please join a var_dump of $postData['genre_id'] ? As The first error message you got states, you have to give a "Genre" type and not a string you got from the $postData variable. Try querying the "Genre" using the "genre_id" and then call the setter using the "Genre" you obtained from the "genre_id" query.Answers_Seeker
please find the var_dump of my postdata: array(4) { ["doadd"]=> string(3) "yes" ["title"]=> string(6) "fghhfg" ["genre_id"]=> string(1) "1" ["btnsubmit"]=> string(3) "Add" }Subhasis Laha
Also can you please elaborate?Subhasis Laha
As you can see in the dump, the 'genre_id' you were giving to the setter in your version with the 'genre' attribute in your entity is a string. Use the FindById function to retrieve the 'Genre' you'll then give to your setter.Answers_Seeker
I doubt you really want a OneToOne association. This would mean each Genre can only be used once. Normally the would me OneToMany, with Album as the owning (child) entity.dualmon

2 Answers

1
votes

Querying Genre by id worked for me. Please find my updated code below in controller:

$albums = new Albums();
$albums->setTitle($postData['title']);
$albums->setCreatedon(date('d/m/Y'));

$genre = $this->em()->find('Album\Entity\Genre', $postData['genre_id']);

$albums->setGenre($genre);
$this->em()->persist($albums);

$this->em()->flush();

Is this the process you specified? Thanks for the help.

1
votes

The problem is that you need to set genre in Album to be an instance of Album\Entity\Genre.

For example:

$genre = new Genre();
$genre->setId($postData['genre_id']);

$albums = new Albums();
$albums->setTitle($postData['title']);
$albums->setGenre($genre);
$albums->setCreatedon(date('d/m/Y'));

$this->em()->persist($albums);
$this->em()->flush();

EDIT

To fix cascading errors you need to add a cascade parameter to your mapping.

/**
 * @ORM\OneToOne(targetEntity="Genre", mappedBy="albums", cascade={"persist"})
 * @ORM\JoinColumn(name="genre_id", referencedColumnName="id")
 **/
private $genre;

You may also need to add a similar thing to the $album variable in your Genre entity, I can't remember exactly which way round it needs to be.