我刚开始学习Laravel 4,遇到了一个小问题,我很好奇如果有解决方案。我有一个多语种的食谱数据库和我有,总之,仿照我的数据库是这样的:用多个外键插入记录到表格中
Schema::create('language', function(Blueprint $table) {
$table->string('id', 2)->primary();
$table->string('display_name');
$table->timestamps();
});
Schema::create('recipe', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug')->unique();
$table->timestamps();
});
Schema::create('translated_recipe', function(Blueprint $table) {
$table->increments('id');
$table->integer('recipe_id')->unsigned();
$table->foreign('recipe_id')->references('id')->on('recipe')->onDelete('cascade');
$table->string('language_id', 2);
$table->foreign('language_id')->references('id')->on('language');
$table->string('translated_name')
$table->timestamps();
});
我的模式是这样的:
class Language extends Eloquent {
protected $table = 'language';
public function translatedRecipes() {
return $this->hasMany('TranslatedRecipe');
}
}
class Recipe extends Eloquent {
protected $table = 'recipe';
public function translations() {
return $this->hasMany('TranslatedRecipe');
}
}
class TranslatedRecipe extends Eloquent {
protected $table = 'translated_recipe';
public function recipe() {
return $this->belongsTo('Recipe');
}
}
然后,我有一些基本的接种:将
Language::create(array('id' => 'en', 'display_name' => 'English'));
Recipe::create(array('name' => 'First Recipe', 'slug' => 'first_recipe'));
这里来了棘手的部分。如何在翻译中插入翻译过的食谱?我已经走到这一步:
$recipe = Recipe::all()->first();
$lang = Language::all()->first();
$translatedRecipe = new TranslatedRecipe;
$translatedRecipe->translated_name = $lang->display_name.' '.$recipe->name;
我可以很容易做到与查询生成器的INSERT:
DB::table('translated_recipe')->insert(
array(
'recipe_id' => $recipe->id,
'language_id' => $lang->id,
'translated_name' => $translatedRecipe->translated_name
)
);
它不设置时间戳,但因为我猜,雄辩的特点。有没有办法让它保持100%的口才?我不能这样做:
$translatedRecipe = $recipe->translations()->save($translatedRecipe);
...因为SQL由于缺少第二个外键而抛出“完整性约束违规:1452 ...”错误。
感谢您的列命名和IDS的建议!有一件事我不明白。我对数据库建模相当陌生,所以请耐心等待。为什么我需要额外的桌子和多对多的关系?一个食谱可以有许多个翻译食谱,一个翻译食谱属于一个食谱。一个translated_recipe只能属于一种语言,一种语言可以有很多translation_recipes。还是我错过了一些基本的东西? – bjoupp
你是完全正确的! (我会编辑我的答案)。你其实不需要第四张桌子。 – kJamesy