2013-03-02 20 views
6

Ben Cakephp kendi MySQL sorguları oluşturmak çalışıyorum.nasıl CakePHP'de özel MySQL sorguları oluşturmak için?

Bu benim LocationsController.php:

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

Bu benim LocationModel.php:

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

    public $name = 'Location'; 

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

Gördüğünüz gibi, sadece basit bir sorgu gerçekleştirmek çalışıyorum ama çalışmıyor. Bu hatayı alıyorum:

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 

ben find gibi sihirli yöntemlerden birini kullanın ("tüm") yerine, çalışıyor ...

Sorunun ne görebilir mi? Gerçekten yapamam ve sadece basit bir görev yapmaya çalışıyorum!

+1

Eğer model içinde, sen $this->Location->query(); kullanmamalısınız,

Ayrıca hataya neden, bir SQL deyimi olarak get yürütmek, ancak ';' location' modelinde yeniden, sadece '$ this-> sorgu ('konumlarda SELECT *') olmaz mı? – AlienWebguy

+0

Aşağıdaki cevabı kontrol edin ve sonra ne olacağını bana bildirin! – Karma

+1

Ben zaten konum modeli var burada özel bir sorgu kullanmak için herhangi bir aklı başında bir sebep görmüyorum ve sadece 'bulmak yapabileceği (tümü)' ... Eğer gerçekten özel bir sorgu kullanmalıdır eğer hep kendinize sormalısınız. o zaman asla onlara gerçekten ihtiyacınız olmadığını öğreneceksiniz. – mark

cevap

7

Konum modelinin sınıf adı Location değil LocationModel olmalıdır. Bu nedenle

, CakePHP yerler veritabanı tablosu için bir 'jenerik' model oluşturmak ve kendi modelin yerine bu modeli kullanır. Bu jenerik modeli değil bir get() yöntemi var olduğundan, 'basitçe $this->query();

+1

'sorgu()' SQL Injection'ı engeller mi? –

+6

@FranciscoCorrales ** not ** bunu, içinde tanımlanmamış değişkenlere sahip bir literal sorgu iletirseniz, ancak * hazırlanmış ifadeleri destekliyorsa (bkz. Kaynak [here] (https://github.com/cakephp/cakephp/blob /2.4.9/lib/Cake/Model/Model.php#L3297)). Bunu şu şekilde kullan: '$ this-> query ('SELECT * FROM fooge WHERE id =? OR somefield =?', Dizi (123, 'foo'));' – thaJeztah

3

Yer Kontrolörü olmalıdır:

<?php 
App::uses('Location', 'Model'); // Y do u need this? 
class LocationsController extends AppController 
{ 
    public $helpers = array('Html', 'Form'); 
    function index() 
    { 
     $this->loadModel("Location"); 
     $this->LocationModel->getLocations(); // I will strongly discourage using get() 
    } 
} 

Yer Modeli olmalıdır:

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

    public $name = 'Location'; 

    public function getLocations() // Try to avoid naming a function as get() 
    { 
    /** Choose either of 2 lines below **/ 

     return $this->query("SELECT * FROM locations;"); // if table name is `locations` 
     return $this->query("SELECT * FROM Location;"); // if table name is `Location` since your public name is `Location` 
    } 
} 
+0

'sorgu()' SQL enjeksiyonunu engeller mi? –

+0

Hayır. Olmaz. Ancak, 'is_int()' veya 'ctype_alnum' – Karma

+0

gibi php işlevlerini kullanarak değişkenleri kontrol edebilirsiniz. Lütfen bu konuya bir göz atın ve ne düşündüğünüzü bana bildirin: https://github.com/cakephp/ CakePHP'de/damla/2.4.9/lib/Kek/Model/Model.php # L3297 –

İlgili konular