【问题标题】:Laravel saving mass assignment with a morphMany relationLaravel 使用 morphMany 关系保存质量分配
【发布时间】:2015-01-05 21:28:27
【问题描述】:

我正在查看 Laravel 中的批量分配,并尝试为一个模型及其相关模型批量分配数据。

我的模型:

class News extends Eloquent {
    protected $table = 'news';  
    protected $fillable = array(
        'title', 'slug', 'author', 'img', 'content',
    );
    public function content() {
        return $this->morphMany('Content', 'morphable')->orderBy('section');
    }
}

class Content extends Eloquent {
    protected $table = 'contents';  
    protected $fillable = array(
        'rawText', 'section',
    );
    public function morphable() {
        return $this->morphMany();
    }
}

我的输入

我的Input:all() 看起来像这样,来自表单:

array(6) {
  ["_token"]=>
  string(40) "irrelevant"
  ["title"]=>
  string(11) "Happy Title"
  ["author"]=>
  string(9) "Mr. Happy"
  ["slug"]=>
  string(11) "happy-title"
  ["img"]=>
  string(9) "happy.png"
  ["content"]=>
  array(1) {
    [0]=>
    array(2) {
      ["section"]=>
      string(4) "body"
      ["rawText"]=>
      string(27) "# I'm happy!   ## So happy"
}}}

我现在该怎么做才能将数据实际保存为两个新的数据库行? (一个新闻,一个内容)

我认为现在它会很简单:

$news = News::create(Input::all());
$news->push();

但我显然遗漏了一些东西。

我收到错误:preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

批量分配对相关模型根本不起作用吗?

或者它可以正常工作,但不能与 morphMany 关系?

我误解了$model->push(),还是$model::create

提前致谢。

【问题讨论】:

    标签: php laravel eloquent polymorphic-associations


    【解决方案1】:

    与 SO 问题一样,写作和格式化给了我一些思考的时间..

    $input = Input::all();
    $contents = Input::get('content');
    unset($input['content']);
    $news = News::create($input);
    foreach ($contents as $c) {
        $content = Content::create($c);
        $news->content()->save($content);
    }
    $news->save();
    

    有效!但感觉有点hackish。有没有更“雄辩”的方式批量分配相关模型?

    编辑:这可能是通常正确的做法 - 批量分配不会处理关系,但至少我可以单独批量分配每个模型。

    我只需要稍微调整一下输入,一旦将验证添加到等式中,我可能无论如何都必须这样做。

    Edit2:成功地将相关模型逻辑移入该模型并保持简单,例如:

    $input = Input::all();
    unset($input['content']);
    $news = News::create($input);
    $news = Content::updateAll($news, true);
    $news->save();
    

    用于创建,并且:

    $input = Input::all();
    unset($input['content']);
    $news = News::find($id)->fill($input);
    $news = Content::updateAll($news, false);
    $news->save();
    

    用于更新

    updateAll() 方法,适合任何感兴趣的人:

    public static function updateAll($model, $create = false) {
        $contents = Input::get('content');
        foreach ($contents as $k => $c) {
            // Save against parent model
            if ($create) {
                $content = Content::create($c);
            } else {
                $content = Content::find($k)->fill($c);             
            }
            $model->content()->save($content);
        }
        return $model;
    }
    

    现在我感觉自己正在全力以赴!

    【讨论】:

    • 首先,我认为不可能批量分配相关模型。您必须手动创建它们。另外我认为您理解push 是对的,但是如果批量分配不起作用,那么 push 将无济于事,因为它只是遍历模型的所有关系并保存它们
    • 哦,好吧,如果不可能,那就不可能了。 :( 我对我必须编写的样板代码太少感到太兴奋了——我想我必须这样做!
    • 好吧,我什么都不知道,但据我所知,这是不可能的:/
    • 限制孕育创造力,一旦我不再试图强迫它,我发现了一个很好的工作流程..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多