Hmm, now that I'm actually trying to apply this example to a DataObject which has a nested DataObject inside it, I can't manage to get to the finish line. I'm stuck on the final lap, I think :)
I've got the call to another function (QuestionAuthor) setting the value of a property in the DataObject being returned and it looks like this:
function LatestQuestions($iNum = 10) {
$oQuestions = DataObject::get("Question", "", "Created DESC", "", $iNum);
foreach ($oQuestions as $oQuestion => $data) {
$data->Author = array($this->QuestionAuthor($data->Author));
$data->Answers = $this->NumAnswers($data->ID);
}
return $oQuestions;
}
function QuestionAuthor($iAuthorID) {
$oAuthor = DataObject::get_by_id("Member", (int) $iAuthorID);
$dsoAuthor = new DataObjectSet();
$dsoAuthor->push(new DataObject(array(
'Avatar' => $oAuthor->data()->AvatarPath,
'Name' => $oAuthor->data()->FirstName." ".$oAuthor->data()->Surname,
'Occupation' => $oAuthor->data()->Occupation
)));
return $dsoAuthor;
}
And then this is what I have in my template:
<ul id="questionsList">
<% control LatestQuestions(5) %>
<li>
<div class="author">
<% control Author %>
<img src="$Avatar" width="35" height="35" alt="" />
<p>
<span class="name">$Name</span><br />
<span class="title">$Occupation</span>
</p>
<% end_control %>
</div>
<p class="question">
<a href="questions/$ID/$Title" class="title">$Title</a><br />
<span>posted on $Created.format(d F Y) ($Answers responses)</span>
</p>
<a href="questions/$ID/$Title#AnswerForm" class="button">respond »</a>
</li>
<% end_control %>
</ul>
So what my problem is, is that I can't seem to access the variables set inside the nested control ($Name, $Occupation and $Avatar). I've been var_dump'ing the complete DataObjectSet returned by the parent control and everything seems to be in place (here is an excerpt with some irrelevant stuff filtered out for readability):
object(DataObjectSet)#70 (16) {
array(5) {
[0]=>
object(Question)#71 (15) {
array(11) {
["ClassName"]=> string(8) "Question"
["Created"]=> string(19) "2010-06-18 14:22:35"
["LastEdited"]=> string(19) "2010-06-18 14:22:35"
["Title"]=> string(12) "A test entry"
["Author"]=> array(1) {
[0]=>
object(DataObject)#79 (15) {
array(3) {
["Avatar"]=> string(32) "assets/Uploads/av/testavatar.jpg"
["Name"]=> string(20) "Lonneke Ceelie"
["Occupation"]=> string(12) "Editor"
}
}
}
["Content"]=> string(50) "Lorem ipsum test test test"
["QuestionHolderID"]=> string(2) "30"
["DocumentID"]=> string(1) "0"
["ID"]=> int(6)
["RecordClassName"]=> string(8) "Question"
["Answers"]=> int(5)
}
}
}
}
Yet in my nested control, I can't seem to access the three variables I want to. I've tried a few different things, like the second function returning (instead of a DataObject as above) a plain Array, an ArrayData object and a DataObjectSet, but the results are similar to the above each time. I also tried i.e. $Author.Occupation in the nested control, but that accomplishes nothing either.
The only time I can get anything to print onscreen is if I remove the <% control Author %> and replace it with $Author, which prints out "Array" onscreen for me.
Am I missing something completely obvious here? Any pointers would be much appreciated :)