Mirad tengo las siguientes clases
ProjectClass
Código:
TaskClass<?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * Description of ProjectClass * * @author alex */ class ProjectClass { private $groupId; private $name; private $projectId; private $tasks = array();//array of main tasks private $taskIdPadre; private $hadChilds; public function __construct($opcion,$var1,$var2){ if($opcion =="CreariDProject"){$this->CreariDProject($var1); $this->loadTasks(); } } public function CreariDProject($var1){ $this->projectId = $var1; } public function CrearProject($var1){ $this->name = $var1; } public function Asignar($var1,$var2){ $this->projectId = $var1; $this->groupId = $var2; } public function getProjectId(){ return $this->projectId; } public function getGroupId(){ return $this->groupId; } public function getArrayOfTask(){ return $this->tasks; } /* public function setTaskIdPadre($taskIdPadre){ return $this->taskIdPadre=$taskIdPadre; } public function getTaskIdPadre(){ return $this->taskIdPadre; }*/ public function createMainTask($taskIdPadre,$parentId,$name, $iniDate, $endDate, $desc){ // create a new object(rojectid(atuomatico) taskid name initialdate enddate desc parentid(asignar)) // $this->setTaskIdPadre($taskIdPadre); // Compruevo si tiene hijas el: array sin padre $bd = $_SESSION['bd']; $dataset = $bd->loadTask($taskIdPadre,$this->projectId); while ($registro = mysql_fetch_array($dataset)) {// creo que tengo k contar los hijos $hadChilds = true; } if($hadChilds){ $TaskClass = new TaskClass($parentId,$name, $iniDate, $endDate, $desc,$taskIdPadre); $this->tasks[] = $TaskClass;} else{ $TaskClass = new TaskClass($parentId,$name, $iniDate, $endDate, $desc,false); $this->tasks[] = $TaskClass; } } private function loadTasks(){ // load all tasks of the project // Cargo de mysql a l'array de objctsTaskClass .a on parentid=0 osea el padre sin padre. $bd = $_SESSION['bd']; $dataset = $bd->loadMainTasks($this->projectId); while ($registro = mysql_fetch_array($dataset)) { if($registro['parentid'] =="0"){ $this->createMainTask($registro['taskid'],0,$registro['name'], $registro['initialdate'], $registro['enddate'], $registro['desc']); } } } /*private function LoadArrayConPadre($taskIdPadre){ $bd = $_SESSION['bd']; // Cargo de mysql a l'array de objctsTaskClass .a on parentid=x osea el padre con padre/s. $dataset = $bd->loadTask($taskIdPadre,$this->projectId); $TaskClass = new TaskClass(1,2,3,4,5); while ($registro = mysql_fetch_array($dataset)) { // if($SESSION['parentid'] =="0") $TaskClass->addChild($registro['parentid'],$registro['name'],$registro['initialdate'], $registro['enddate'], $registro['desc'],$this->LoadArrayConPadre($taskIdPadre)); } }*/ public function deleteTask($taskId){ // only mark for remove the task selected. // if the task has childs then, are direct sons of his grandfather. } public function deleteBranch($taskId){ // mark for remove the task selected and all of his sons } public function addWorker($taskId, $userId){ //add a person as a worker in that task } public function deleteWorker($taskId, $userId){ // delete a worker from that task } public function saveChanges(){ //saves the project. In fact, it only saves the tasks and his workers } } ?>
Código:
EN projectClass hago lo siguiente;class TaskClass { private $hasChanged;// notify if the task has been change private $deleted;// notify if the task has been removed private $childsDeleted; //notify if the task has been removed so has been his children //put your properties here, ex: childs, inidate, endate... //projectid taskid name initialdate enddate desc parentid private $taskId; private $name; private $iniDate; private $endDate; private $desc; private $parentId; private $childs = array();// Array de hijos private $numChilds= 0; private $padreId; function __construct($parentId, $name, $iniDate, $endDate, $desc, $padreId) { $this->parentId = $parentId; $this->name = $name; $this->iniDate = $iniDate; $this->endDate = $endDate; $this->desc = $desc; $this->padreId = $padreId; if ($padreId != false) { $this->loadTask($this->padreId); } } function loadTask($padreId){ $bd = $_SESSION['bd']; $dataset = $bd->loadTask($padreId,$_SESSION['projectId']); while ($registro = mysql_fetch_array($dataset) && $this->numChilds<2) { // if($SESSION['parentid'] =="0") $this->numChilds++; echo "TaskClass->LoadTask<br>".$this->numChilds; $this->addChild($registro['parentid'],$registro['name'],$registro['initialdate'], $registro['enddate'], $registro['desc']); } } /* public function createChild($parentId,$name, $iniDate, $endDate, $desc){ // create a new object * if($registro['parentid'] !="0"){ // Cargo de mysql a l'array de objctsTaskClass .a on parentid=x osea el padre con padre/s. $datasetLoadTask = $bd->loadTask($taskIdPadre,$this->projectId); while ($registro2 = mysql_fetch_array($datasetLoadTask)) { // if($SESSION['parentid'] =="0") $TaskClass->addChild($registro['parentid'],$registro['name'],$registro['initialdate'], $registro['enddate'], $registro['desc'],$this->LoadArrayConPadre($taskIdPadre)); } } }*/ function addChild($parentId,$name, $iniDate, $endDate, $desc){ //adds a subtask //hijo/s de array sin padre $bd = $_SESSION['bd']; $dataset = $bd->loadTask($this->padreId, $_SESSION['projectId']); while ($registro = mysql_fetch_array($dataset)) {// creo que tengo k contar los hijos $this->padreId = true; } if($this->padreId){ $TaskClass = new TaskClass($parentId,$name, $iniDate, $endDate, $desc,$this->padreId); $this->childs[] = $TaskClass; } else{ $TaskClass = new TaskClass($parentId,$name, $iniDate, $endDate, $desc,false); $this->childs[] = $TaskClass; } } public function getNameProject(){ return $this->name; } function getArrayOfChilds(){ return $this->childs; } function getNumChilds(){ return $this->numChilds; } function removeAll(){ //remove all task includes himself $this->numChilds=0; return numChilds; } function getParentId(){ //return parent task id return $this->parentId; } function getChild($taskId){ //return (could be a direct or non direct child) foreach ($this->childs as $value) { } $this->childs[$i]; } function addWorker($userId){ // workers MUST be users that belongs to groupId } function deleteWorker($userId){ // delete a worker from that task } function save(){ //save this object and his workers to BD throught BDController //and save all childs //if one task is mark for erased, then must be erase from BD } }
en loadTasks hago;
// returns a list of task that belongs to the project and has no parents.
depués en createMainTask hago;
miro si tiene hijas ese padre sin padre, si tiene mando el id de este padre a TaskClass.
Aguí hago el mismo proceso pero al crear obj task no estoy usando el array para esos objetos prk en cada new taskclass dentro de taskclass, creo un nuevo
obj tipo TaskClass con su array de child[] que es el array del nuevo obj.
Bien lo que quiero es; 1) tener un array en TaskClass de obj de su misma clase ó 2)que atraves del array task[] creada en ProjectClass pueda acceder a cada al array de childs de TaskClass q esta a su vez tendrá su array de childs(en funcion de los hijos que tenga esa tarea) el mismo tema para la primera opción, también.
La primera opción creo que es la más elegante ya que es, lo que me esta diciendo el diagrama de clases
Y otra duda que tengo, los obj creados en taskClass, donde están? hay alguna manera de acceder a ellos? xD