custom queries in cakephp - php

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?

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());
}}

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.

Related

Missing Database Table Error: Database table supports for model Support was not found

I'm building a controller for a legacy CakePHP2.5 application, the controller loads an existing model then query data from the table for that model, when call the controller I keep getting the error message below
Missing Database Table Error: Database table supports for model Support was not found.
below is the Support Controller code
<?php
class SupportController extends AppController{
public $name='Support';
public $helpers = array('Html','Form');
public function index(){
$this->loadModel('Applicant');
$appConditions = array(
'fields'=>array('first_name','surname','middle_name','maiden_name','date_of_birth'),
'order'=>array('Applicant.created DESC')
);
$this->paginate['Applicant'] = $appConditions;
$applicationData = $this->paginate('Applicant');
$this->set(compact('applicationData'));
// $this->set('applicants', $this->Applicant->find('all'));
}
}
The controller does not require a model as it is suppose to load data from the applicant model which already exists.
it seems cakephp requires the use of var $uses = null or public $uses = array(); ; to be declared in all controllers without a model, to resolve the error I had to declare a public
public $uses = array();
<?php
class NameController extends AppController{
public $name='Name';
public $helpers = array('Html','Form');
public $uses = array();
public function index(){
}
}

Get all records in cakephp3 using TableRegistry?

My Demo Controller (DemoController.php):
<?php
namespace App\Controller;
class DemoController extends AppController
{
public function users()
{
$this->loadmodel('registration');
$result = $this->registration->getAllUsers();
$this->set('user_data',$result)
}
}
?>
My registration model (registration.php):
<?php
namespace App\Model;
use Cake\ORM\TableRegistry;
class Registration extends AppModel {
$articles = TableRegistry::get('registration');
public function getAllUsers()
{
return $query = $articles->find();
}
}
?>
My View:
path -- src/Template/Demo/users.ctp
but it's getting error like this (in below image) --
Your model should be named RegistrationsTable, and be in src/Model/Table/RegistrationsTable.php. Load it with loadModel('Registrations'). (It's your entity that should be named Registration.) To use your custom finder method, name the function findAllUsers, fix the signature per the documentation (should take two parameters: Query $query, array $options) and call it as $this->Registration->find('all_users');.
And why are you trying to initialize a $articles variable as the registrations model inside the registrations model (but outside of any function)? So much mess...

How can I change Layout in Controller's Method (Laravel)

I used multiple layouts in Laravel using code as below, it works.
class UsersController extends BaseController {
public $layout = 'layouts.default';
...
}
But now I want to change the layout in Method (In UsersController)
public function myFunc(){
//I want to change myFunc's layout to 'default2'
$this->layout->content = View::make('user.myfunc');
}
How can I do? Because when I use $this->layout = 'layouts.default2'
it always returns me ErrorException: Attempt to assign property of non-object
You can use this in your controller method :
public function myFunc(){
$this->layout = View::make('layouts.default2');
$this->layout->content = View::make('user.myfunc');
}

CakePHP: Controller not finding Model function

So, I'm getting a fatal error because the method is undefined when the controller calls the method. Though this is not true as the method is inside the classes model.
StudentsController.php
<?php
class StudentsController extends AppController{
public function index(){
$students = $this->Student->find('all');
$this->set('students', $students);
}
public function add(){
if($this->request->is('post')){
$this->formatData($this->request->data);
}
}
}
?>
And then my model:
Student.php (Model)
<?php
class Student extends AppModel{
var $studentData;
public function formatData($studentData){
if(is_null($studentData)){
return false;
}else{
echo $studentData;
}
}
}
?>
You're not invoking the method on the model, but on the controller where there is no such method available, hence the error.
While the controller may automatically load the model, it doesn't expose its API, it just makes an instance of the model available via magic property accessors.
So instead of
$this->formatData($this->request->data);
you have to invoke the method on the model like this:
$this->Student->formatData($this->request->data);
See also
http://book.cakephp.org/2.0/en/controllers.html#Controller::$uses
http://book.cakephp.org/2.0/en/controllers.html#Controller::loadModel
http://book.cakephp.org/2.0/en/models.html#understanding-models

Cakephp error:Call to a member function find() on a non-object

I am having problems with my code. When I try to debug my web application I get the following error message....Call to a member function find() on a non-object.....here is my code
class TeamsController extends AppController {
var $name = 'Teams';
function index() {
$this->set('teams', $this->team->find('all'));
}
function Welcome() {
}
}
I am trying to display records from my MySQL database. Now with that said, I did this tutorial and I followed the instructions down to the tee.....but somehow my code has bugs. The only difference between my code and the code of the tutorial I did is the variable names...and the controller names....and the I dont have the hello world function... Here is a sample of the code from the tutorial I did....
class PostsController extends AppController {
var $name = 'Posts';
function index() {
$this->set('posts', $this->Post->find('all'));
}
function hello_world() {
}
}
With that said, am I suppose to declare an instance of an object to get this to work?
It's likely a case sensitivity issue:
function index() {
$this->set('teams', $this->Team->find('all'));
}
If not, ensure your controller has access to the Teams model (e.g. $uses).

Categories