Bonjour,
Je suis nouvelle en informatique et j'utilise le framework cakephp3
je commence par expliquer mon modéle:
un programme contient de 1_n projets
un projet contient de 1_n tâches.
ce que j'aimerais faire, est de selectionner une tâche en fonction du projet auquel elle appartient, je m'explique, dans le premier champs du formulaire je selectionne le "projetA" par exemple, et dans le champ de la selection de la tâche, je ne voudrais avoir que les tâches appartenantes au "projetA".
Quelqu'un aurait des pistes SVP? J'ai beau cherché sur le net, mais je n arrive pas à résoudre le problème. Merci à vous ; je vous serais vraiment reconnaissante.
// modele ProjectTable.php
<?php
namespace App\Model\Table;
use App\Model\Entity\Project;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Project Model
*
* @property \Cake\ORM\Association\BelongsTo $Program
* @property \Cake\ORM\Association\HasMany $TaskActivity
*/
class ProjectTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('project');
$this->displayField('project_name');
$this->primaryKey('IDproject');
$this->belongsTo('Program', [
'foreignKey' => 'program_id'
]);
$this->hasMany('TaskActivity', [
'foreignKey' => 'project_id'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('IDproject')
->notEmpty('IDproject', 'create');
$validator
->notEmpty('project_name');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['program_id'], 'Program'));
$rules->add($rules->isUnique(['project_name']));
return $rules;
}
}
// le modele ProjectTabel.php
<?php
namespace App\Model\Table;
use App\Model\Entity\TaskActivity;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* TaskActivity Model
*
* @property \Cake\ORM\Association\BelongsTo $Project
*/
class TaskActivityTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('task_activity');
$this->displayField('task_activity_name');
$this->primaryKey('IDtask_activity');
$this->belongsTo('Project', [
'foreignKey' => 'project_id'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('IDtask_activity')
->notEmpty('IDtask_activity', 'create');
$validator
->notEmpty('task_activity_name');
$validator
->notEmpty ('project_id')
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['project_id'], 'Project'))
//le nom de la tâche doit être unique
$rules->add($rules->isUnique(['task_activity_name']));
return $rules;
}
}
Tu as juste à récupérer les tâches en fonction de l'ID du projet...
$project = $this->Projects->get($id);
$taks = $this->Projects->Tasks->find('all', [
'conditions' => ['Tasks.project_id' => $project->id]
]);