关联和模型数据保存问题

发布于 2024-08-28 12:39:06 字数 2201 浏览 3 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-09-04 12:39:06

解决了! :) 问题出在视图中:

'date'=>array('label'=>'Date', 'type'=>'date', 'dateFormat'=>'Y'),

但是数据库中的日期字段。该字段的类型是年份。

Solved! :) The problem was in the view:

'date'=>array('label'=>'Date', 'type'=>'date', 'dateFormat'=>'Y'),

But date field in database. the type of this field is year.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文