开发者

How to use complex query in cakephp?

开发者 https://www.devze.com 2023-04-10 17:14 出处:网络
I am trying to do a query inside my model and call it in my controller, but for some reason I get some errors. Here is my model:

I am trying to do a query inside my model and call it in my controller, but for some reason I get some errors. Here is my model:

class Room extends AppModel {
var $name = 'Room';
var $displayField = 'title';
var $actsAs = array('Containable');

function select($qwe){
    $results = $this->Room->query("S开发者_JAVA技巧ELECT * FROM test WHERE qwe= ".$qwe." ");
}

In this case, I get Undefined property: Room::$room. In the controller I have:

...
function success(){
    $qwe = "testing"
$this->set('get', $this->Room->select($qwe));    
}
}

Any idea about what am I doing wrong?


You are calling within a Model so

    $results = $this->Room->query("SELECT * FROM test WHERE qwe= ".$qwe." ");

should be

    $results = $this->query("SELECT * FROM test WHERE qwe= ".$qwe." ");

because query() is a method of Class Model which is the parent class of every other Model, Model Room has no property Room that's why causing the error. If you would call this function via controller then you use $this->ModelName->method()

Your function select isn't returning anything, you should change it to

   function select($qwe){
       $results = $this->query("SELECT * FROM test WHERE qwe= ".$qwe." ");
       return $results;
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号