doulei8475 2016-01-27 04:38
浏览 154
已采纳

使用Laravel Doctrine的InvalidFieldNameException

I'm converting my Zend Framework 1 app into a Laravel App. I was using Doctrine so I moved the models to Laravel. I am using laravel-doctrine

The fields are written in camelCase and when the database was created through doctrine, they were also created in camelCase, like this:

/**
 * @ORM\Entity (repositoryClass="Repositories\Customer")
 * @ORM\Table(name="customers")
 * @ORM\HasLifecycleCallbacks
 */
class Customer
{
    /**
     * @ORM\Id @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** @ORM\Column(type="string", length=255, nullable=true) */
    protected $firstName;

    /** @ORM\Column(type="string", length=255, nullable=true) */
    protected $lastName;

    /** @ORM\Column(type="string", length=255, nullable=true) */
    protected $companyName;

When I do EntityManager::find('App\Entities\Customer', 1); I get this error:

An exception occurred while executing 'SELECT t0.id AS id_1, t0.firstName AS firstName_2, t0.lastName AS lastName_3, t0.company_name AS company_name_4, t0.email AS email_5, t0.phone_number AS phone_number_6, t0.type AS type_7, t0.created AS created_8, t0.updated AS updated_9, t0.jobsource_id AS jobsource_id_10 FROM customers t0 WHERE t0.id = ?' with params 1: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.company_name' in 'field list'

Is there a reason why is looking for underscore? Is there a way for me to change that?

I am using PHP 7.0.1

  • 写回答

2条回答 默认 最新

  • doumindang2416 2016-01-27 16:46
    关注

    laraveldoctrine.org supports defining custom naming strategies:

    Doctrine\ORM\Mapping\DefaultNamingStrategy Is probably the one you are looking for.

    In doctrine.php config file: (after you have published it to your project)

    'managers'                  => [
            'default' => [
                'naming_strategy' => 'Doctrine\ORM\Mapping\DefaultNamingStrategy',
                'dev'        => env('APP_DEBUG'),
                'meta'       => env('DOCTRINE_METADATA', 'annotations'),
                'connection' => env('DB_CONNECTION', 'mysql'),
                'paths'      => [
                    base_path('app')
                ],
               ....
          ]
    ]
    

    If you want a custom one, implement the naming strategy according to: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/namingstrategy.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥18 模拟电路问题解答有偿
  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题