【问题标题】:Symfony2 Doctrine Unrecognized field:Symfony2 Doctrine Unrecognized 字段:
【发布时间】:2015-06-16 07:33:42
【问题描述】:

我创建了一个实体来存储我通过 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));
    }
}

【问题讨论】:

    标签: symfony doctrine field


    【解决方案1】:

    属性名其实是:

    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 =&gt; $value)) 时,原则仍期望$property 引用属性名称,并且它将其转换为SQL 为类似'SELECT FROM .... where StadiumID = :value'

    【讨论】:

    • 感谢您的出色回答,列名是否应使用任何特定的命名约定,保持列名与驼峰式中的属性名称相同是个好主意吗?
    • 非常高兴!如果您不提及列名,则学说会自动将您的属性名称(如果是驼峰式)转换为蛇形。我发现在使用 sql 数据库时这一点非常清楚,并且在某种程度上是一种最佳实践,因为它提供了一致性。但这一切都取决于业务逻辑,以及谁将处理数据库。还有一些特征,例如 Blameable 以驼峰形式命名列(createdBy、updatedBy)。您还可以为您的实体配置学说的命名策略:coderwall.com/p/kj1reg/…
    【解决方案2】:

    对于仍有问题的其他人,
    确保您在调用函数findOneBy(...) 时输入了符号“=&gt;”而不是“,
    所以不是:

    findOneBy(["code" , "Code-Example"]);
    

    这样做:

    findOneBy(["code" => "Code-Example"]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 2014-04-04
      • 1970-01-01
      • 2014-01-05
      • 2014-10-16
      相关资源
      最近更新 更多