php - How to use variable from mySQL DB in Doctrine? -


i have problem doctrine. task set new product category other table. category name unique. category , product tables in onetomany relation. when make :

        $brand = new category();         $brand->setbrand('beers');          $beer = new product();         $beer->setmodel('bavaria');         $beer->setbrand($brand);         $em = $this->getdoctrine()->getmanager();         $em->persist($brand);         $em->persist($beer);         $em->flush(); 

it's fine. when make query other model name (p.ex. heineken) have duplicated brand name. want have 1 brand name wine, vodka, beers. don't want create each time brand name if exists when make query. answers.

if want let user choose amongst categories use form entitytype.

if want let user type name , create category if doesn't exist :

$newbrand = 'userinput'; $em       = $this->getdoctrine()->getmanager(); $category = $em->getrepository('youbundle:youentity')->findoneby(array('brand' => $newbrand)); if ($category == null) {     $category = new category();     $category->setbrand($newbrand);     $em->persist($category); }  $beer = new product(); $beer->setmodel('bavaria'); $beer->setbrand($category); // if need use $category in view don't forget $category->addproduct($beer); $em->persist($beer); $em->flush(); 

ps : mysql has create or update directive wouldn't advise here since need match fields.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -