I created Entity to store some data that I pull through api
When I try to find ByOne to find out if a record exists, I get an error when the field is not recognized.
I made the doctrine php app / console: schema: update --force I see a table in my mySQL manager with the correct field name 'StadiumID' So, why the doctrine can not find it when I do findByOne(); My Entity Class:
<?php namespace FantasyPro\DataBundle\Entity; use Doctrine\ORM\Mapping as ORM; class Stadium { private $id; private $stadiumID; private $name; private $city; private $state; private $country; private $capacity; private $playingSurface; public function getId() { return $this->id; } public function setStadiumID($stadiumID) { $this->stadiumID = $stadiumID; return $this; } public function getStadiumID() { return $this->stadiumID; } public function setName($name) { $this->name = $name; return $this; } public function getName() { return $this->name; } public function setCity($city) { $this->city = $city; return $this; } public function getCity() { return $this->city; } public function setState($state) { $this->state = $state; return $this; } public function getState() { return $this->state; } public function setCountry($country) { $this->country = $country; return $this; } public function getCountry() { return $this->country; } public function setCapacity($capacity) { $this->capacity = $capacity; return $this; } public function getCapacity() { return $this->capacity; } public function setPlayingSurface($playingSurface) { $this->playingSurface = $playingSurface; return $this; } public function getPlayingSurface() { return $this->playingSurface; } }
The code I use to add data if it does not exist and update it if it exists:
<?php namespace FantasyPro\DataBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use FantasyPro\DataBundle\Entity\Stadium; class DefaultController extends Controller { public function indexAction($name) { return $this->render('DataBundle:Default:index.html.twig', array('name' => $name)); } public function updateStadiumAction(){ //get list of stadiums $client = $this->container->get('fantasyapi'); $stadiumData = $client->Stadiums(); //var_dump($stadiumData); //get the entity manager $em = $this->getDoctrine()->getManager(); $repo = $em->getRepository('DataBundle:Stadium'); $log = $stadiumData; foreach($stadiumData as $stadium){ // Get the current stadium in the list $criteria = array("StadiumID" =>$stadium['StadiumID']); var_dump($criteria); $storedStadium = $repo->FindOneBy($criteria); if (!$storedStadium) { /* throw $this->createNotFoundException( 'No product found for id '.$stadium['StadiumID'] );*/ //no stadium exists with the StadiumID passed //create a new entry $entry = new Stadium(); $entry->setStadiumID($stadium['StadiumID']); $entry->setName($stadium['Name']); $entry->setCity($stadium['City']); $entry->setState($stadium['State']); $entry->setCountry($stadium['Country']); $entry->setCapacity($stadium['Capacity']); $entry->setPlayingSurface($stadium['PlayingSurface']); $em->persist($entry); $log .= 'Added New Stadium: '.$stadium['StadiumID'].' : '.$stadium['Name'].'<br>'; }else{ $storedStadium->setStadiumID($stadium['StadiumID']); $storedStadium->setName($stadium['Name']); $storedStadium->setCity($stadium['City']); $storedStadium->setState($stadium['State']); $storedStadium->setCountry($stadium['Country']); $storedStadium->setCapacity($stadium['Capacity']); $storedStadium->setPlayingSurface($stadium['PlayingSurface']); $em->persist($storedStadium); $log .= 'Updated Stadium: '.$stadium['StadiumID'].' : '.$stadium['Name'].'<br>'; } } //$em->flush(); return $this->render('DataBundle:Default:index.html.twig', array('log' => $log)); } }
source share