cakephp: blog tutorial tutorial extended - cakephp

I am trying to extend the simple blog tutorial found in the documentation.
I have an index.ctp on /app/View/Users/index.ctp with all users being shown with a link on each username.... When i click on the username, I expect it to go to the view.ctp and show each user's blog posts and the number of posts each user has done.
I get an error here. Do I need to write a component for this? If so, how do I go about it?
class UsersController extends AppController {
public function view($id = null) {
$this->set('allposts',$this->Post->findByUserId($id));
}
}
So I get an error at this point because it cannot see the Post model in the user model. How do I go around this?
Thanks....

No, you don't need a component for that. If you have linked your models correctly (that's it, set belongsTos and hasMany(es) etc etc), then you have to access the post by concatenating.
$this->set('allposts',$this->User->Post->findByUserId($id));
Please see other questions regarding this.
Now, extending this a little bit since you seem a bit confused on what Components actually do. Components are a way to extending or adding functionalities to Controllers. If you want to, say, create a new Paginator, or call Facebook from your controller to get info, and there's no definite place to put that (users can have facebook info, but if you have another controller named "Friends", you might have to put some facebook functions there too).
Normally, if you think you need a component to have calls from one model to another, that's a sign you are organizing things wrongly (not a general rule, I'm open to examples on when this isn't true).

Related

wagtail modeladmin: is it possible to add "explore child pages" column?

I've been using wagtail-modeladmin to create custom lists of certain page types. That gives me the ability to edit those pages. But I'd also like to be able to click through somehow to the "normal" admin explorer version of those pages, and be able to view/add child pages to them.
essentially giving myself a column with the little arrows in on the right, just like in the normal wagtail admin page explorer...
OK I know it's bad form to answer your own question, but I've got this working by using a custom method on the model admin object that reverse wagtails admin urls:
class MySpecialPageModelAdmin(ModelAdmin):
def view_children(self, obj):
url = reverse('wagtailadmin_explore', args=[obj.id])
return format_html(f'View Children')
list_display = ('title', 'live', 'view_children')
but actually, I think I'm not going to end up using this, just replacing this particular modeladmin with a direct link to the right place in the explorer.

Independent views or different methods for a page?

I have an application that the unique thing that uses Backbone is a navigator that highlights where the user are. I mean, it's a "breadcrumb" to guide him to locate himself.
But now, I'm implementing internationalization and I need to identify which language the user is navigating in – and I would to do that with Backbone as well.
Until now, my view is this:
var breadcrumbView = new BreadcrumbView({
scope: '#{#scope}'
});
Then I ask: it's better to create another view just for the internationalization fashion or the right thing to do is aggregate this two resources into a HomeView? E.g.
var home = new HomeView;
home.Breadcrumb.create('#{#scope}');
home.Internationalization.create();
I found the right solution. Let's go?
Understanding the real problem
The meaning of "internationalization" in my question refers to highlighting which language the user is using. Let's suppose that the "current" language is en-US, then we highlight it in the menu of languages.
Extracting the solution
Firstly, I searched on Wikipedia the follow explanation about views:
A view is told by the controller all the information it needs for generating an output representation to the user. It can also provide generic mechanisms to inform the controller of user input.
Source
Secondly, I know that internationalization and breadcrumbs are components of my view. Of my view. You see? These two "features" aren't the view itselves – they're just components that compose it.
Implementing the solution
Based on the knowledge we acquired, is simple to say that internationalization and breadcrumbs should to be methods of my HomeView.
Translating this into code, this is the right thing to do:
var home = new HomeView;
home.Breadcrumb.create('#{#scope}');
home.Internationalization.create();

CakePHP a class that can be used anywhere?

I have a multi-domain site. Depending on the domain the site needs to behave accordingly.
I created a helper called CompanyInfo it has methods such as name(), phone(), email(), etc.
Depending on what domain you are on it returns the correct information.
So for example if I need to display the phone number for a user to call I would use $this->CompanyInfo->phone() and it will display the correct phone number for the user depending on the domain.
Ok, this is all good, but not really relevant. The real issue is, I need this information in more than just the view. Helpers are just for views though. If I want to access this information from a controller I need to create a component to do it.
I really don't want to have a Helper and a Component doing the same thing. I would rather have one class handle it rather than copy and paste logic.
So whats the best way to have a class with methods that can be accessed from the controller or view or even model?
Is it just this kind of static information (name, phonenumber, email, etc) what you need to display? Why not just add them to your configuration in core.php?
Something like
# in core.php
Configuration::write('Company.name', 'Acme Corp.');
Configuration::write('Company.email', 'joe#acme.com');
You can then get this info anywhere you need using
Configuration::read('Company.name');
You can define this variable in your app_controller and then use these variable easily in any of your controller as set these variables from there only.
Call this function in your construct class.
i think that will solve your problem.
You can access model classes from any place this way :
$companyInfoModel = ClassRegistry::init('CompanyInfo');
$phone = $companyInfoModel->phone();
a) you can use libs in cake1.3 for that
b) static model methods which you can pass the content to and which will return the expected value
echo Model::phone($data)

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

How to handle form data in cakephp

i have a form for adding people object ,i want to add as many people without saving people in database,after clicking add people form submit button,the same form should appear without saving the form data to database,instead it should save to the session.please explain in detail if possible with the help of a example to do this, i have not found any tutorial which explains cakephp session in detail.
Vinay,
check out the official examples.
But you should definitly start with the easy tutorials here and dissect these. You will learn form handling and later can put it together with the sessions.
Sessions are simple. You include the component, write something to the session and read it from the session later. That's it. You can store in it almost anything you want, including arrays.
class FooController extends AppController {
public $components = array('Session');
public function foo() {
$this->Session->write('some.key', 'some value');
}
public function bar() {
$baz = $this->Session->read('some.key');
}
}
See http://book.cakephp.org/view/1311/Methods

Resources