標題有點含糊我知道,但讓我解釋一下我試圖達到的目標。循環通過樹枝中的值並替換空值
我正在嘗試根據從Symfony2 CRM中的原則查詢中提取的數據生成CSV。檢索的數據基於OpenCart產品和屬性數據,以及一些與此問題無關的定製信息。
每個產品最多可以有5個不同的屬性值,分別命名爲A,B,D,L1和L2。但是,有些產品並不是全部,只有A,B和L1。 CSV要求每個屬性值位於單獨的單元格中 - 所以標題如下所示:
ATTRIBUTE:A |屬性:B |屬性:D | ATTRIBUTE:L1 | ATTRIBUTE:L2
然後我遍歷我的枝條文件,如下所示:
{% for attribute in row.product.attributes %}
{% if attribute.text is not null %}
{{ attribute.text }},
{% else %}na,{% endif %}
{% endfor %}
如果產品有各種屬性5,在CSV的結構是好的。但是,如果產品只有3個屬性,則意味着所有後續值都會被拉回一個單元格,這意味着其他數據位於錯誤的標題下。我第一次嘗試檢查值:
{% for attribute in row.product.attributes %}
{% if attribute.attributeName.name == "A" %}
{% if attribute.text is not null %}
{{ attribute.text }},
{% else %}na,{% endif %}
{% endif %}
{% endfor %}
而且我這樣做是對每個可能的屬性名稱,但unfortuantely這行不通,因爲如果名稱不存在,它只是跳過也無妨。如果試圖想通過這些屬性進行循環並在不存在的情況下輸入n/a,我無法想到 - 我確信有一種方法,但我不知道它是什麼。
作爲參考,這裏是正在生成的數據的CSV的控制器編碼:
public function adminCsvAction($filter) {
$repository = $this->getDoctrine()->getRepository('AppBundle:Project');
$stages_repository = $this->getDoctrine()->getRepository('AppBundle:Stage');
$users_repository = $this->getDoctrine()->getRepository('AppBundle:User');
$results = $repository->getSearchResults($filter);
$users = $users_repository->findAll();
$stages = $stages_repository->findBy(array('deleted' => 0), array('sortOrder' => 'ASC'));
$filename = "export_".date("Y_m_d_His").".csv";
$response = $this->render('AppBundle:pages:csv.html.twig', array('data' => $results,'users' => $users, 'stages' => $stages));
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename='.$filename);
return $response;
}
的Project
實體具有各種映射,其中一個鏈接到Opencart的產品表,這意味着所有的屬性和鏈接值可以通過這個訪問。
在這方面的任何幫助,非常感謝。
在樹枝編程(與php相比)可能會很痛苦。就我個人而言,我會寫一個接受產品數據作爲輸入的php函數,並返回一個csv就緒數組,其中包含所有五個插槽。 – Cerad
我同意Cerad,'枝'應該用於介紹,而不是修改 – DarkBee
你已經嘗試var_dumping每個變量?我有一個類似的問題,並var_dumped發現,翻譯成你的情況,我必須使用'attribute [0] .text'而不是'attribute.text' – Florian