我有一個'AddOns'的模型,它有一堆數據。我也有標籤,它們以通常的方式工作,只是就像在SO上一樣,從逗號分隔的列表中最多允許5個標籤。CakePHP - 將兩個表手動鏈接在一起(向HABTM鏈接表中添加一個字段)
我試圖創建的行爲如下:
- 用戶創建上添加
- 添加上創建
- 系統分裂標記成一個陣列,並通過他們循環
- 系統查找標記,如果存在,我們將使用該ID
- 如果標記不存在,我們將創建它並使用該標識
- 創建一個鏈接be補間的標籤和添加
我可以很容易地使用手動查詢,但我不知道這是最好的方法或我應該如何處理這個問題。這裏是我的代碼:
if ($this->request->is('post')) {
$this->AddOn->create();
$this->AddOn->set('user_id', $this->Auth->user('id'));
if ($this->AddOn->save($this->request->data)) {
// Get the ID of the addon
$addon_id = $this->AddOn->getInsertID();
$tagsarr = explode(',', $this->request->data['Tag']['Tag']);
foreach($tagsarr as $tag){
$tagdb = $this->Tags->findByTagName(trim($tag));
if(!empty($tagdb)){
// HELP! We have a tag, but how do we add the link?
} else {
// Add the tag, but then how do we link it?
}
unset($tagdb);
}
$this->Session->setFlash(
__('The %s has been saved', __('add on'))
);
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(
__('The %s could not be saved. Please, try again.', __('add on')),
);
}
}
編輯:添加了一些我試圖實現的僞代碼。
$AddOn->create();
$AddOn->save(); // Create AddOn
$AddOnID = $AddOn->insertId(); // Newly inserted AddOn's ID
$tagsArr = explode(',', $post->tags); // Make an array of tags [this,is,a,tag,list]
foreach($tagsArr as $tag){ // Loop through them
$tagFromDb = $Tags->findTagByName($tag); // Try and find the tag from the tags table
if(!$tagFromDb){ // Have we found it? No
$tagId = $Tags->newTag($tag); // Add it and reference
} else { // Have we found it? Yes
$tagId = $tagFromDb->id; // Reference it
}
$AddOnTagsLink->addAddOnTagsLink($AddOnID, $tagId); // Add the link
}
您是否閱讀了本書中介紹如何保存HABTM數據的部分:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm你有,你有什麼嘗試? – Dave 2013-05-13 13:34:52
我不確定那是我需要的。我希望在添加主記錄('addon')後創建鏈接,引用標記表中的標記ID(假設有一個)。我將用我想要實現的一些僞代碼更新這個問題 – Mike 2013-05-13 17:34:03
@Dave - 我已經添加了僞代碼,可能更好地解釋我想要實現什麼。 – Mike 2013-05-13 17:41:07