My problem it’s very simple but I can’t use any model other than the model associated with my controller.
I have plants, they are in a greenhouse. I would like to display the plants according to the greenhouse each time but I cannot call the plants in my controller (greenhouse).
Please, how to make this? :’(
Assuming that the tables are associated, you could use something like
$this->Greenhouses->Plants. Or load an additional model. Or use the TableRegistry. Or consider that perhaps if your goal is to display plants, that function should actually be in the Plants controller, not the Greenhouses one.
Sorry, i don’ understand.
I will explain my problem.
I have my greenhouse contoller, I have my plant controller and I would like to display the plants corresponding to the greenhouse after clicking on the greenhouse. But I don’t know how to set this up. CakePhP is my first framework and must say that I am having a lot of trouble.
This is Greenhouse table
This is plants table :
Please, help me…
Have you gone through the tutorial? This seems very much like the relations that it takes you through. Also, have you used the
bake command? That should generate much of this code for you, though it seems that your naming scheme may not be standard (e.g. I don’t know if bake works with upper case letters in column names; it very well may, but most everyone that uses Cake uses all lower case column and table names), so that might hinder it somewhat.
this is extremely simple in Cake.
Define the relation between Greenhouse and Plant model in the Greenhouse model;
public $hasMany = array(
‘Plant’ => array(
‘className’ => ‘Plant’,
‘foreignKey’ => ‘greenhouse_id’
Don’t forget to add the field greenhouse_id to the plants table and fill with 1 some records.
In GreenhpusesControler write this:
$greanhouse = $this->Greenhause->find(‘first’, array(
‘conditions’ => array(
‘Greenhause.id’ => 1,
‘contain’ => array(
you obtan an array with Greanhouse where id=1 and ALL the Plant(s) used in this (those record that you filled with 1).
replace the field named id_Users with user_id
replace the field named id_Serres with maybe serre_id (model name must to be singular)
the example is in cakephp 2 because is the fastest and the best, but the principle is the same for all versions.
Pls. don’t forget, the power of Cake is contain!.