cakephp including two views on the home page - cakephp

I'm a cakephp and MCV newbie, so bear with me. I've been reading until my head exploded, but I'm not getting the big picture yet.
I have a TransactionsTable, TransactionsController with add() method, and an "add" view that outputs an "Add Transaction" form.
I have an AccountsTable, AccountsController with index() method, and an "index" view that outputs a list of Accounts.
What's the best way to include this form AND this list on the home page?

If you just want to add the list into your add view then your really don't need two views. Here's a simple way of doing what you want though: In your add() method you can get the list of accounts by loading the Accounts table, then just pass that to your view.
In add() :
$this->loadModel('Account');
$articles = $this->Account->find(//Add whatever you use to produce the list here);
$this->set('articles', $articles);
Then just display the list as you did in your accounts/index page. You're essentially combining two views into one.

Related

Cakephp - Load a view from another view in a different controller

I've been trying for the past 2 days, how to embed a view from a different controller.
I have two tables (hotels, hotelQuotes), related to each other. The hotels table is linked with a hasmany to the hotelQuotes table, and hotelQuotes with a belongsTo hotels table. Both are linked and working, they both show their related content with no problem.
The hotelQuotes it's pretty much a form, where users fill it out, and then it sends an Email with what the user filled out. Everything is working fine up to this point.
What I want is, when I show the information about the hotel, is to also show that form, I dont want the user to go to another page to fill out the form. So I pretty much want to embed the view of hotelQuotes/Add to the hotel/view.
I doing that using elements, as recommended last night in #cakephp IRC channel, and also here in StackOverFlow at: Embedding view in another view with CakePHP
The problem is that when I do that, the association between the two models gets broken. I mean, the users fill out the form, but I have nowhere to know where that form is coming from, what hotel is referring to. It just doesn't show that piece of vital information for me.
I've been reading about passing variables when using elements at: http://book.cakephp.org/2.0/en/views.html#passing-variables-into-an-element but I don't know how to pass a variable to a form select box. I am using CakePHP 2.5.2.
I hope I explain myself clear here, any input it would be appreciated. See picture below for more info:
http://i.stack.imgur.com/wcG64.jpg
Edit> I just realized that the form is not even submitting when I using elements. Why would that be?
Ok, I just figured out.
The form wasn't even submitting when calling it from elements. So I just passed the action myself like so:
echo $this->Form->create('Hotelquote', array(
'action' => 'add'
));
And now is working.

CAKEPHP - Including view into another view

I have a controller for posts and an element for comments. I want to include the posts view somehow in comment element. Is this possible to include the PostsController view into an element?
I know how to include elements but never heard or thought of including Controller views.
You can get the data for the PostsController view using requestAction. If you want to include view-layer code in two different places, pull it into an element - that's what they're for.

Best solutions for merging two controllers/views in CakePhp

I have two models:
Book
Category
I have two views:
books/browse
In this view there is paginated list of books, with sort options, book language select, author/title filters and so on. One thing there isn't - book category tree. In fact this controller takes as a parameter category_id and displays only books from this category.
categories/tree
This view (controller) finds the category tree and it takes one parameter -> category_id -> to determine how to print the tree (to show all parent nodes, and parents list and so on).
Now I need a view (for example browsetree) where there is category tree on the left and a list of books (with all stuff) on the right.
What will be best solution to accomplish this? (The problem is also I want to be able to save the link to book browse view with all it's params (category_id param + sorting + few GET params for filtering) and I also don't want to rewrite any view or controller method...
-------------- edit
Okay I have my tree in element
categorytree
and it looks like this:
$params = $this->requestAction('/categories/tree/'.$category_id);
print_category($params['tree'][0],$params['parents'],$category_id);
The $category_id is passed to the element as parameter.
This is inside /books/browse:
<?php echo $this->element('categorytree', array("category_id" => $category_id)); ?>
What I need is that in categorytree element I want that each tree node was a valid link to /books/browse based on current url that books/browse is loaded from.
I mean that the url might be:
books/browse/34/sort:asc/page:4?author=Author&something=something
I could get this link
inside /books/browse/...
view and pass it to the element and then parse it to replace /34/ (it's category id) with other categories... But is there any easier / more elegant way of doing this?
Put your category tree in an element and use CakePHP's requestAction to get it's data from the Category controller.
You can keep your tree view in the Books area (which makes sense, since that's what the user is browsing) - up to you which view you put it in.
Bottom line, the user is searching books, so that's where the view and the code should be. Use the Category tree to just pass a variable for which category you want to use in your query against the books.

Count number of posts in cakephp

I'm trying to create a menu in cake php where I can also know how many articles are inside the section, should I use a manual query, or does exist some existing method to do it?
My site menu:
- Works (12)
- Photos (35)
- Stuff (7)
- Contacts
My problem is also I didn't get how I can access to data like this for every view, this should be a main menu, so I should use this in every view but If i put it in default.ctp, every model deosn't exist, because I cannot access it from a view.
Does exist some page which talks about this?
Since those are separate models that are not related to each other, you'll need to do a manual count.
$this->Model->find('count');
EDIT
Ok, so looks like you are talking about different models.
If this is used in a menu, that means it will be shown in all pages.
You have two ways of doing this.
You can do it by having an AppController for you application. Basically, you can put this code in the beforeRender method so it runs everytime your a request is rendered
function beforeRender() {
App::import('Model', array('Work', 'Photo', 'Stuff'));
$work = new Work();
$workCount = $work->find('count');
//do the same for the other
$this->set('workCount', $workCount);
}
Have a look at this for more details on callbacks : http://book.cakephp.org/view/977/Controller-Methods#Callbacks-984
Secondly, you can do this via a helper. You can put the same code (that is inside the bforeRender) into a helper, and you can call the helper method.
You can look here for more info on creating a helper : http://book.cakephp.org/view/1097/Creating-Helpers
The CounterCache behavior will help you out:
http://book.cakephp.org/view/1033/counterCache-Cache-your-count

cakePHP: how to combine two or more application views on one cakePHP layout page?

Using cakePHP my goal is to combine the index view of two or more controllers in one layout page.
Example:
I have controllers for: news, events, links.
I want to show the last five entries from each table in one layout page.
Also, when one of the links from the views is selected it should take the user to the respective view for that record.
I have read through the books section on views but don't see how making a view into an element would accomplish this.
What confuses me is how to combine from three separate controller/views into one layout?
Thanks
Create methods in your News, Event and Link models for fetching the last 5 records. Then in your controller either include the models in the Controller::uses property, or in the action use ClassRegistry::init() to get access to the model, e.g.
function my_action() {
$news = ClassRegistry::init('News')->getRecent();
$events = ClassRegistry::init('Event')->getRecent();
$links = ClassRegistry::init('Link')->getRecent();
$this->set(compact('news', 'events', 'links'));
}
You can then call these model methods from any controller action, keeping your application DRY.
In your my_action.ctp view, and indeed many other views, just render the elements e.g.
// my_action.ctp
<?php
echo $this->element('recent_news');
echo $this->element('recent_events');
echo $this->element('recent_links');
?>
Your elements can then just iterate over the $news (or whatever) variable displaying the items with links to the 'view' actions in their respective controllers.
Just because a controller matches a model, doesn't mean you can't use other models in it.
First I would say that views and controllers are not necessarily tied together -- Cake will implicitly add the view specified by the file heirarchy / naming convention, but this doesn't necessarily have to be the case. So try to think of the views as decoupled from the controller (which is one of the main purposes for using the MVC architecture).
Assuming your three views (A,B,C) are exactly how you want them copied, put them into an element (which is just a view file located in the special APP/views/elements/ directory). Now you can use them in either layouts or other views, just by making a call to $this->element( 'elementName', array( 'options' ) ).
Basically, just abstract the code you want to display into elements, then insert those elements into the desired layouts.
You can set up your controller to use the RequestHandler and then make your view elements capable of fetching their own data from separate controllers in your application.
This link explains it better than I can
http://bakery.cakephp.org/articles/view/creating-reusable-elements-with-requestaction
One thing to keep in mind is that the controller actions you are calling should account for caching their own data so you don't do unnecessary database queries.

Resources