PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] What does $this->Model1->Model2 accomplish in CakePHP HABTM?

 April 22, 2022     cakephp, cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

Basically, I've implemented the HABTM successfully in CakePHP, but the trouble is, I don't understand why it works.

The thing I hate about the CakePHP cookbook is that is tells you what to do but make very little effort to explain the underlying segments of their code.

Essentially, my data model is like this.

Task HABTM Question

I don't understand this code fragment.

$this->set('questions', $this->Task->Question->find('list'))

In particular, what is $this->Task->Question supposed to accomplish?

Also how is the above code link to this code fragment in the view?

echo $this->Form->input('Question'); 

One thing that is very peculiar is that with the above code fragment, I get a multiple select option.

However, if I change the code to this,

echo $this->Form->input('question');

I get a single select drop down list.

I scoured the entire documentation and still cannot find a satisfactory explanation to my doubts.

Would really appreciate if anyone can clarify this issue for me.


Solution

1. Model chaining

When a model has an association to another model (like in your example an HABTM one) then you can call methods of the associated model by chaining it to the current model. This is explained early in Associations and an example of exactly how it works is given at the end of the first section.

When you are someplace in your TasksController normally you would expect that only your Task model would be available. Instead any association described in the Task model is chained to that model in the form of $this->Model1->Model2.

So $this->set('questions', $this->Task->Question->find('list')) means:

From current model Task that you know about, access the associated model Question and then call its find('list') method. Then $this->set the results to the view as variable questions.

2. FormHelper Conventions

When you use a CamelCased single name for field input, like in $this->Form->input('Question'); you are saying to FormHelper that the data contained in the questions variable come from a model named Question with a HABTM association, therefore they should be handled as a multiple select (as HABTM points to such an association).

With a field name of model_id, like in this example question_id, you're asking for a single select (select a single id of the connected model).

With anything else, FormHelper looks at the field definition and takes the decision itself, but of course your can override any default behavior you want using options.

This is explained in detail and I'm surprised you missed both. CakePHP has one of the best documentations available, almost everything you need is there.



Answered By - user221931
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing