symfony - Symfony2 Doctrine 无法识别的字段:

标签 symfony doctrine field

我创建了一个实体来存储一些我通过api拉取的数据

当我尝试使用findByOne查看条目是否已存在时,出现错误,提示无法识别该字段。

我已经完成了一个PHP应用程序/控制台学说:schema:update --force
我可以在mySQL管理器中看到具有正确字段名'StadiumID'的表
那为什么我做findByOne();时找不到教义
我的实体类:

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


我正在使用的代码添加数据(如果不存在)和更新数据(如果存在):

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

最佳答案

该属性名称实际上是:

private $stadiumID;


所以你应该做:

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


因此更改此行:

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


对此:

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


Doctrine的主要作用是将您的应用程序层(实体)连接到数据库层(表)。因此,学说试图将来自应用程序层的请求转换为数据库。即使您在数据库"StadiumID""table_name_snake_case"中为字段命名,当您调用findOneBy(array($property => $value))时,主义也希望$property引用属性名,并将其转换为SQL到'SELECT FROM .... where StadiumID = :value'之类的东西。

关于symfony - Symfony2 Doctrine 无法识别的字段:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29563846/

相关文章:

Symfony2 : How to filter the options of an entity-choice form field by a certain attribute?

symfony - 错误 : Serialization for the format json is not supported

php - json_array 中的 Doctrine 搜索

php - Symfony ORM PostRemove 软删除

python - 使用 protobuf,如何从已解析的对象中删除字段?

c# - 我可以使用反射更改 C# 中的私有(private)只读字段吗?

php - 使用关系数据库构建受 GitHub 启发的时间线的设计模式?

php - Doctrine 2.0 准备好使用了吗?

symfony - 映射异常: Class does not exist Symfony2 Deployment

c# - 如何获得一个 System.Collections.Generic.List<FieldInfo> ,它包含类型 T 对象的所有 FieldInfo,直到类层次结构中的对象?