dpgu5074 2014-02-04 17:55
浏览 93

DB :: raw()始终使用默认数据库

I've been searching for a while for a solution here but no luck. I have a model named Currency which extends eloquent.

class Currency extends Eloquent {

   protected $connection = 'currency';
   protected $table = 'dbo.sfCXDetail';
   public $timestamps = false;


    public function monthlyTransactions(){
        return Currency::select(array(DB::raw('COUNT(trx_number) AS Transactions'), DB::raw('MONTH(update_stamp) as TransactionsMonth')))
                                ->whereBetween(DB::raw('DATEPART(YYYY, update_stamp)'), array(2012,2012))
                                ->groupBy(DB::raw('YEAR(update_stamp)'))
                                ->groupBy(DB::raw('MONTH(update_stamp)'))
                                ->orderBy(DB::raw('YEAR(update_stamp)'))
                                ->orderBy(DB::raw('MONTH(update_stamp)'))
                                ->get();
    }

} 

The problem is, DB::raw uses the default database inside the database config file, so when I try using:

Currency::raw()

I get an error strtolower() expects parameter 1 to be string, object given

The database I'm using can't be used as the default database. How do I use the DB::raw method with the current database in use inside the model?

This query works without error when I set the default database to 'currency', but not if I set it to use my local default mysql database.

This is in my DB config file:

'currency' => array(
        'driver'   => 'sqlsrv',
        'host'     => 'xx',
        'database' => 'xx',
        'username' => 'xx',
        'password' => 'xx',
        'prefix'   => '',
    ),
  • 写回答

2条回答 默认 最新

  • douxiao0400 2014-02-04 21:00
    关注

    I could be wrong but I believe that setting the connection property as a protected property of Currency would not also set connection for the DB class.

    would something like this work (I am at work at not able to test, sorry):

    $db = new DB;
    $db->connection = 'currency'
    $db->table = 'dbo.sfCXDetail';
    ...
     return Currency::select(array($db->raw('COUNT(trx_number) ...
    ...
    

    I think it's a scope thing

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答