2014-03-19 41 views
0

我目前正在laravel 4中的web應用程序中工作。應用程序的本質是將表單數據存儲在數據庫中。雄辯地存儲表單數據

由於表單輸入框中的名稱不同,我的主要問題是將不同的值存儲在例程表中,因爲我只有一個值字段並將該值與通過id的指定任務一起鏈接。我還沒有找到在例程中有效存儲每場一行的方法。

數據庫看起來像這樣(忽略的Arduino和Endringslogg):

Database model diagram

HverTimeController.php:

class HverTimeController extends BaseController 

{ 
    public $restful = true; 

    public function getHvertime() 
    { 
     return View::make('hvertime') 
     ->with('title', 'Hvert Time'); 
    } 

    public function postInsert() 
    { 


     //insert logic 

     return Redirect::route('hvertime') 
     ->with('message', 'Lagret i databasen!'); 


    } 
} 

routes.php文件

Route::get('bassengweb/hvertime', array('as' => 'hvertime', 'uses' => [email protected]')); 
Route::post('bassengweb/insertHT', array('uses' => '[email protected]')); 

雄辯模型 (即使在圖中的名字上述m與在挪威,我已經因爲它們改變爲英語在數據庫中作爲在下面的代碼)

Emp.php(Ansatt在圖)

class Emp extends Eloquent 
{ 
    protected $table = 'emps'; 
    protected $fillable = array(
     'user_name', 'first_name', 
     'last_name', 'email', 
     'password', 'user_type'); 

    public function routines() 
    { 
     $this->hasMany('Routine', 'emp_id', 'id'); 
    } 
} 

Routine.php(Rutiner在圖看到的)

class Routine extends Eloquent 
{ 
    protected $table = 'routines'; 
    protected $fillable = array('date', 'time', 'value', 'emp_id'); 

    public function emps() 
    { 
     $this->belongsTo('Emp', 'emp_id', 'id'); 
    } 

    public function tasks() 
    { 
     $this->belongsToMany('Task', 'task_routine', 'routine_id', 'task_id'); 
    } 

    public function measurements() 
    { 
     $this->belongsToMany('Measurement', 'measure_routine', 'routine_id', 'measure_id'); 
    } 
} 

Measurement.php(圖中沒有什麼上班表)

class Measurement extends Eloquent 
{ 
    protected $table = 'measurements'; 
    protected $fillable = array('title'); 

    public function routines() 
    { 
     $this->belongsToMany('Routine', 'measure_routine'); 
    } 
} 

Task.php(Oppgaverた在圖BLE)

class Task extends Eloquent 
{ 
    protected $table = 'tasks'; 
    protected $fillable = array('title'); 

    public function routines() 
    { 
     $this->belongsToMany('Routine', 'task_routine', 'task_id', 'routine_id'); 
    } 
} 

hvertime.blade.php

@extends('layouts.default') 

@section('content') 

    <h1>HVER TIME</h1> 

    @if($errors->has()) 
     <ul> 
      {{ $errors->first('badendeTime', '<li>:message</li>') }} 
      {{ $errors->first('temp', '<li>:message</li>') }} 
     </ul> 
    @endif 

    {{ Form::open(array('url' => 'bassengweb/insertHT', 'method' => 'POST')) }} 
     <table> 
      <tr> 
       <td>{{ Form::label('badendeTime', 'Badende per Time:') }}</td> 
       <td>{{ Form::text('badendeTime', Input::old('badendeTime')) }}</td> 
      </tr> 

      <tr> 
       <td>{{ Form::label('temp', 'Temperatur:') }}</td> 
       <td>{{ Form::text('temp', Input::old('temp')) }}</td> 
      </tr> 

      <tr> 
       <td><hr/></td> 
       <td>{{ Form::submit('Lagre') }}</td> 
      </tr> 
     </table> 
    {{ Form::close() }} 
@stop 

任何幫助非常感謝,謝謝!

回答

0

分析這些關係

Oppgaver X OppgaverRutiner X Rutiner

Oppgaver (hasMany) OppgaverRutiner and 
Rutiner (hasMany) OppgaverRutiner too 

所以逆

OppgaverRutiner (belongsTo) Oppgaver 
OppgaverRutiner (belongsTo) Rutiner 

Ansatt X Rutiner

Ansatt (hasMany) Rutiner 

所以逆

Rutiner (belongsTo) Ansatt 

什麼上班X MalingerRutiner X Rutiner

在表MalingerRutiner缺少外鍵表Rutiner !!!!!

Malinger (hasMany) MalingerRutiner and 
Rutiner (hasMany) MalingerRutiner too 

MalingerRutiner (belongsTo) Malinger 
MalingerRutiner (belongsTo) Rutiner 

Basseng X MalingerRutiner

Basseng (hasMany) MalingerRutiner 

MalingerRutiner (belongsTo) Basseng 

你的模型中有這個邏輯嗎?

+0

嘿,是的,這將是我的模型中的邏輯,在MalingerRutiner中來自Rutiner的外鍵是id行,這與Rutiner中的相同。 – user249494

+0

@ user249494,一個好的做法是:[數據庫規範化](http://en.wikipedia.org/wiki/Database_normalization)和[第三範式](http://en.wikipedia.org/wiki/Third_normal_form) – JulianoMartins