symfony - Persist association symfony2 -


i started using doctrine2 , associations don't understand aspects.

i've 2 php class :

class user extends baseuser {          protected $id;      protected $team;  /**  * id  *  * @return integer $id  */ public function getid() {     return $this->id; }  public function setid($id) {     $this->id = $id; }  public function getteam() {     return $this->team; }  public function setteam($team) {     $this->team = $team; }   } 

and

/**  * team  *  * @orm\table(name="basket__team")  * @orm\entity(repositoryclass="tperroin\basketbundle\entity\teamrepository")  */ class team { /**  * @var integer  *  * @orm\column(name="id", type="integer")  * @orm\id  * @orm\generatedvalue(strategy="auto")  */ private $id;  /**  * @var string  *  * @orm\column(name="categorie", type="string", length=255)  */ private $categorie;  /**  * @var string  *  * @orm\manytomany(targetentity="application\sonata\userbundle\entity\user", inversedby="users", cascade={"persist"})  * @orm\jointable(name="basket__team_user_coach")  */ private $coachs;  /**  * @var string  *  * @orm\manytomany(targetentity="application\sonata\userbundle\entity\user", inversedby="users", cascade={"persist"})  * @orm\jointable(name="basket__team_user_player")  */ private $players;  /**  * @var string  *  * @orm\column(name="level", type="string", length=255)  */ private $level; 

my problem when user create new profile association not created.

so i've function in user class postpersist:

public function addteam(lifecycleeventargs $args) {       $entity = $args->getentity();      $em = $args->getentitymanager();      $player = $this->getteam();      $player->setplayers(array($entity));      $em->persist($player);     $em->flush(); } 

it works 1 user, function generate delete query before insert one.

is there solution kind of problem ?

thank you

there errors in orm annotation : both $coach , $players have targetentity of user , same inveredby attribute. inversedby should attribute of user class other side of many many association, exemple repectively $coachedteams , $playinteams.

your addteam code remove previous associations because call $player->setplayers(array($entity)): replace old collection of associations new one. should call $player->addplayer($entity) instead, add new object existing collection.

if generated entities php app/console doctrine:generate:entities, should not have method setplayers() addplayer() , removeplayer().

please note association doctrine stored doctrine\common\collections\collection , not plain array.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -