也许它看起来很基本,我忘记或遗失了什么?当我这样做时
class DefaultController extends Controller
{
public $mSelectedDepartment =0;
public $mDepartment;
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$mSelectedDepartment=$this->getSelectedDepartment();
$departments=$this->getDoctrine()->getRepository('AppBundle:departments')->findAll();
//create department links
for($i=0;$i<count($departments);$i++)
{
//generate simple link exp 'index?DepartmentId/1 '
$this->mDepartment[$i]['link_to_department']='index/DepartmentId/='; // my point is to fench [department_id]
}
// replace this example code with whatever you need
return $this->render('index/index.html.twig', [ 'department'=>$departments,'selectedDepartment'=>$mSelectedDepartment
]);
}
function getSelectedDepartment()
{
return $this->mSelectedDepartment;
}
}
然后转储变量dump($departments[$i])
,它显示我
departments {#528 ▼
-department_id: 1
-name: "Regional"
-discription: "Proud of your country? Wear a T-shirt with a national symbol stamp!"
}
在DefaultController.php第31行:
departments {#526 ▼
-department_id: 2
-name: "Nature"
-discription: "Find beautiful T-shirts with animals and flowers in our Nature department!"
}
在DefaultController.php第31行:
departments {#525 ▼
-department_id: 3
-name: "Seasonal"
-discription: "Each time of the year has a special flavor. Our seasonal T-shirts express traditional symbols using unique postal stamp pictures."
}
所以我认为它是阵列。但是为什么当我尝试访问
时dump($departments[$i]['department_id ']);
它说:
不能使用类型部门的对象作为数组
答案 0 :(得分:0)
你有一个对象,而不是一个数组,所以你必须做这样的事情
foreach ($departments as $department){
$my_department=(array) $department;
print_r($my_department);
}
答案 1 :(得分:0)
感谢继续尝试后我意识到部门_id属性是私有的,所以我改为公开
数组([0] =&gt; AppBundle \ Entity \ departments对象( [department_id:AppBundle \ Entity \ departments:private] =&gt; 1 [name:AppBundle \ Entity \ departments:private] =&gt;区域性 [描述:AppBundle \ Entity \ departments:private] =&gt;为你的骄傲 国家?穿上带有国家象征印章的T恤! )[1] =&gt; AppBundle \ Entity \ departments对象( [department_id:AppBundle \ Entity \ departments:private] =&gt; 2 [name:AppBundle \ Entity \ departments:private] =&gt;性质 [描述:AppBundle \ Entity \ departments:private] =&gt;找到美丽 我们自然部门的动物和鲜花T恤! )[2] =&gt; AppBundle \ Entity \ departments对象( [department_id:AppBundle \ Entity \ departments:private] =&gt; 3 [name:AppBundle \ Entity \ departments:private] =&gt;时令的 [描述:AppBundle \ Entity \ departments:private] =&gt;每一次 一年有一种特殊的味道。我们的季节性T恤表达传统 使用独特的邮政邮票图片的符号。 ))
按照我想要的方式修复
//generate simple link exp 'index?DepartmentId/1 '
$this->mDepartment[$i]['link_to_department']='index/DepartmentId/'.$departments[$i]->department_id;
现在新的问题是保存在实体上使用公共属性 之前的类属性感谢支持者
array:1 [▼
0 => array:1 [▼
"link_to_department" => "index/DepartmentId/1"
]
]
In DefaultController.php line 32:
array:2 [▼
0 => array:1 [▼
"link_to_department" => "index/DepartmentId/1"
]
1 => array:1 [▼
"link_to_department" => "index/DepartmentId/2"
]
]
In DefaultController.php line 32:
array:3 [▼
0 => array:1 [▼
"link_to_department" => "index/DepartmentId/1"
]
1 => array:1 [▼
"link_to_department" => "index/DepartmentId/2"
]
2 => array:1 [▼
"link_to_department" => "index/DepartmentId/3"