Moumita Paul
Moumita Paul

Reputation: 21

How to custom query in Cakephp 1.3

I want to write some custom query in cakephp 1.3.17

I write the following in the function in the Users controller

$arrayTemp =array();
$arrayTemp = $this->Users->query('SELECT * FROM ht_users Where id=$id');
$this->set('post',$arrayTemp);
pr($post);

but its getting the following error in the page

Error: Call to a member function query() on a non-object    
File: D:\xamp\htdocs\devworks\app\Controller\UsersController.php    
Line: 1478

Line:1478 means it indicate -> $arrayTemp = $this->Users->query('SELECT * FROM ht_users Where id=$id ');

how I can execute a custome query here plz help

Upvotes: 1

Views: 2420

Answers (2)

user4094161
user4094161

Reputation:

As Your Model name is User so you need to write custom query as below.

$arrayTemp =array();
$id='1'; // For example. $id is whatever id you're receiving here
$arrayTemp = $this->User->query("SELECT * FROM ht_users Where id = '$id'");
$this->set('post',$arrayTemp);

Now you can pr($post) in your function.ctp file. If this code is written in index() function then try to print in index.ctp likewise.

Please try with above code, Let me know if still any issues.

Upvotes: 2

Mahendra Singh
Mahendra Singh

Reputation: 1

As per Cakephp Conventions,

Model class name are singular, this should be the database table name but in singular format.

So,In your case it must be User

And then use below code

$arrayTemp =array();
$arrayTemp = $this->User->query('SELECT * FROM ht_users Where id=$id');
$this->set('post',$arrayTemp);

Upvotes: 0

Related Questions