【问题标题】:Walk through a table to grab unique certificate numbers for shares浏览表格以获取股票的唯一证书编号
【发布时间】:2013-12-02 22:33:26
【问题描述】:

我正在使用的表名为共享。假设$share 是模型Share 的一个实例。 share 属于 company

| id | company_id | number_of_shares |
| 31 | 1          | 3                |
| 33 | 1          | 9                |
| 34 | 1          | 4                |

我想打电话给$share->getCertificate() 并让它返回一个证书号,这些应该是:

id 31 -> 1
id 33 -> 2
id 34 -> 3

注意:这会根据其在表中的位置递增一个唯一的证书编号。

然后当我打电话给$share->getDistinctive() 我希望它返回以下内容:

id 31 -> array('start' => 1, 'end' => 3)
id 33 -> array('start' => 4, 'end' => 13)
id 34 -> array('start' => 14, 'end' => 18)

注意:这会根据从 1 开始的共享数量递增

假设

表格将始终按 id 排序

我使用的是 laravel 4.0。

很高兴有人推荐更好的标题,以便其他人可以找到解决问题的方法。

【问题讨论】:

    标签: php mysql symfony laravel eloquent


    【解决方案1】:

    解决了 - 不知道为什么我认为它会更复杂。

    getCertificateNumber()

    public function getCertificateNumber()
    {
        $x=1;
    
        foreach($this->company->holdings()->get() as $holding)
        {
            if($holding->id == $this->id)
            {
                return $x;
            }
    
            $x++;
        }
    }
    

    getDistinctiveNumbers()

    public function getDistinctiveNumbers()
    {
        $shares=0;
    
        foreach($this->company->holdings()->orderBy('id')->get() as $holding)
        {
            if($holding->id == $this->id)
            {
                return ['start'=> $shares + 1, 'end' => $shares + $this->number_shares];
            }
    
            $shares += $holding->number_shares;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      相关资源
      最近更新 更多