所有4則留言

[–]bloompire1指标

你好!

我不明白你想要达到的最后一行设置相机的实体camera_id??

Lens::find($_POST['lens_select'])->camera->update(array('camera_id' => 0));

反正。你在你的关系中使用默认的列名。就我的理解,你的相机和镜头lens_id } { ID,ID camera_id {,}。我不知道为什么你想有两个关系。只定义一个实体作为家长和中学作为一个孩子是多对多的关系威胁,但只有一行。

所以你的相机应该有:{编号,其他属性…},你的镜头应该有:{编号,camera_id }

Now you have a scene where Camera is a 'master' and Lens is 'slave'. Camera do not hold reference to lens, it is lens who hold ref to camera. Now "$this->belongsTo" makes sense in Lens and "$this->hasOne" in Camera.

And you will still be able to fetch relations in both ways to $camera->save( $lens ) , $lens->camera()->associate( $camera ) will work, and $camera->lens->somethign will work and $lens->camera->something will work.

另外,我个人如果有时雄辩不循规蹈矩,你会喜欢他,在你读取/保存,把这个线下:

那是_(DB):getquerylog(阳极);();

检查查询,你会看到什么样的DB是试图做的此刻,这将提供非常有用的线索,你犯了一个错误。

[–]fuckmywetsocksS]1指标

你好

谢谢你的回应。是啊,有点混乱的关系,但它没有被用错了,所以这只是死代码。我已经通过削减了这一切。

我已经使用原始的SQL语句的工作,但感觉糟糕。看下面:

function changeLensOnCamera() {
    DB::update('UPDATE lens SET camera_id = 0 WHERE camera_id = '.$_POST['camera_id']);
DB::update('UPDATE lens SET camera_id = '.$_POST['camera_id'].' WHERE id = '.$_POST['lens_select']);
return Redirect::to('cameras');
}

Any pointers on how this can be represented in Eloquent? Your var_dump() came in handy to debug the process step by step but that first step of setting camera_id to 0 where camera_id = the requested camera ID doesn't seem to run. In my mind, associate() should be doing this in the background.

在GitHub上的争论在这里关于这一点,泰勒奥特韦尔说设置外键为空(这给了我尝试原始SQL的想法)但我不快乐,这是唯一的方法。

编辑:之前你提到它,我知道这样的数据后在运行SQL是一个可怕的,可怕的想法。我现在我投入消毒。我只是在想它只是点…工作

[–]bloompire1指标

不知道这将工作…

class Lens extends Eloquent {
  public function camera() { 
    $this->belongsTo( 'Camera' ); 
  };
}

class Camera extends Eloquent {
  public function lens() {
    $this->hasOne( 'Lens' );
  }
}

function changeLensOnCamera() {

  $cameraId = Input::get( 'camera_id' );
  $lensSelect = Input::get( 'lens_select' );

  $camera = Camera::find( $cameraId );
  $oldLens = $camera->lens;

  if ( $oldLens ) {
    $oldLens->camera()->associate( null );
  }

  $camera->lens()->associate( Lens::find( $lensSelect ) );
  return Redirect::to( 'cameras' );

}

If it does not work this way, just go straight with $oldNews->camera_id = null

Its Eloquent, it is not like Doctrine, i mean so offical. Eloquent is much closer to DB than other ORMs and its just the way it works. I see nothing wrong in setting camera_id - its just like you do User::where( 'id' ,'=' , $id )->where( 'active' , '=' , 1 )->first(); You still have to find something using raw db column.

If it would be my project, I would just go with camera_id = null. To be honest, there is no "elegant" or "unelegant" ways to do things in web development (and programming). Its just all about, look at your code - will another programmer know what is going on here by looking at this method? If its clear, leave it as is. This is the most important thing, to make your code be easly understandable for other programmers (which reduces maintain cost).

看我的方法