【问题标题】:Laravel order items in database数据库中的 Laravel 订单项目
【发布时间】:2015-09-30 07:14:11
【问题描述】:

您好,我正在使用 Laravel 和自定义 CMS 将存储在 CMS 中的一些标签同步到数据库中。此功能工作正常。但是,我想在将它们插入数据库时​​以数字方式为它们设置顺序,并且如果将新的同步到数据库中,我希望将其作为最后一个值添加到数据库中,即如果有 12 个项目存在新项目将在 order 列中添加为值 12(因为 0 将是第一个值)。我希望在不重置现有订单值的情况下完成此操作。

这是我的存储库中的内容:

public function sync()
{

    $tags = $this->client->fetchTags('Status');
    $updated = $this->update_tasks($tags);        
    // remove the rest of the statuses
    \Status::whereNotIn('id',$updated->lists('id'))->delete();
}

/**
 * Save any tags to the local DB;
 * @param $tag
 */
public function update_tags($tags)
{


    $return = [];
    foreach ($tags as $tag) {
        $return[] = $tag->id;    
    }


    $local  = \Status::whereIn('cms_tag_id',$return)->get()->toIndexedArray('cms_tag_id');

    foreach ($tags as $tag) {
        
        if (isset($local[$tag->id]))
        {
            $local_doc = $local[$tag->id];
        } 
        else
        {
            $local_doc = new \Status;
        }
        $local_doc->updateFromCMS($tag);
        $uids[] = $local_doc->cms_tag_id;
    }
    return \Status::whereIn('cms_tag_id',array_unique($uids));
}

然后在我的模型中我有这个: 状态.php

public function updateFromCMS($tag)
{
    $i = 0;
    $this->cms_tag_id = $tag->id;
    $this->cms_tag_name = $tag->name;
    $this->order = $i++;
    $this->save();
    return $this;
}

但是,这会将订单列中的所有值添加为零,这是不希望的,并且找到的任何新值也将添加为零值。有什么想法可以让我保存这个订单,例如:0-100?

更新

根据@insanebits 的说明,我已将模型更改为以下内容:

public function updateFromCMS($tag)
{
    $i = Status::orderBy('order', 'DESC')->pluck('order');
    $this->cms_tag_id = $tag->id;
    $this->cms_tag_name = $tag->name;
    $this->order = $i+1;
    $this->save();
    return $this;
}

但是,现在这会更改所有行的顺序值。即如果最大值是 5,那么 0 的值将被设置为 6,然后所有的值都将增加 1。因此,设置的顺序将丢失。任何想法我做错了什么??

【问题讨论】:

  • 因为每次调用此方法时,它都会在开头使用$i = 0;。您需要从数据库中检索order 值,然后才递增
  • $i = ModelClass::where('order')->orderBy('order', 'DESC) 其中模型类是您正在使用的实际模型
  • 我的意思是你需要获得最大的相关order 值,它存储在你的数据库中

标签: php database laravel model


【解决方案1】:

如果您将数据库中的order 列设置为autoincrement 会怎样?例如您的迁移可能如下所示:

class CreateStatusTable extends Migration {

    public function up() {
        Schema::create('status', function (Blueprint $table) {
            $table->increments('id');
            $table->increments('order');
            /* ... your other fields ... */
            $table->timestamps();
        }
    }
}

这样您就不必担心在模型中显式设置order 字段,并且您知道每次向数据库添加新的Status 时,它总是会被分配下一个最高整数值.

或者,您可以完全删除order 列并依赖id 字段,您知道该字段将是一个整数,并在您将新的Status 行添加到数据库时按顺序递增。

我可以看到这种方法的唯一潜在缺点是,如果您删除了 Status,您最终会得到一些具有非顺序值的行 idorder,即如果您有 5表中的项目并且您删除了带有id = 3 的项目,您最终会得到4 个id 值为0, 1, 2, 4 的项目。但是,顺序将始终保留,代码如下:

$statuses = Status::orderBy('id', 'DESC');

将始终按照您将它们添加到数据库中的原始顺序返回一个 Status 对象数组。

【讨论】:

    【解决方案2】:

    如果您不需要更新现有标签的 id 和名称,您应该只为新标签调用 updateFromCMS

    foreach ($tags as $tag) {
    
        if (isset($local[$tag->id]))
        {
            $local_doc = $local[$tag->id];
        } 
        else
        {
            $local_doc = new \Status;
            $local_doc->updateFromCMS($tag); //only for new tags
        }
    
        $uids[] = $local_doc->cms_tag_id;
    }
    

    如果您确实需要更新现有标签的 id 和名称,则向 updateFromCMS 添加一个参数,告诉它是否更改顺序:

    public function updateFromCMS($tag,$update_order)
    {    
        $this->cms_tag_id = $tag->id;
        $this->cms_tag_name = $tag->name;
    
        if($update_order) {
            $i = Status::orderBy('order', 'DESC')->pluck('order');
            $this->order = $i+1;
        }
    
        $this->save();
        return $this;
    }
    

    并按如下方式更改您的foreach

    foreach ($tags as $tag) {
    
        if (isset($local[$tag->id]))
        {
            $local_doc = $local[$tag->id];
            $local_doc->updateFromCMS($tag,false); //don't update order
        } 
        else
        {
            $local_doc = new \Status;
            $local_doc->updateFromCMS($tag,true); //update order
        }
    
        $uids[] = $local_doc->cms_tag_id;
    }
    

    【讨论】:

    • 太好了,这就是我需要的。当计时器用完时,我会奖励赏金
    猜你喜欢
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2019-09-13
    • 2019-07-18
    • 2018-06-24
    • 2018-01-02
    相关资源
    最近更新 更多