Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom queries in cakephp

Tags:

php

cakephp

I'm a beginner in cakephp. I want to create a custom query and retrieve data from a table in cakephp. I have a table called key_uniqueword in the database.

Here is my code:

MODEL:

key_uniqueword.php

<?php

class key_uniqueword extends AppModel{


    var $name = 'key_uniqueword';


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


    } 

Controller: ReadingManualsController.php

<?php


class ReadingManualsController extends AppController{

    var $name = 'ReadingManuals';


    function index(){

        $this ->set('results',$this ->key_uniqueword-> get());


        }}

view: index.ctp

<!DOCTYPE html>

<?php


foreach($results as $result)
{

//display results here  
    }

?>

But I'm getting this error message.

Error: Call to a member function get() on a non-object  
File: F:\xampp\htdocs\18\cake\app\Controller\ReadingManualsController.php   
Line: 22

How can I correct this?

like image 414
Asanka sanjaya Avatar asked Jun 06 '26 18:06

Asanka sanjaya


1 Answers

Your model file :

KeyUniqueword.php

<?php

 class KeyUniqueword extends AppModel{


 public $name = 'KeyUniqueword';
 public $useTable = 'key_uniqueword';  // use own table name  


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


}

Your controller:

<?php


class ReadingManualsController extends AppController{

  public $name = 'ReadingManuals';

  public $uses = array('KeyUniqueword','ReadingManual') 

  function index(){

    $this ->set('results',$this ->KeyUniqueword-> get());


    }}
like image 76
Salines Avatar answered Jun 08 '26 06:06

Salines



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!