Reputation: 478
i'm new to cakePHP and OO programation, this question will seem VERY obvious to all of you, but i'll ask it anyway :
I have a Model, called 'Dossier', that gets it's data from an SQL table containing various fields, of of which is a int(1) (boolean) type called 'bin_valide'.
I wanted to write a simple function that just checks the value of this field and display true or false in the View part (index.ctp)
where do i declare such function (that will look like something like this, i put this code in the model Dossier.php page, but maybe it should go in DossierController.php ... (not sure here)
public function estClos() {
if ($this->Dossier->field('bin_valide')==0) { return $this->true ; }
else return $this->false;
}
how can i call this function ? (i tried something like this in the index.ctp View page:
echo $dossier['Dossier']['commentaire'];
echo $dossier->estClos();
I know this seems very easy, but i just can't figure out where i should decalre such method, and how i am supposed to access it. Thank you very much.
Upvotes: 0
Views: 90
Reputation: 25698
Instead of trying to glue pieces together I recommend you to start by getting a basic understanding of MVC and CakePHP.
It is required that you understand the MVC design pattern. Check this link about design patterns in general and this link about MVC. And this github repo has examples of different patterns in php.
Also start by doing the CakePHP blog tutorial instead of trying and unstructured trial and error approach by doing something without having read about the basic.
Your controller is named wrong. Controllers should be named plural, so it should be DossiersController. Models are always singular. You should follow the CakePHP coding standards. If you don't follow them some of the automagic won't work.
All data manipulation and fetching should go into the model
public function getStatus() {
return $this->Dossier->field('bin_valide');
}
Your controller acts like a manager getting the data from the model and returning it to the view. It basically handles just your request. Models are also easier to test than controllers and the basic rule is that you want fat models and skinny controllers. The controller will just set the data to the view:
public function status() {
$this->set('status', $this->Dossier->getStatus());
}
In your view simply do something with it:
if ($status === 1) { echo 'Yes'; } else { echo 'No'; }
Do yourself a favor and do the blog tutorial first, it will give you a basic insight into CakePHP and prevent frustration from unstructured trial and error attempts. :)
Upvotes: 1
Reputation: 34877
You shouldn't call any functions in your View, the Controller is the place to prepare all data you want to use in your view. In this case, I'd just cast the integer that should be in your dataset as boolean in your view.
First of all, fetch all the entries from the dossiers
table (or add conditions to it if needed):
// app/Controller/DossiersController.php
public function index() {
// Find all the dossiers
$dossiers = $this->Dossier->find('all');
// Pass the found dossiers to your view
$this->set(compact('dossiers'));
}
Then you will get an array that looks something like:
0 => array( // First dossier
'Dossier' => array(
'id' => 1,
'name' => 'Some dossier',
'bin_valide' => 0
)
),
1 => array( // Second dossier
'Dossier' => array(
'id' => 2,
'name' => 'Another dossier',
'bin_valide' => 1
)
)
In your View you can then simply loop over the dossiers:
// app/View/Dossiers/index.ctp
foreach ($dossiers as $dossier):
echo '<p>The name of the dossier is: ' . $dossier['Dossier']['name'] . '</p>';
echo '<p>Valid: ' . (bool)$dossier['Dossier']['bin_valide'] . '</p>';
endforeach;
This will print:
The name of the dossier is: Some dossier
Valid: false
The name of the dossier is: Another dossier
Valid: true
Upvotes: 0
Reputation: 787
pass a variable to your view, and you can reference it from there on.
public function view_action() {
$value = false;
if ($this->Dossier->field('bin_valide')==0) { $value = true; }
$this->set('myValue', $value);
}
you can then reference myValue variable in your view.
Upvotes: 0