我有一个基于的嵌套关联模型
Project=>ProjectDocument=>ProjectDocumentSection=>ProjectDocumentSectionVariable=>Codeset=>Code
我正在使用可包含的行为来获取包含这些元素的特定数组(对于单个项目)。
我已经在它们各自的模型中为这些模型建立了关联。
使用 foreach 我可以查询所有数据,包括ProjectDocumentSectionVariable level.
但是,一旦我尝试进入 Codeset 级别,就会出现错误。
相关代码是:
foreach ($project['ProjectDocument'] as $project_document):
if ($project_document['document_type'] == 1)
{
foreach ($project_document['ProjectDocumentSection'] as $project_document_section):
echo "<h4><a href=\"P4\">" . $project_document_section['title']. "</a></h4><div><table><th>Variable</th><th>Full text</th><th>Type</th><th>Format</th><th>Codeset</th>" ;
foreach ($project_document_section['ProjectDocumentSectionVariable'] as $project_document_section_var):
echo "<tr><td>" . $project_document_section_var['variable_name']
. "</td><td>".$project_document_section_var['variable_description']
. "</td><td>".$project_document_section_var['variable_type']
."</td><td>".$project_document_section_var['variable_format']
."</td><td>".$project_document_section_var['codeset_id'] ;
foreach($project_document_section_var['Codeset'] as $codeset):
foreach($codeset['Code'] as $pcode):
echo $pcode['codevalue'].", ";
endforeach;
endforeach;
echo "</td></tr>";
endforeach;
echo "</table></div>";
endforeach;
}
endforeach;
foreach()
但我得到非法字符串偏移“代码”,并且在回显行提供了无效参数$pcode['codevalue'].", ";
我查看了数组$project_document_section_variable
,我认为代码集数组存在问题,但我无法弄清楚它为什么会这样做。
以下是数组的一些输出示例$project_document_section_variable
:
array(
'id' => '32',
'project_id' => '05a',
'project_document_id' => '3',
'project_document_section_id' => '2',
'variable_id' => '2',
'variable_name' => 'Respondent Education',
'variable_description' => 'B1. What is your highest level of education?',
'variable_type' => 'Categorical',
'variable_format' => 'Quantitive',
'codeset_id' => '43',
'Codeset' => array(
'id' => '43',
'title' => 'Educational level',
'Code' => array(
(int) 0 => array(
'id' => '96',
'codeset_id' => '43',
'codevalue' => '1',
'title' => 'No education'
),
(int) 1 => array(
'id' => '97',
'codeset_id' => '43',
'codevalue' => '2',
'title' => 'Primary'
),
(int) 2 => array(
'id' => '98',
'codeset_id' => '43',
'codevalue' => '3',
'title' => 'Secondary'
),
(int) 3 => array(
'id' => '99',
'codeset_id' => '43',
'codevalue' => '4',
'title' => 'Tertiary'
),
(int) 4 => array(
'id' => '100',
'codeset_id' => '43',
'codevalue' => '7',
'title' => 'Other (describe)'
),
(int) 5 => array(
'id' => '101',
'codeset_id' => '43',
'codevalue' => '8',
'title' => 'Refuses to answer'
),
(int) 6 => array(
'id' => '102',
'codeset_id' => '43',
'codevalue' => '9',
'title' => 'Don’t know'
)
)
)
)
您可能会注意到代码集数组没有(int) 0
. 也许这是因为只为代码集返回了一个数组(这是正确的,每个数组project_document_section_variable
只有一个关联的代码集。
我在这里遗漏了什么明显的东西吗?