0

我在 Laravel 4 中建立了以下多对多关系:

class SampleType extends Eloquent {

  public function intake() {
    return $this->belongsToMany('Intake', 'tbl_samples', 'Sample Type', 'Animal ID');
  }

}

但是,当我回显时SampleType::find(2)->intake;,由于“样本类型”和“动物 ID”中的空格,我收到以下 MySQL 错误。

SQLSTATE [42S22]:未找到列:1054“字段列表”中的未知列“tbl_samples.Animal”(SQL:选择. * tbl_intake,. tbl_samplesas Sampleas.as from inner join on . = . where . =?)(绑定:数组( 0 => 2, ))tbl_samplesAnimalastbl_intaketbl_samplestbl_intakeAnimal IDtbl_samplesAnimal IDtbl_samplesSample Type

奇怪的是,这些键在 WHERE 和 ON 部分工作正常,但在 SELECT 部分中断,它们似乎触发了 AS 别名。

如果我用下划线替换空格,它可以正常工作,但我更喜欢使用空格,因为列名会在前端回显。有什么方法可以保持我的空间完整,还是我需要在模型中使用下划线并稍后将它们切换出来?提前感谢您的任何提示。

4

1 回答 1

0

I talked to Taylor about it. He made a good point in that having column names with spaces doesn't work with Eloquent, since you wouldn't be able to access the field on the Model via $result->field. No spaces for me after all! Will switch to underscores and switch them out before I echo the column titles to the front end.

于 2013-10-03T21:02:42.187 回答