久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 CakePHP 中創建自定義 MySQL 查詢?

How to create custom MySQL queries in CakePHP?(如何在 CakePHP 中創建自定義 MySQL 查詢?)
本文介紹了如何在 CakePHP 中創建自定義 MySQL 查詢?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試在 Cakephp 中創建我自己的 MySQL 查詢.

I am trying to create my own MySQL queries in Cakephp.

這是我的LocationsController.php:

<?php
App::uses('Location', 'Model');
class LocationsController extends AppController
{
    public $helpers = array('Html', 'Form');
    function index()
    {
        $this->loadModel("Location");
        $this->Location->get();
    }
}

這是我的LocationModel.php:

<?php
App::uses('AppModel', 'Model');
class LocationModel extends Model {

    public $name = 'Location';

    public function get()
    {
        $this->Location->query("SELECT * FROM locations;");
    }
}

如您所見,我只是嘗試執行一個簡單的查詢,但它不起作用.我收到此錯誤:

As you can see, I am just trying to perform a simple query but it doesn't work. I get this error:

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error 
in your SQL syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near 'get' at line 1

當我使用 find("all") 等魔術方法之一時,它會起作用...

When I use one of the magic methods like find("all") instead, it works...

你能看出問題是什么嗎?我真的做不到,我只是想做一個簡單的任務!

Can you see what the problem is? I really can't and I'm only trying to do a simple task!

推薦答案

Location 模型的類名應該是 Location,而不是 LocationModel.

The class name of your Location model should be Location, not LocationModel.

因此,CakePHP 將為 Locations 數據庫表生成一個通用"模型,并使用該模型而不是您自己的模型.因為這個泛型模型沒有沒有get()方法,它會將get作為SQL語句執行,導致錯誤

Because of this, CakePHP will generate a 'generic' model for the Locations database table and use that model instead of your own model. Because this generic model does not have a get() method, it will execute get as a SQL statement, causing the error

此外,在模型內部,您不應該使用 $this->Location->query();,而應使用 $this->query();代碼>

Also, inside the Model, you should not use $this->Location->query();, but simply $this->query();

這篇關于如何在 CakePHP 中創建自定義 MySQL 查詢?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: a久久 | 国产精品久久久久无码av | 成年免费在线观看 | 亚洲国产网| 亚洲视频在线观看一区二区三区 | 91pao对白在线播放 | 日韩欧美手机在线 | 成人a网| 欧美国产精品一区二区三区 | 久久久久av | 国产精品美女久久久 | 一区二区免费 | 日韩欧美精品一区 | 欧美另类日韩 | 在线观看亚 | 久久久夜色精品亚洲 | 精品免费在线 | 久久亚洲精品国产精品紫薇 | 超黄毛片 | 亚洲精品乱码8久久久久久日本 | 国产一区久久精品 | 视频一区二区三区中文字幕 | 精品无码久久久久久国产 | 午夜激情影院 | av黄色在线 | 久久久www | 日本久久精 | 人人天天操| 欧美国产一区二区 | 中文字幕在线视频精品 | 91精品在线播放 | 亚洲精品视频观看 | 久久久久久国产精品免费免费男同 | 日日操av | 欧美久久精品一级c片 | 性色视频| 欧美精品网站 | 成人免费一区二区三区视频网站 | 日韩毛片在线视频 | 日韩中文一区二区三区 | 久久久精品一区 |