CakePHP - Class ** cannot be found - cakephp

I have a strange problem with CakePHP
CakePHP gives an error on the following line:
View/Designer/cards:
$this->JsBridge->set('Card.DISPLAY_TYPE_FOLDER_GREETING', Card::DISPLAY_TYPE_FOLDER_GREETING);
Class 'card' can not be found.
However in DesignersController I load the model Card via de following line:
public $uses = array('Designer', 'Card');
If I add the following line in the top of DesignersController
App::uses('Card', 'Model');
The page loads, but the following line does not work:
$this->paginate = $this->Card->getPagination($filter);
I have put the code for the model Card.php here : http://pastebin.com/U7zxKHCx
Can you tell me what is going wrong?
Thank you!

Are you including the CardModel in your Controller?
$uses = array('....','Card',....);
Controller properties, classes attached etc. are NOT directly accessible in a View. You will need to set this constant Card::DISPLAY_TYPE_FOLDER_GREETING to a variable:
$this->set('variableName1', Card::DISPLAY_TYPE_FOLDER_GREETING);
Then use it in the View:
$this->JsBridge->set('Card.DISPLAY_TYPE_FOLDER_GREETING', $variableName1);

Related

View Error CakePHP UploadPack plugin

I am using CakePHP's UploadPlugin (https://github.com/szajbus/uploadpack). Storing images goes smooth as documented, the problem kicks in when trying to use the helper to view the images:
Fatal Error
Error: Call to a member function image() on a non-object
Code:
<?php echo $upload->image($entry['User']['id'], 'User.avatar') ?>
The helper is correctly loaded in the controllers. What could caseis issue? thanks
UPDATE:
My helper is included as follows:
public $helpers = array('Form', 'UploadPack.Upload');
I had the same issue. I was able to fix it by doing the following:
echo $this->upload->image($MODELNAME, 'MODEL.avatar');

How to use find('all') in Views - CakePHP

I searched a lot but I couldn't find on How to use the find('all') in Views as used in Rails, but here I'm getting the error "Undefined property: View::$Menu [APP\Lib\Cake\View\View.php, line 804]"
'Menu' is the model which I'm using to fetch data from the menus table.
I'm using the below code in views:
$this->set('test',$this->Menu->find('all'));
print_r($test);
Inside your Menu model create a method, something like getMenu(). In this method do your find() and get the results you want. Modify the results as you need and like to within the getMenu() method and return the data.
If you need that menu on every page in AppController::beforeFilter() or beforeRender() simply do
$this->set('menu', ClassRegistry::init('Menu')->getMenu());
If you do not need it everywhere you might go better with using requestAction getting the data using this method from the Menus controller that will call getMenu() from the model and return the data. Setting it where you need it would be still better, if you use requestAction you also want to cache it very likely.
TRY TO NOT RETRIEVE DATA WITHIN VIEW FILE. VIOLATION OF MVC RULE
try this in view file:
$menu = ClassRegistry::init('Menu');
pr($menu->find('all'));
In AppHelper ,
Make a below function
function getMenu()
{
App::import('Model', 'Menu');
$this->Menu= &new Menu();
$test = array();
$test = $this->Menu->find('all');
return $test;
}
Use above function in view like :
<?php
$menu = $html->getMenu();
print_r($menu);
?>
Cakephp not allow this .
First create the reference(object) of your model using ClassRegistry::init('Model');
And then call find function from using object
$obj = ClassRegistry::init('Menu');
$test = $obj->find('all');
echo ""; print_r($test); `
This will work.

CakePHP 2.0: Error on loading an image with the html-helper

I've switched to the newest Version of CakePHP, but when I was about to build up my layout, I got this error:
Fatal error: Call to a member function image() on a non-object in C:\xampp\htdocs\propfe\app\View\Layouts\default.ctp on line 91
of course I've already included the helper in lib/Cake/Controller/AppController.php:
public $helpers = array('Session','Html','Js');
and although it's improbable, the error is in this line, here's no 91 of the layout:
<?php echo $this->html->image('aesculap.jpg'); ?>
You have to use $this->Html instead of $this->html.
You have the helper defined in AppController, but does your controller that is loading this view extend AppController or Controller.

CakePHP 2.0: Preparing data from a different model by the AppController for a Helper?

I've made a helper, that should output some data from my database in the layout, so this data have to be available everywhere. Now I tried to load and set a variable by the AppController, but it seems I can not use the usual find()-method. Here's my error:
Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\propfe\Controller\AppController.php on line 46
That's how I tried to load the Model, once by the $uses-variable and once by the App::import()-Command:
var $uses = 'Surgeryhour';
App::import('Model','Surgeryhour');
And here's the line 46 of the error:
$this->set('data', $this->Surgeryhour->find(null, '1'));
Any ideas how I can get all this to work?
Try load model like this:
$this->loadModel('Surgeryhour');
$this->set('data', $this->Surgeryhour->find(null, '1'));
Don't need variable $uses.
never use App::import for models
always:
$this->Surgeryhour = ClassRegistry::init('Surgeryhour');

HtmlHelper not loaded in tutorial example

I followed the tutorial to create a blog for CakePHP 1.3 up to this step but keep getting error when running the app:
Notice (8): Undefined property: View::$Html [APP\views\posts\index.ctp, line 27]
Line 27:
echo $this->Html->link($post['Post']['title'], array('controller' => 'posts', 'action' => 'view', $post['Post']['id']));
Apparently CakePHP doesn't load the HtmlHelper class, I check over and over again in my controller, the Html should be loaded properly.
class PostsController extends AppController {
var $name = 'Posts';
var $helpers = array('Html', 'Form');
function index() {
$this->set('posts', $this->Post->find('all'));
}
}
When I added this line to the view (index.ctp), it works
$this->Html = &$this->loaded['html'];
But apparently I can't do that for every ctp file. I'm running Windows 7, WAMP 2, PHP 5.3.5, CakePHP 1.3.7 stable.
Anyone has a clue?
Like the comment by mtnorthrop above:
Is the FormHelper being loaded in your
views? What do you get if you do
pr($this->Html) in your view? How
about pr($html)? Until CakePHP 1.3,
helpers were accessed directly instead
of through the View object. In CakePHP
1.3 both methods should work. Does the plain $html->link() or $form->input()
methods work for you? – mtnorthrop 51
mins ago
From the book:
"The HtmlHelper is available in all
views by default. If you're getting an
error informing you that it isn't
there, it's usually due to its name
being missing from a manually
configured $helpers controller
variable."
You shouldn't need to specify it in your controllers. Perhaps somehow doing so is interfering with the core? BTW, you don't need to specify Form either.

Resources