Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 662 Vote(s) - 3.63 Average
  • 1
  • 2
  • 3
  • 4
  • 5
custom queries in cakephp

#1
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?
Reply

#2
Firstly, your get() function does not return anything! Also, '$this->key_uniqueword' does not yet exist. There are two main ways to prepare that variable, including:

class ReadingManualsController extends AppController{

var $name = 'ReadingManuals';
public $uses = array('key_uniqueword');//populates $this->key_uniqueword
...

Or you can create it before using it, e.g. inside the controller:

$this->loadModel('key_uniqueword');
//now you can use $this->key_uniqueword

But... You should realy do a simple CakePHP tutorial and follow the conventions. In rare/tricky cases a custom query may be required, but this is not one of them.
Reply

#3
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());


}}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through