关联和模型数据保存问题
使用 cakephp 1.3 进行开发(来自 github 的最新版本)。 有 2 个模型与 hasAndBelongsToMany 绑定:文档和标签。换句话说,文档可以有许多标签。我添加了一个新的文档提交表单,用户可以在其中输入用逗号分隔的标签列表(将添加新标签,如果尚不存在)。我在github上查看了cakephp Bakery 2.0 源代码并找到了解决方案。但似乎有些不对劲。
class Document extends AppModel {
public $hasAndBelongsToMany = array('Tag');
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['tags']) && !empty($this-
>data[$this->alias]['tags']))
{
$tagIds = $this->Tag->saveDocTags($this->data[$this->alias]
['tags']);
unset($this->data[$this->alias]['tags']);
$this->data[$this->Tag->alias][$this->Tag->alias] = $tagIds;
}
return true;
}
}
class Tag extends AppModel {
public $hasAndBelongsToMany = array ('Document');
public function saveDocTags($commalist = '') {
if ($commalist == '') return null;
$tags = explode(',',$commalist);
if (empty($tags)) return null;
$existing = $this->find('all', array(
'conditions' => array('title' => $tags)
));
$return = Set::extract($existing,'/Tag/id');
if (sizeof($existing) == sizeof($tags)) {
return $return;
}
$existing = Set::extract($existing,'/Tag/title');
foreach ($tags as $tag) {
if (!in_array($tag, $existing)) {
$this->create(array('title' => $tag));
$this->save();
$return[] = $this->id;
}
}
return $return;
}
}
因此,新标签创建效果很好,但文档模型无法保存关联数据并告诉我们: SQL 错误:1054:“字段列表”中的未知列“数组” 查询:INSERT INTO documents
(title
, content
, shortnfo
, 日期
, 状态
) VALUES ('带有标签的文档', '', '', Array, 1) 有什么想法如何解决这个问题吗?
PS 从 firebug 中发布此表单的数据: _方法 POST data[文档][内容]测试文档内容 数据[文件][日期][年份] 2010 data[Document][shortnfo] 有关文档的简短信息 数据[文档][状态] 1 数据[文档][标签]测试、类别、列表 数据[文档][标题] 测试标题 我们可以看到没有数组。
Developing with cakephp 1.3 (latest from github).
There are 2 models bind with hasAndBelongsToMany: documents and tags. Document can have many tags in other words. I've add a new document submitting form there user can enter a list of tags separated with commas (new tag will be added, if not exist already). I looked at cakephp bakery 2.0 source code on github and found the solution. But it seems that something is wrong.
class Document extends AppModel {
public $hasAndBelongsToMany = array('Tag');
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['tags']) && !empty($this-
>data[$this->alias]['tags']))
{
$tagIds = $this->Tag->saveDocTags($this->data[$this->alias]
['tags']);
unset($this->data[$this->alias]['tags']);
$this->data[$this->Tag->alias][$this->Tag->alias] = $tagIds;
}
return true;
}
}
class Tag extends AppModel {
public $hasAndBelongsToMany = array ('Document');
public function saveDocTags($commalist = '') {
if ($commalist == '') return null;
$tags = explode(',',$commalist);
if (empty($tags)) return null;
$existing = $this->find('all', array(
'conditions' => array('title' => $tags)
));
$return = Set::extract($existing,'/Tag/id');
if (sizeof($existing) == sizeof($tags)) {
return $return;
}
$existing = Set::extract($existing,'/Tag/title');
foreach ($tags as $tag) {
if (!in_array($tag, $existing)) {
$this->create(array('title' => $tag));
$this->save();
$return[] = $this->id;
}
}
return $return;
}
}
So, new tags creation works well but document model can't save association data and tells:
SQL Error: 1054: Unknown column 'Array' in 'field list'
Query: INSERT INTO documents
(title
, content
, shortnfo
,date
, status
) VALUES ('Document with tags', '', '', Array, 1)
Any ideas how to solve this problem?
P.S. Post data from firebug for this form:
_method POST
data[Document][content] test document content
data[Document][date][year] 2010
data[Document][shortnfo] short info about document
data[Document][status] 1
data[Document][tags] test, categories, list
data[Document][title] Test title
No arrays as we can see.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了! :) 问题出在视图中:
但是数据库中的日期字段。该字段的类型是年份。
Solved! :) The problem was in the view:
But date field in database. the type of this field is year.