我正在尝试从数据库doctrine查询迭代数组。获取id字段的值后,将其设置为方法的返回类型值。在获取端点uri时,它返回状态200 - 响应为空
这是我的尝试。
/**
* @Route("/get/{email}")
*/
public function emailAction($email)
{
$var = "";
$singleresult = $this->getDoctrine()->getRepository('Api3Bundle:Influ')->findOneBy(array('email' => $email));
if ($singleresult === null) {
return new View("user not found", Response::HTTP_NOT_FOUND);
}
//area of problem
foreach($singleresult as $users)
{
$var = $users.id;
}
return $var;
//end of area of problem
}
我正在尝试使用电子邮件返回ID。上述尝试通过邮递员显示响应为空
答案 0 :(得分:0)
如果 private static String url_all_customers = "http://developer-api.bringg.com/partner_api/customers";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CUSTOMERS = "customers";
private static final String TAG_CID = "cid";
private static final String TAG_NAME = "name";
// products JSONArray
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_customers);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading customers in Background Thread
new LoadAllCustomers().execute();
// Get listview
ListView lv = getListView();
// on seleting single customers
// launching Edit Customers Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.cid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditProductActivity.class);
// sending pid to next activity
in.putExtra(TAG_CID, cid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
/**
* Background Async Task to Load all custormer by making HTTP Request
* */
class LoadAllCustormers extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllCustormersActivity.this);
pDialog.setMessage("Loading custormers. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_customers, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Customers: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Customers
customers = json.getJSONArray(TAG_CUSTOMER);
// looping through All Customers
for (int i = 0; i < customers.length(); i++) {
JSONObject c = customers.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_CID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no customers found
// Launch Add New customer Activity
Intent i = new Intent(getApplicationContext(),
NewCustomerActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all customers
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllCustomersActivity.this, customersList,
R.layout.list_item, new String[] { TAG_CID,
TAG_NAME},
new int[] { R.id.cid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
只有一个结果,那你为什么要使用foreach()?
如果$singleresult
有多个结果,您将循环遍历所有结果并返回最后一个(因为之前的所有$singleresult
都将被覆盖。
如果没有循环,您是否无法直接访问$var
?
如果$singleresult->id
为空,也许你可以通过返回id
内的所有内容来开始调试,看看为什么没有$singleresult
。