Raildex
Raildex

Reputation: 4747

cakePHP - Model, Controller without convention/ member returns null

I have a Problem with cakePHP.

This is my Model:

<?php
class Veranstaltung extends AppModel
{
    var $name = 'Veranstaltung';
    var $useTable = 'veranstaltungen';
}
?>

and this is my Controller:

<?php
class VeranstaltungenController extends AppController
{
    var $name = 'Veranstaltungen';
    public function index()
    {
        $this->set('veranstaltungen',$this->Veranstaltung->find('all'));
    }

}
?>

but $this->Veranstaltung Returns null.

How do I solve this?

Upvotes: 1

Views: 98

Answers (3)

Karthik Keyan
Karthik Keyan

Reputation: 426

 <?php
    class VeranstaltungenController extends AppController

    var $uses = array('Veranstaltungen');
    public function index()
    {
        $this->set('veranstaltungen',$this->Veranstaltung->find('all'));
    }
   } 
 ?>  

Try in your controller

Upvotes: 0

marian0
marian0

Reputation: 3337

You forget to include your model in controller. Do do this add $uses field in your controller which is type of array:

class VeranstaltungenController extends AppController
{
     public $uses = array('Veranstaltung');

     public function index()
     {
         $this->set('veranstaltungen',$this->Veranstaltung->find('all'));
     }
}

Documentation

If there still be a problem it could be required to use

App::uses('Veranstaltung', 'Model');

before your controller class definition.


And please forget about var keyboard, because it's only for backward compatibility with PHP4.

In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in addition to) public, protected, or private. However, var is no longer required. In versions of PHP from 5.0 to 5.1.3, the use of var was considered deprecated and would issue an E_STRICT warning, but since PHP 5.1.3 it is no longer deprecated and does not issue the warning.

Upvotes: 2

Fury
Fury

Reputation: 4776

You don't need var $name = 'Veranstaltungen'; in your controller.

Also make sure you have configured your database properly?

Then try this:

$this->loadModel("Veranstaltungen");
$this->set('veranstaltungen',$this->Veranstaltung->find('all'));

Upvotes: 0

Related Questions