elements or view extensions or something else? reference app? - cakephp

In cakephp [2.2] I have baked everything and my "people" view is quite busy with relations and phones and addresses and other related data. I do want all of that information visible in the people view, though not quite in the baked layout.
How should I handle those portions of the related data? I'm not sure if I should use elements or extended views or plugins or what, I'm kinda new to this and the documentation wasn't clear to me (at my level) which should be used when. The baked code seemed to be a monolithic approach, so I didn't get much help looking there.
Once the user chooses to edit a phone number (for instance) from the listing on the person view, it takes them to the phone edit view and then returns them to the phone listing (index view) and not the person view that they were on. How do I get them back to the person view instead?
The blog example they provide is nice, but is there a "reference" application somewhere for cakephp that demonstrates best practices on a wide variety of their features? I couldn't find one, or anything more than just a simple app example.
Thanks, I appreciate the guidance.

This is a rather broad question, but I'm going to try and answer it. I'm not sure how advanced you're programming knowledge is, so forgive me if I'm rehashing things you already know. First, this article was a great help when I started to use the framework for the first time as it explains what code should go where and why. It's the closest I've seen to a "reference application", which would actually be a great learning tool. You could try and have a look at some of the higher profile Cake applications, like Croogo (a Cake-based CMS). But the codebase is bound to be a little bit complex.
Personally I would use elements when you want to actually reuse them in different views. The problem however, is feeding the element its data. There's a method called requestAction, but even the manual states that this should be used with moderation and in combination with caching. The problem is that using a lot of requestAction calls in different elements litters your Controllers with methods and doesn't adhere to the "Skinny Controllers, Fat Models" mantra.
I would put most of the related data calls in their respective Models and call those Model methods from the Controller and feed them to the View. So let's say you want the 10 latest PhoneNumbers and related Users.
You would have a method in your PhoneNumber model which returns an array of users and their phonenumbers. Use the Containable behaviour to limit the number of related models which are returned. The code below is an example, so the practical implementation might vary:
public function getRecentPhoneNumbers($limit=10) {
$phoneNumbers = array();
$phoneNumbers = $this->find('all', array(
'limit' => $limit,
'contain' => array('User'),
'order' => 'PhoneNumber.id DESC'
));
return $phoneNumbers;
}
If the PhoneNumber and User model are properly related you would be able to call getRecentPhoneNumbers() from the User model:
$this->PhoneNumber->getRecentPhoneNumbers(10)
Or from the Users Controller:
$this->User->PhoneNumber->getRecentPhoneNumbers(10)
Say you have an element which shows a list of those 10 numbers and it accepts a variable called $recentPhonenumbers, you then set the variable in the relevant UsersController method with the returned array from the getRecentPhoneNumbers call:
$this->set('recentPhonenumbers', $this->User->PhoneNumber->getRecentPhoneNumbers(10));
This will make it available to the View that contains the element.
The extended views are relatively new (from Cake 2.1 and onwards) and I haven't used them, but seem a great way to create conditional markup.
As for the second question, redirecting the user to the person view, rather than the index view. This is a matter of adjusting the redirect (see the manual for more details) in the edit() method of the Controller. Standard baked edit() methods accept an $id parameter you can use this to redirect to the view() (which probably also accepts an $id paramater).
So the redirect probably looks something like this:
$this->redirect(array('controller' => 'users', 'action' => 'index'));
Change it to:
$this->redirect(array('controller' => 'users', 'action' => 'view', $id));

Related

Removing a belongsTo from a model shows warning Model 'x' is not associated with model 'y'

I am new to CakePHP and I am having to maintain/develop a CakePHP application and struggling with removing a BelongsTo in one of the models.
The code has been copied from another project that used a lot more related tables. In the new project I do not need the relationships because it is creating LEFT JOINS in the queries that I really want to get rid of in order to speed things up. So I went in to the model and removed the $belongsTo property..
When I go to the home page I now get a warniing:-
Warning (512): Model "Product" is not associated with model "ParentProduct"
[CORE/Cake/Model/Behavior/ContainableBehavior.php, line 343]
If I turn off debugging it is not shown but I would like to know why this message is being generated. I am unable to find any information on how I should go about removing the belongsTo relationships.
Any help appreciated.
So I went in to the model and removed the $belongsTo property
Because you're asking for it
This kind of error is displayed when using the containable behavior incorrectly.
E.g. Consider User hasMany Article, and the following code:
$User->find('all', array(
'contain' => array(
'Article'
)
));
This would return all users and their articles (only), irrespective of any other associations that exist.
If an association is requested that isn't defined:
$User->find('all', array(
'contain' => array(
'Article',
'Comment'
)
));
Containable will generate a warning so that you, the developer, can know that there will be no Comments in your results which either indicates a typo or another kind of development error (Ah... it's User<-Article<-Comment).
This is in-keeping with what you describe since the model associations have been modified but (evidently) not the application code.
The message is been generated (most probably) because in your controller (check the ParentProductsController first) is something like
$this->ParentProductsController->Product->find();
That association of models no longer exists because you wanted to get rid of it, so there's no way for ParentProducts to call Product. You need to change calls like that to
ClassRegistry::init('the_name_of_the_model');
(in this case ClassRegistry::init('Product');) or load the model with
$this->loadModel('Product');
Now, this only is necessary when you call models not related to the current controller. If you are calling Product->find from the ProductsController, there will be no error.
Ok, that's the cause of the error.
But now: I don't recommend you deleting the association. There's really no need and your removing a logical association in the code even though it's still there in the database. There are other ways to avoid left joins and useless queries to the database without cutting someone a limb.
If you want to speed things up, read about Containable Behavior, and set all models to $recursive = -1 (recursive doc). That way you won't get LEFT JOINS unless you explicitly say so.

cakePHP model associations lost after model imported into controller

Due to a client's desire to have URLs that defy Cake logic I have decided to use the pages_controller.php copied from cake/libs to organize my app.
Most of the site's functionality occurs from one url making calls with ajax to different controllers so I chose Pages as the home base.
Pages has no model but I still need to access some of the relevant models so I import them.
I have tried all three methods:
$this->loadModel('Inventories');
----
$Inventories =& ClassRegistry::init('Inventories');
----
App::import('Controller', 'Inventories');
$Inventories = new InventoriesController;
The Inventories Model seems to load fine with each but when I find some records:
$(...)->find("all", array(
'conditions' => array('id' => '1'),
'recursive'=>2)
);
I only get results as if recursive had been set to -1 or as if there were no associated models.
This happens no matter what I set recursive to.
When I load the model in another controller I get the appropriate recursive response.
Any ideas how i can get full access to a model from pages_controller.php?
A messy but instant solution would be to simply populate the $uses array in your controller:
var $uses = array('Inventories', 'User'); // or whatever models need to be loaded
This is considered poor practice and could quickly spiral out of control, however. Mostly handy for quick testing.
You could place the functionality in appropriately named controllers, and then route your client's desired URLs to the corresponding controller / action:
http://book.cakephp.org/view/945/Routes-Configuration
A little-known controller method (it's not in the cookbook, only the API) called setAction() might be the solution for you. See:
http://api13.cakephp.org/class/controller#method-ControllersetAction
also in Cake 2.0
http://api20.cakephp.org/class/controller#method-ControllersetAction

What are the reserved database table names in CakePHP?

I was planning my database and since CakePHP hasn't named its classes with its own prefixes, there are many classes that may collison with model classes created according to naming conventions.
So my question is in three parts:
Is there a list of database
table names that are reserved or a
simple way to check if it is? It
would be a pain if I plan database
with 100 tables and notice some of
the tables and their connections would
have to be renamed...
Is there a simple way around it without breaking any CakePHP magic? Is it adviced? Would naming the tables in other way by adding own prefix 'my_' or similar at the beginning of table name the best way?
Is namespaces or something similar coming to
CakePHP version 2 that would allow
use of all kinds of table names?
No, there aren't. Cake doesn't care what you name your tables as long as you adhere to Cake's naming conventions. It generates the schemas it uses for magic model methods the first time a model/s is loaded by a controller; you don't have to lift a finger. See http://book.cakephp.org/view/903/Model-and-Database-Conventions
Best advice: don't fight Cake on this. If you really cannot adhere to to Cake's conventions, you might as well not use Cake; it's stupidly difficult, confusing and succeeding just means you've lost most of Cake's heavy lifting abilities. Pluralizing your table names isn't THAT bad, and Cake will be happy.
This functionality is already available in 1.3 - name your tables anything that pleases you (as long as they're plural words.)
-- You'd probably be well-served to check out baking apps in the console so you can get familiar with what Cake wants to see and how it works on different table layouts.
Edit after clarification:
Your models, controllers, and view directories all share a common name, like so:
// in /app/models/rate.php
class Rate extends AppModel {
var $name = 'Rate';
// in /app/controllers/rates_controller.php -- note the underscore
class RatesController extends AppController {
// controllers are capitalized + plural
var $name = 'Rates';
// in /app/views/rates/*.ctp - Cake's magic autoRender expects to find a view file with
// the same name as the action rendering it - the view for Rates::index() is index.ctp
All of your models extend cake's AppModel class (which extends cake's Model class), no prefix needed. All controllers will extend Cake's AppController class - the class name is suffixed with Controller, and the file name is suffixed with _Controller.
You'll fine AppModel and AppController in /app, and they exist expressly for whatever app-wide custom methods / properties you may have. Since all of your models / controllers extend them, inheritance automatically disperses whatever properties / methods you place in them - for example, Auth. ^_^
But you can still name a table Models, or Controllers, or Views, or whatever, I guess. The $name property is an alias; you can create multiple instances of the same table in the same model by aliasing it with a different name. You can create models without tables, and you can switch between multiple tables - or databases, or servers - in a single model. You can also create non-database-type data objects (such as flat xml files) for your models. Dynamically named classes / methods ($$Model::save(), etc) are what's running under the hood anyway. I've done something similar in iterations for the sake of DRYing up my app and I didn't have a problem. (Although I personally doubt actually pulling off a local model named Model would be worth the effort you'd put into the experiment...)
And on that note, Cake's API spells out all it's classes their methods, etc. (generates off the comments in the codebase):
http://api13.cakephp.org/classes
HTH. :D
I know from experience you can't use table names like 'files' and 'models' because they create classes that are already used by Cake for other things such as File and Model.
I haven't come across any other problems like this but I'm sure they are there to be found.
I would suggest avoiding the use of any name used by cake core classes.
I know this is a bit of an old thread but just came across it searching for something else. I think this shoud help answer question #2. In database.php you can add your db table prefix in the DATABASE_CONFIG class /app/config/database.php. See the last key in the config array below:
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
);

CakePHP - What is the best approach to create an Admin Section

I am looking for an insight into the best approach to create an administrator section in CakePHP. I've looked at plugins like BrowniePHP as well as others, but I am not entirely satisfied with using plugins. So I am trying to create my own which will encompass the things I need. I;ve looked at some tutorials, but cant find the right answer.
I am currently creating a vast application, which is about 10% done, but I now feel the need to have an admin section before moving on.
Basically I would like a section where I can add new articles, approve comments, deny user access, etc. This section should only be accessible by an administrator.
Also, this administrator section must be able to save to any other model.
I am still learning CakePHP and any detailed instruction would be appreciated.
to create an admin-section the first thing you have to do is to manually edit the core.php within /app/config and write the setting Routing.prefixes. This line should be around line 88 somewhere and you just have to uncomment it.
In case you can't find it, it should look like this:
Configure::write('Routing.prefixes', array('admin'));
So now you can write your admin-functions within your controllers like this:
function admin_edit($id = null) {
//your admin function
}
You don't need access to every model since your writing these function within your controllers like every other "normal" action.
You just have to connect a route to handle the admin-actions:
Router::connect('/admin/:controller/:action/*', array('admin' => true, 'prefix' => 'admin', 'controller' => 'pages'));
// 'admin' => true is a variable for you so you can check if it's an admin-action which is requested
// 'prefix' => 'admin' means that you can write function with this prefix like above
You can then access these actions via the url http://yourapp.com/admin/controller/action
If you now use the Auth-Component you can write methods for checking if a user is allowed to access these methods.
For further information please read these manual-entrys:
Prefix-Routing
Authentication (Auth-Component)

Can You Access Model Data in Controller in CakePHP?

This is probably a dumb question, but I can't find a definitive answer anywhere. Is it possible to access model data in a Controller, and if so, how?
I tried the following:
$this->set('mydata', $this->Model->find('all', 'conditions' => array('id' => 'someID')));
And accessing it via this in the controller:
$mydata['Model']['field']
But that appears to only be for the views.
Is there a way to access the model data in the controller? The reason is that I need to perform calculations on an associated model (belongsTo) that can't be done via hidden fields or anything because the ID of the associated model isn't passed until after the form is submitted.
Any help would be greatly appreciated!
Hmm, how about:
$myData = $this->Model->find('all', 'conditions' => /* ... */);
$myData['Model']['field'];
$myData['RelatedModel']['field'];
$this->set('mydata', $myData);
Simple enough :)
Model::find() returns your data, you don't need to pass it directly to Controller::set(), you can mess with it first and then pass it on to your views.
But, I'd advise against it, it's better if you have a Model::messWithData($data) and let models deal with data, and let controllers take care of application logic. Remember, fat models, skinny controllers!

Resources