我有以下问题。
我有2张桌子:
public class TrainingActivity extends ActionBarActivity {
private String jsonResult;
public String url ="";
ListView listView;
List<Map<String,String>> activity = new ArrayList<Map<String,String>>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_training);
listView = (ListView) findViewById(R.id.listView);
accessWebService();
}
private class JsonReadTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try{
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}catch(ClientProtocolException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is){
String rLine ="";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try{
while((rLine = rd.readLine()) != null){
answer.append(rLine);
}
}catch(IOException e){
Toast.makeText(getApplicationContext(),"error ..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result){
ListDrwaer(); // we will create it later
}
}
public void accessWebService(){
JsonReadTask task = new JsonReadTask();
task.execute(new String[] {url});
}
public void ListDrwaer (){
try{
JSONObject jsonResonse = new JSONObject(jsonResult.substring(jsonResult.indexOf("{"), jsonResult.lastIndexOf("}")+1));
JSONArray jsonMainNode = jsonResonse.optJSONArray("name");
final ArrayList<HashMap<String,String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String,String> map;
for(int i=0; i<jsonMainNode.length(); i++){
JSONObject c = jsonMainNode.getJSONObject(i);
map = new HashMap<String,String>();
map.put("name", c.getString("name"));
map.put("email", c.getString("email"));
MyArrList.add(map);
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(TrainingActivity.this, MyArrList, R.layout.activity_column, new String[]{"name","email"}, new int[]{R.id.ColMemberID, R.id.ColName});
listView.setAdapter(sAdap);
}
}catch (JSONException e){
Toast.makeText(getApplicationContext(),"error ..." + e.toString(), Toast.LENGTH_LONG).show();
}
}
}
articles (id, title, body, article_type_id, created, modified)
。 (article_types** (id, name)
表中已有一些数据)我还有article_types
:
ArticlesController
ArticlesTable :
<?php
namespace App\Controller;
class ArticlesController extends AppController {
public function create() {
$article = $this->Articles->newEntity();
if ($this->request->is('post')) {
$article = $this->Articles->patchEntity($article, $this->request->data(), [
'associated' => ['ArticleTypes']
]);
dump($this->request->data);
dump($article);
}
$this->set('article', $article);
$this->set('articleTypes', $this->loadModel('ArticleTypes')->find('list')->toArray());
}
}
create.ctp :
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class ArticlesTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->addBehavior('Timestamp');
$this->belongsTo('ArticleTypes', [
'foreignKey' => 'article_type_id',
'className' => 'ArticleTypes',
'propertyName' => 'article_type'
]);
}
}
如果我在Controller中使用数据,它看起来像这样:
$这 - &GT;请求 - &GT;数据
<?= $this->Form->create($article) ?>
<?= $this->Form->input('title') ?>
<?= $this->Form->input('body', ['type' => 'textarea']) ?>
<?= $this->Form->input('article_type.id', ['type' => 'select', 'options' => $articleTypes]) ?>
<?= $this->Form->button('send') ?>
<?= $this->Form->end() ?>
$制品
array:3 [▼
"title" => "abcdefg"
"body" => "abcdefghsdflösdfklösdfsddfghdfgd"
"article_type" => array:1 [▼
"id" => "3"
]
]
正如您在Article {#101 ▼
+"body": "abcdefghsdflösdfklösdfsddfghdfgd"
+"article_type": Entity {#127 ▼
+"id": 3
+"[new]": true
+"[accessible]": array:1 [▶]
+"[dirty]": array:1 [▶]
+"[original]": []
+"[virtual]": []
+"[errors]": []
+"[invalid]": []
+"[repository]": "ArticleTypes"
}
+"[new]": true
+"[accessible]": array:1 [▶]
+"[dirty]": array:2 [▶]
+"[original]": []
+"[virtual]": []
+"[errors]": array:1 [▶]
+"[invalid]": array:1 [▶]
+"[repository]": "Articles"
}
中所看到的,属性$article
中只有可用实体的ID。
"article_type"
的{{1}}是否可以自动分配"article_type"
的名称?