2015-04-10 27 views
5

Utworzona jednostka do przechowywania danych Jestem ciągnąc za pośrednictwem APISymfony2 Doktryna Nierozpoznany pola:

Kiedy próbuję findByOne aby sprawdzić, czy wpis już istnieje, pojawia się błąd mówiący pole jest nie rozpoznany.

Zrobiłem php app/doktrynę konsoli: schema: update --force mogę patrz tabela w moim menedżerem mySQL z prawej nazwy pola „StadiumID” Więc dlaczego nie mogę go znaleźć, gdy doktryna zrobić findByOne(); Moja Entity Class:

<?php 

namespace FantasyPro\DataBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Stadium 
* 
* @ORM\Table("fp_stadium") 
* @ORM\Entity(repositoryClass="FantasyPro\DataBundle\Entity\StadiumRepository") 
*/ 
class Stadium 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="StadiumID", type="integer", length=2, nullable=false, unique=true) 
    */ 
    private $stadiumID; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="Name", type="string", length=100, nullable=false) 
    */ 
    private $name; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="City", type="string", length=50, nullable=false) 
    */ 
    private $city; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="State", type="string", length=10, nullable=true) 
    */ 
    private $state; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="Country", type="string", length=2, nullable=false) 
    */ 
    private $country; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="Capacity", type="integer", length=32, nullable=true) 
    */ 
    private $capacity; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="PlayingSurface", type="string", length=50, nullable=true) 
    */ 
    private $playingSurface; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set stadiumID 
    * 
    * @param integer $stadiumID 
    * @return Stadium 
    */ 
    public function setStadiumID($stadiumID) 
    { 
     $this->stadiumID = $stadiumID; 

     return $this; 
    } 

    /** 
    * Get stadiumID 
    * 
    * @return integer 
    */ 
    public function getStadiumID() 
    { 
     return $this->stadiumID; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return Stadium 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set city 
    * 
    * @param string $city 
    * @return Stadium 
    */ 
    public function setCity($city) 
    { 
     $this->city = $city; 

     return $this; 
    } 

    /** 
    * Get city 
    * 
    * @return string 
    */ 
    public function getCity() 
    { 
     return $this->city; 
    } 

    /** 
    * Set state 
    * 
    * @param string $state 
    * @return Stadium 
    */ 
    public function setState($state) 
    { 
     $this->state = $state; 

     return $this; 
    } 

    /** 
    * Get state 
    * 
    * @return string 
    */ 
    public function getState() 
    { 
     return $this->state; 
    } 

    /** 
    * Set country 
    * 
    * @param string $country 
    * @return Stadium 
    */ 
    public function setCountry($country) 
    { 
     $this->country = $country; 

     return $this; 
    } 

    /** 
    * Get country 
    * 
    * @return string 
    */ 
    public function getCountry() 
    { 
     return $this->country; 
    } 

    /** 
    * Set capacity 
    * 
    * @param integer $capacity 
    * @return Stadium 
    */ 
    public function setCapacity($capacity) 
    { 
     $this->capacity = $capacity; 

     return $this; 
    } 

    /** 
    * Get capacity 
    * 
    * @return integer 
    */ 
    public function getCapacity() 
    { 
     return $this->capacity; 
    } 

    /** 
    * Set playingSurface 
    * 
    * @param string $playingSurface 
    * @return Stadium 
    */ 
    public function setPlayingSurface($playingSurface) 
    { 
     $this->playingSurface = $playingSurface; 

     return $this; 
    } 

    /** 
    * Get playingSurface 
    * 
    * @return string 
    */ 
    public function getPlayingSurface() 
    { 
     return $this->playingSurface; 
    } 
} 

Kod używam aby dodać dane, jeśli nie istnieje i zaktualizować go, jeśli istnieje:

<?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)); 
    } 
} 

Odpowiedz

22

Nazwa nieruchomość jest faktycznie:

private $stadiumID; 

więc należy zrobić:

$repo->findOneBy(array('stadiumID' => $value)); 

więc zmienić ten rząd:

$criteria = array("StadiumID" =>$stadium['StadiumID']); 

do tego: Główną rolę

$criteria = array("stadiumID" =>$stadium['StadiumID']); 

doktryny jest podłączenie do warstwy aplikacji (entites) do warstwy bazy danych (tabele). Tak więc doktryna próbuje przetłumaczyć żądanie przychodzące z warstwy aplikacji do bazy danych. Nawet jeśli nazywa swoje pole w bazie "StadiumID" lub "table_name_snake_case", gdy dzwonisz findOneBy(array($property => $value)), doktryna spodziewa $property odnosi się do nazwy właściwości i będzie tłumaczyć to do SQL coś jak 'SELECT FROM .... where StadiumID = :value'

+0

dzięki za wielką odpowiedzi, są tam wszelkie konwencje nazewnictwa, które powinny być używane w nazwach kolumn, czy dobrze jest zachować nazwę kolumny tak samo jak nazwa właściwości w liście kamer? –

+1

z wielką przyjemnością! Jeśli nie podasz nazwy kolumny, doktryna automatycznie przeniesie nazwę twojej własności (jeśli jest w kamelce) do sprawy węża. Uważam, że jest to bardzo jasne, gdy używam bazy danych SQL i jest to najlepsza praktyka, ponieważ zapewnia spójność. Wszystko zależy jednak od logiki biznesowej i od tego, kto będzie obsługiwał bazę danych. Również niektóre cechy, takie jak Blameable, nazywają kolumny w przypadku wielbłąda (createdBy, updatedBy). Możesz również skonfigurować strategię nazewnictwa dla doktryny dla swoich podmiotów: https://coderwall.com/p/kj1reg/implementing-a-doctrine2-naming-strategy-in-symfony2 – anegrea

Powiązane problemy