ORM的基本功能通常表示,由于数据库迁移很容易从一个数据库迁移到另一个数据库。在这里,我最初为我的项目使用了MySQL数据库,并计划将其迁移到PostgreSQL。在迁移时,它几乎可以处理select * from table,插入更新等所有简单查询。当从mysql迁移到postgresql时,Laravel elequent ORM不起作用
但是,当我通过MySQL中的某个datediff
函数时,ORM无法在PostgreSQL中创建相应的查询。
这里是我的查询
$result = DB::table('sales_target')
->select(db::raw('DATEDIFF(end_date,start_date) as DaysInQuarter'))
->whereraw("sales_target.target_quarter=$currentQuarter AND sales_target.target_year=$currentYear and status=1")
->first();
我不知道我是否已经写在查询中完全ORM结构与否。请帮我解决这个问题。
编辑1
N短期我想任何人告诉我,我怎么可以这样写查询
DB::table('sales_target')
->select(db::raw("DATEDIFF(end_date,start_date) AS DaysInQuarter"))
->first();
应在MySQL和postgresl使用ORM工作。
在Mysql中它正常工作,但在postgresql中不是这样。
我如果使用db::raw()
那么ORM不能帮助你从数据库引擎抽象得到一个错误,这样
No function matches the given name and argument types. You might need to add explicit type casts. (SQL: select DATEDIFF(end_date,start_date) AS DaysInQuarter from "sales_target" limit 1)
查询生成器是否也基于雄辩?他们的功能是不是可以互换的(我可以在一个雄辩的querry中使用每个DB功能)?我同意Db :: raw不是以任何方式ORM 请考虑到我只是在学习模式时,当我问这个:) – Radu
不,Eloquent使用查询生成器,但查询生成器不使用Eloquent。在上面你使用查询生成器,因为你开始于'DB :: table('sales_target')' - 如果你从你的模型'SalesTarget'开始,那么你将使用Eloquent –
谢谢先生! – Radu