我想爲cakephp3應用程序編寫一個簡單的Tagging-Plugin。所以可以說我們有一個型號書籍和一個型號評論。對於每個模型,都應該可以附加標籤 - 只需添加一個行爲(在插件中):$this->addBehavior('Tag.Taggable')
。cakephp3:保存相關數據
我在數據庫中創建了兩個表格:tags, tagged_tags
。
表tagged_tags:
id | tag_id | tagged_id |
1 | 1 | 1 |
2 | 2 | 1 |
tagged_id是被標記的實體的ID。它所屬的信息是在另一個表中。 表標籤:
id | tag | model |
1 | book | App\Model\Table\BooksTable |
2 | nobook | App\Model\Table\ReviewsTable|
顯然,只有第一個標籤屬於一本書。
class TaggableBehavior extends Behavior
{
// Some more code here
public function __construct(Table $table, array $config = [])
{
parent::__construct($table, $config);
$this->_table = $table;
$this->_table->belongsToMany('Tag.Tags', [
'joinTable' => 'tagged_tags',
'foreignKey' => 'tagged_id',
'targetForeignKey' => 'tag_id',
'conditions' => [
'Tags.model' => get_class($this->_table);
]
]);
}
}
檢索數據完美地工作。但是,儲蓄是一個問題:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Tags.model' in 'where clause'
SQL Query:
SELECT TaggedTags.id AS
TaggedTags__id
, TaggedTags.tagged_id ASTaggedTags__tagged_id
, TaggedTags.tag_id ASTaggedTags__tag_id
FROM tagged_tags TaggedTags WHERE (tagged_id = :c0 AND Tags.model = :c1)
我不是那麼舒爾爲什麼這裏的CakePHP執行SELECT查詢,我不真的關心。爲什麼這個查詢導致錯誤很明顯。但我的錯誤在哪裏?它與'conditions' => ['Tags.model' => get_class($this->_table);
有關。如果沒有這一點,我可以保存數據(但不能說至極標籤屬於一本書或不)
編輯:一些額外的信息
下面是完整的SQL語句,在調試套件顯示http://freetexthost.com/tc3s46nugi
控制器代碼:
public function add()
{
$book = $this->Books->newEntity();
if ($this->request->is('post')) {
$book = $this->Books->patchEntity($book, $this->request->data);
if ($this->Books->save($book)) {
$this->Flash->success(__('The book has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The book could not be saved. Please, try again.'));
}
}
在行爲我有一些邏輯(複製/粘貼)形成書籤教程
public function beforeSave($event, $entity, $options)
{
if ($entity->tag_string) {
$entity->tags = $this->_buildTags($entity->tag_string);
}
}
protected function _buildTags($tagString)
{
$new = array_unique(array_map('trim', explode(',', $tagString)));
$out = [];
$query = $this->_table->Tags->find()
->where([
'Tags.tag IN' => $new,
'Tags.model' => $this->name()
]);
// Remove existing tags from the list of new tags.
foreach ($query->extract('tag') as $existing) {
$index = array_search($existing, $new);
if ($index !== false) {
unset($new[$index]);
}
}
// Add existing tags.
foreach ($query as $tag) {
$tag['count'] = $tag['count']+1;
$out[] = $tag;
}
// Add new tags.
foreach ($new as $tag) {
$out[] = $this->_table->Tags->newEntity(['tag' => $tag, 'model' => $this->name(), 'count' => 0]);
}
return $out;
}
你能提供你的代碼是在控制? – Ray
我已將代碼添加到上面的帖子中。 –
堆棧跟蹤:http://freetexthost.com/3msbh4rabw –