2014-02-12 26 views
0

我已经尝试了很长一段时间才得到这个工作,但无法设法得到我真正想要的:)。PHP为什么我的父母在定义子女时没有设置

首先,我有一个“Scope”,其中包含“Projects”,其中包含“Tasks”,当我要显示范围的任务时,我想显示与该任务相关的项目。

这意味着当我启动:

$scope->get_Tasks(); 

它将检索项目,并为每个projets,检索任务,如果在这一点上我:

var_dump($scope); 

我看到从范围到项目,再到任务。因此,当我接手任务时,如何在不重新读取数据库的情况下抓住此项目的项目。我试图扩展项目的类任务,但是项目的每个成员都会在帖子结尾处结束“null”。

我有一个具有“GET_Tasks”功能一样,我的项目类:

/** 
* get_Tasks 
* 
* Returns the tasks associated with this project. 
*/ 
public function get_Tasks() 
{ 
    // If the tasks aren't already set 
    if (!(isset($this->_tasks))) 
    { 
     // We get the super object 
     $CI =& get_instance(); 
     // Build the conditions 
     $condition = array("ProjectID" => $this->get_ProjectID()); 
     // Get the tasks 
     $this->_tasks = _TaskClass::fetch($condition); 
    } 

    return $this->_tasks; 
} 

这里是我的“_TaskClass”类取功能现在

/** 
* fetch 
* 
* Fetch task(s) from the database. 
* 
* @param array The array of conditions we want. 
* @return array The array of objects that meets all of the conditions. 
*/ 
public static function fetch($conditions = NULL) 
{   
    // If we have conditions and that they're not in array, we return false 
    if (!($conditions === NULL) && (!(is_array($conditions)))) 
     return false; 
    // We get the super object 
    $CI =& get_instance(); 
    // Load the models needed 
    $CI->load->model('task_mod','',TRUE); 
    // Get the records 
    $array = $CI->task_mod->fetch($conditions)->result(); 
    // If we found records 
    if (count($array) > 0) 
    { 
     // Transform all of the records found as array 
     foreach ($array as $record) 
      $return_array[] = new _TaskClass($record); 

     // Return the list 
     return $return_array; 
    } 

    // We didnt find a record. 
    return false; 
} 

,在第一块码,如果不是

return $this->_tasks; 

我做的:

var_dump($this->_tasks); 

它给了我这个信息(注意,父根本没有设置):

array (size=2) 
    0 => 
    object(_TaskClass)[28] 
     public '_taskid' => string '20' (length=2) 
     public '_projectid' => string '1' (length=1) 
     public '_userid' => null 
     public '_title' => string '' (length=0) 
     public '_description' => string '' (length=0) 
     public '_user' => null 
     private '_scopeid' (_ProjectClass) => null 
     private '_tasks' (_ProjectClass) => null 
     private '_localisations' (_ProjectClass) => null 
     private '_projectid' (_ProjectClass) => null 
     private '_userid' (_ProjectClass) => null 

我想这是个具有构造去也许......也许是我”的东西试图达到这种方式并不是真的可以实现的吗?

非常感谢!

编辑1 这里的构造函数,它是最小的,我_TaskClass的

class _TaskClass extends _ProjectClass 
{ 
var $_taskid; 
var $_projectid; 
var $_userid; 
var $_title; 
var $_description; 

// Objects 
var $_user; 


public function __construct($params = NULL) 
{   
    parent::__construct(); 
    if (is_object($params)) 
     $this->_init_object($params); 
    else 
     return; 
} 
/** 
* _init_object 
* 
* Initialise the whole project. 
* 
* @param object the object containing all of the information of the project. 
*/ 
private function _init_object($project) 
{ 
    // Save all of the informations 
    $this->set_TaskID($project->TaskID); 
    $this->set_ProjectID($project->ProjectID); 
    $this->set_UserID($project->UserID); 
    $this->set_Title($project->Title); 
    $this->set_Description($project->Description); 
} 

编辑2 下面是我的项目类的构造函数

class _ProjectClass 
{ 
// Project variables 
private $_projectid; 
private $_scopeid; 
private $_userid; 
private $_tasks; 

private $_localisations = NULL; 

public function __construct($params = NULL) 
{   
    if (is_object($params)) 
     $this->_init_object($params); 
    else   
     return; 
} 
/** 
* _init_object 
* 
* Initialise the whole project. 
* 
* @param object the object containing all of the information of the project. 
*/ 
private function _init_object($project) 
{ 
    // Save all of the informations 
    $this->set_ProjectID($project->ProjectID); 
    $this->set_UserID($project->UserID); 
    $this->set_ScopeID($project->ScopeID); 
} 
+0

你可以发布你的任务类的构造函数代码? –

+0

@ ginovva320 - 发布我的构造函数 – SantaClauss

+0

您还应该将$ params传递给父构造函数。 –

回答

0

工程类

class _ProjectClass 
{ 
// Project variables 
private $_projectid; 
private $_scopeid; 
private $_userid; 
private $_tasks; 

private $_localisations = NULL; 

public function __construct($params = NULL) 
{   
    if (is_object($params)) 
     $this->_init_object($params); 
    else   
     return; 
} 

项目 - > get_Tasks()(注意$此变量)

/** 
* get_Tasks 
* 
* Returns the tasks associated with this project. 
*/ 
public function get_Tasks() 
{ 
    // If the tasks aren't already set 
    if (!(isset($this->_tasks))) 
    { 
     // We get the super object 
     $CI =& get_instance(); 
     // Build the conditions 
     $condition = array("ProjectID" => $this->get_ProjectID()); 
     // Get the tasks, we pass the object to set its parent 
     $this->_tasks = _TaskClass::fetch($condition, $this); 
    } 

    return $this->_tasks; 
} 

Task类(注意$ _project变量)

class _TaskClass 
{ 

var $_taskid; 
var $_projectid; 
var $_userid; 
var $_title; 
var $_description; 

// Objects 
var $_user; 
var $_project; 


public function __construct($params = NULL, $project = NULL) 
{ 
    // If the project is passed, set it 
    if (isset($project))   
     $this->_project = $project; 
    if (is_object($params)) 
     $this->_init_object($params); 
    else 
     return; 
} 

Task类 - > get_Project()

/** 
* get_Project 
* 
* Gets the project associated with the object. 
* 
* @return object the project. 
*/ 
public function get_Project() 
{ 
    // If the object isn't already set 
    if (!(isset($this->_project))) 
    { 
     // We get the super object 
     $CI =& get_instance(); 
     // Build the conditions 
     $condition = array("ProjectID" => $this->get_ProjectID()); 
     // Get the object 
     $this->_project = _ProjectClass::fetch($condition)[0]; 
    } 

    return $this->_project; 
} 

这种方式,我能够去访问我的项目从我的任务,而不必重新读取数据库,并宣布许多时间应该释放一点内存的同一个对象。

感谢大家分享你的想法和想法! :)

0

它给我这个信息(注意父母没有设置):

从外观上看,您正在实例化一个扩展父对象的Task对象,因此您不应该期望获得单独的任务/父对象,而是任务对象将包含以下成员(变量/方法)父对象。

$this->_tasks = _TaskClass::fetch($condition); 

// Which calls this 

$CI =& get_instance(); 

这是通过您发布的var_dump输出证明:

array (size=2) 
    0 => 
    object(_TaskClass)[28] 
     public '_taskid' => string '20' (length=2) 
     public '_projectid' => string '1' (length=1) 
     public '_userid' => null 
     public '_title' => string '' (length=0) 
     public '_description' => string '' (length=0) 
     public '_user' => null 

     // Notice the reference to _ProjectClass in the following 
     private '_scopeid' (_ProjectClass) => null 
     private '_tasks' (_ProjectClass) => null 
     private '_localisations' (_ProjectClass) => null 
     private '_projectid' (_ProjectClass) => null 
     private '_userid' (_ProjectClass) => null 
+0

我怎样才能设法从第一个项目,将有父设置的任务?...我想能够为例做... .. 。 $ task :: parent-> get_Title()。在返回数组时,它可能是带有byref(&)符号的东西吗? – SantaClauss

+0

Byref应该没有效果。你从数据库中提取这些数据吗? –

+0

从提取中,我从数据库中提取数据是。 – SantaClauss