2
votes

I'm trying to create association from my entity 'Entry' to entity FosUserUser. I created table Entry in database and relation to fos_user_user and then doctrine generated entities by: php app/console doctrine:mapping:import AcmeDemoBundle annotation php app/console doctrine:generate:entities AcmeDemoBundle

Generated entity in shortcut:

/**
 * Entries
 *
 * @ORM\Table(name="entry", indexes={@ORM\Index(name="user_id", columns={"user_id"})})
 * @ORM\Entity
 */
class Entry {
 .............

 /**
 * @var \FosUserUser
 *
 * @ORM\ManyToOne(targetEntity="FosUserUser")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 * })
 */
private $user;

/**
 * Set user
 *
 * @param \Acme\DemoBundle\Entity\FosUseUser $user
 */
public function setUser(\Acme\DemoBundle\Entity\FosUseUser $user = null)
{
    $this->user = $user;
    return $this;
}
}

Doctrine generated entity with setUser(\Acme\DemoBundle\Entity\Entry $user = null) and i changed it to setUser(\Acme\DemoBundle\Entity\FosUseUser $user = null).

Now Im trying to create object Entry public function addEntryAction() {

$em = $this->getDoctrine()->getManager();
$user = $this->getDoctrine()->getRepository('AcmeDemoBundle:FosUserUser')->find($this->getUser()->getId());

$new_entry = new Entry;
$new_entry->setEntry('test');
$new_entry->setUser($user);

$em->persist($new_entry);
$em->flush(); 

End the problem is that Symfony doesnt match my entities and respond error Found entity of type Acme\DemoBundle\Entity\FosUserUser on association Acme\DemoBundle\Entity\Entry#user, but expecting Acme\DemoBundle\Entity\Entry

Is it association created corectly or where is the problem?

1
Somewhere in the code you did not copy here, there's a method who's variable declaration you did not change from Acme\DemoBundle\Entity\Entry to Acme\DemoBundle\Entity\FosUserUser .Noy
is \Acme\DemoBundle\Entity\FosUseUser class correct? You're talking about some FosUserUser class but type hinting as FosUseUser class in setUser method..xurshid29
is it not enought to declare in class Entry? /** * @var \FosUserUser * * @ORM\ManyToOne(targetEntity="FosUserUser") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="user_id", referencedColumnName="id") * }) */ private $user;Demoniczna Kaczka Zaglady
@xurshid29 FosUserUser class pastebin.com/5JWRbSQEDemoniczna Kaczka Zaglady

1 Answers

0
votes

Ok, i resolved it. Is another doctrine config file src/Acme/DemoBundle/Resources/config/doctrine/Entry.orm.xml with relation:

<many-to-one field="user" target-entity="Entry"> //should be target-entity="FosUserUser"
  <join-columns>
    <join-column name="user_id" referenced-column-name="id"/>
  </join-columns>
</many-to-one>