dpmopn8542 2017-06-22 10:00
浏览 37
已采纳

CakePHP 3 - 放置自定义SQL代码的位置

I'm building an application in CakePHP 3. It uses a number of legacy databases which are not built in the Cake conventions.

I do not want to use any of the ORM features Cake provides, as it's more tedious to set up all the relations than just write "Raw SQL". We are also not going to make any changes to the database structures, so the ORM is a non-starter. So I'm going to write raw SQL queries for everything.

However, I'm not sure where this code would be put. I've read https://book.cakephp.org/3.0/en/orm/database-basics.html#running-select-statements but it doesn't say where you actually put that code.

I don't want to put my queries in a controller ideally since that defeats the purpose of MVC.

All I really need is one Model where I can put all my queries in different functions and reference them in my Controller(s).

In Cake 2.x it was easy to just create a model under app/Model/ then load it (loadModel) where needed in controller(s). But with the new Cake 3.x Table and Entity spaces, I'm not sure how this fits in?

I've also read up on Modelless Forms but don't think they're right either. For example the initial page of the application shows a list of chemicals which is just a SELECT statement - it doesn't involve forms or user input at all at this stage.

Obviously there will also be situations where I need to pass data from a Controller to the Model, e.g. queries based on user input.

Any advice would be appreciated.

  • 写回答

1条回答 默认 最新

  • doumiang0597 2017-06-22 14:36
    关注

    As mentioned in the comments, I would suggest to not ditch the ORM, it has so many benefits, you'll most probably regret it in the long run.

    Setting up the tables shouldn't be a big deal, you could bake everything, and do the refactoring with for example an IDE that does the dirty work of renaming references and filenames, and then set up the rules and associations manually, which might be a little tedious, but overally pretty simple, as there shouldn't really be much more to configure with respect to the database schema, than the foreign keys, and possibly the association property names (which might require updating possible entities @property annotations too) - maybe here and there also conditions and stuff, but oh well.

    That being said, for the sake of completeness, you can always create any logic you want, anywhere you want. CakePHP is just PHP, so you could simply create a class somewhere in say the Model namespace (which is a natural fit for model related logic), and use it like any other class wherever needed.

    // src/Model/SomeModelRelatedClass.php
    
    namespace App\Model;
    
    class SomeModelRelatedClass
    {
        public function queryTheDatabase()
        {
            // ...
        }
    }
    
    $inst = new \App\Model\SomeModelRelatedClass();
    $results = $inst->queryTheDatabase();
    

    See also

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题