我已经尝试了很长时间才能让这个工作起来但却无法得到我真正想要的东西:)。
首先,我有一个“范围”,其中包含“项目”,其中包含“任务”,当我要显示范围的任务时,我想显示与该任务相关的项目。 / p>
这意味着当我启动时:
$scope->get_Tasks();
它将检索项目,并为每个项目检索任务,如果在那时我:
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 这是我的Project类的构造函数
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)
它向我提供了这些信息(注意父母根本没有设置):
从它的外观来看,您正在实例化一个扩展父对象的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
答案 1 :(得分: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()(注意$ this变量)
/**
* 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;
}
任务类(注意$ _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;
}
任务类 - > 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;
}
通过这种方式,我可以从我的任务中访问我的项目,而无需重新读取数据库并且多次声明应该释放一些内存的相同对象。
感谢大家分享您的想法和想法! :)