What is a proper way to use model inside component in cakephp? - cakephp-2.0

When calling Partner model from component it is showing error.
App::import("MyPlugin.Model", "Partner");
$objPartner = new Partner();
Fatal error: Class 'Partner' not found in D:\htdocs\myapp\app\Plugin\MyPlugin\Controller\Component\TestComponent.php on line 287

Never use App::import.
Always either use loadModel in controller or shell scope or the always working
$Partner = ClassRegistry::init('Partner');

Related

Symfony2 - Trouble using usort inside a controller

I am trying to merge the contents of 2 arrays then use usort to get the posts with most views.
Trying to use usort to sort the contents of an array.
Am getting the follow error:
("Notice: Undefined property: Acme\DemoBundle\Entity\Article::$getViews in /.../PageController.php line 15")
Can someone point out what I am doing wrong?
Sort function inside of controller
private static function popularSort($articles, $posts, $articles2, $posts2)
{
return $articles->getViews() == $posts->getViews() ? 0 : ( $articles->getViews() < $posts->getViews()) ? 1: -1;
}
Sidebar action
$articles = $this->getDoctrine()->getRepository('AcmeDemoBundle:Article')
->getArticles();
$posts = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post')
->getPosts();
$popular = array_merge($articles, $posts);
usort($popular, array($this, 'popularSort'));
getViews is a getter method for accessing the property views of the Entities Post and Article. So while accessing it u should access it as $articles->getViews().
But if you simply want to compare the property views of the two entities compare them using their property name instead of their getter
Assuming views as the name of the property, the call should be something like:
$posts->views and $articles->views.
You don't have a property called getViews in your Article class.
You probably have a property called views and a method getViews meaning you should call the actual method with the brackets like $article->getViews().

CakePHP - Class ** cannot be found

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

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: 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');

CakePHP - Including ACL component gives fatal error: ConnectionManager::getDataSource - Non-existent data source

I'm trying to figure out ACL, and so I'm trying to work through the tutorial in the book (Cake 1.3, by the way).
I've created the database tables (aros,acos,aros_acos). As soon as I try to include the Acl component in my AppController, however, I get a fatal error when I try to access any page:
Fatal Error (256): ConnectionManager::getDataSource - Non-existent data source [CORE/cake/libs/model/connection_manager.php, line 102]
In my AppController:
public $components = array('Auth','Session','RequestHandler','Acl');
Removing Acl from the components array makes everything work again.
The errors disappear when I comment out some code in my AppController. Here is the code - the lines commented out are the culprits. AppModel::slugList() is a custom function that just does a find query based on a slug. It works fine, at least until Acl is included.
if ($this->modelClass != 'Country'){
$this->loadModel('Country');
}
if ($this->modelClass != 'Category'){
$this->loadModel('Category');
}
$this->Session->write('Country',1);
$this->Session->write('City',1);
$_countryId = $this->Session->read('Country');
//$_countries = $this->Country->slugList();
$_cityId = $this->Session->read('City');
//$_cities = $this->Country->City->slugList();
Edit - also, three notices appear:
Notice (8): Trying to get property of non-object [CORE/cake/libs/model/datasources/dbo_source.php, line 813]
Notice (8): Trying to get property of non-object [CORE/cake/libs/model/datasources/dbo_source.php, line 838]
Notice (8): Trying to get property of non-object [CORE/cake/libs/model/datasources/dbo_source.php, line 841]
This error is caused by an inconsistency between your models and/or the database table structure. The key to debugging it is the set of notices. Inserting a var_dump statement on dbo_source.php::line 813 will give you a hint as to where your break is located. Example:
foreach ($model->{$type} as $assoc => $assocData) {
$linkModel =& $model->{$assoc};
$external = isset($assocData['external']);
var_dump($model->name, $assoc);
if ($model->useDbConfig == $linkModel->useDbConfig) {
if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
$linkedModels[$type . '/' . $assoc] = true;
}
}
}
In my case, I had a model defined for a database table I had forgotten to create.
Well, I had a model called "Permission" that my model "Role" that actsAs an Acl requester was related to. This was the problem. I guess the Acl component uses a class with this name somewhere?

Resources