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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?