CakePHP saveAssociated未通过模型数据保存HasMany

前端之家收集整理的这篇文章主要介绍了CakePHP saveAssociated未通过模型数据保存HasMany前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在Cake PHP 2.3中获取我的关联模型以便正确保存,但我遇到了问题.我正在存储帖子,我想知道这些帖子中有哪些链接.对于每个链接,我想存储锚文本(如果可用).我的数据库设置如下图所示.

Database Diagram http://derekperkins.com/wp-content/uploads/cakephp-database.png

锚模型

class Anchor extends AppModel {

        public $hasMany = array(
            'PostsUrl' => array(
                'className' => 'PostsUrl','foreignKey' => 'anchor_id','dependent' => false
            )
        );

        public function save($data = NULL,$validate = true,$fieldList = array()) {
            $id = Anchor::find('first',array(
                'fields' => array('id'),'recursive' => -1,'conditions' => array('anchor' => $data['anchor'])
            ));
            if( $id )
                $data['id'] = $id['Anchor']['id'];
            return parent::save($data,$validate,$fieldList);
        }
    }

网址模型

class Url extends AppModel {

    public $hasMany = array(
        'PostsUrl' => array(
            'className' => 'PostsUrl','foreignKey' => 'url_id','dependent' => false
        )
    );

    public function save($data = NULL,$fieldList = array()) {
        $id = Url::find('first',array(
            'fields' => array('id'),'conditions' => array('url' => $data['url'])
        ));
        if( $id )
            $data['id'] = $id['Url']['id'];
        return parent::save($data,$fieldList);
    }
}

PostsUrl模型

class PostsUrl extends AppModel {

    public $belongsTo = array(
        'Post' => array(
            'className' => 'Post','foreignKey' => 'post_id'
        ),'Url' => array(
            'className' => 'Url','foreignKey' => 'url_id'
        'Anchor' => array(
            'className' => 'Url','foreignKey' => 'anchor_id'
        )*/
    );
}

发布模型

class Post extends AppModel {

    public $hasMany = array(
        'PostsUrl' => array(
            'className' => 'PostsUrl','foreignKey' => 'post_id','dependent' => false
        )
    );


    public function save($data = NULL,$fieldList = array()) {
        $id = Post::find('first','conditions' => array('external_post_id' => $data['external_post_id'])
        ));
        if( $id )
            $data['id'] = $id['Post']['id'];

        return parent::save($data,$fieldList);
    }
}

提交数据

我创建了一个表单来测试我的模型.这是我用来保存表单创建的数组的代码.我收到一条消息说事情已成功保存,但只有帖子保存.其他三个表中没有输入任何内容.我也使用DebugKit,没有sql调用引用任何数据.

$this->Post->saveAssociated($this->request->data,array('deep' => true))

Array
(
    [Post] => Array
        (
            [external_post_id] => 12345
            [sentiment_score] => 3.3
        )

    [URL] => Array
        (
            [url] => http://test.com
        )

    [Anchor] => Array
        (
            [anchor] => Test Anchor
        )
)

我也尝试格式化我的数组,将PostsUrl下面的URL和Anchor作为子数组,但这也不起作用.

我的Model :: save函数可以防止我复制数据,它们可以在我过去使用的其他模型中正常工作(虽然我愿意接受更好的方法来做这个,因为这会使用数据库调用每次检查).我也试过评论它们,它不会影响我的代码.我应该如何构建这个来妥善保存?

首先关于Model :: save函数,例如:
public function save($data = NULL,$fieldList = array()) {
    $id = Post::find('first',array(
        'fields' => array('id'),'conditions' => array('external_post_id' => $data['external_post_id'])
    ));
    if( $id )
        $data['id'] = $id['Post']['id'];

    pr($data);

    return parent::save($data,$fieldList);
}

它以这种方式打印$data:

Array
(
    [id] => 3 //for example 3
    [Post] => Array
        (
            [external_post_id] => 12345
            [sentiment_score] => 3.3
        )

    [URL] => Array
        (
            [url] => http://test.com
        )

    [Anchor] => Array
        (
            [anchor] => Test Anchor
        )
)

这个$数据不正确,正确的数据是:

Array
    (
        [Post] => Array
            (
                [id] => 3 //for example 3
                [external_post_id] => 12345
                [sentiment_score] => 3.3
            )

        [URL] => Array
            (
                [url] => http://test.com
            )

        [Anchor] => Array
            (
                [anchor] => Test Anchor
            )
    )

您必须以这种方式更改Model :: save函数

public function save($data = NULL,'conditions' => array('external_post_id' => $data['external_post_id'])
    ));
    if( $id )
        $data[$this->name]['id'] = $id['Post']['id'];

    return parent::save($data,$fieldList);
}

第二,你不能用单一保存来保存这些数据,你应该这样保存你的数据:

$postData = array
(
    'Post' => array
        (
            'external_post_id' => 12345
            'sentiment_score' => 3.3
        )
);

$this->Post->save($data);

$postUrlId = $this->PostsUrl->find('first',array(
'conditions' => array(
                   'post_id' => $this->Post->id
                     ),'fields' => array('id')
));

$urlAnchorData = array(
    'URL' => array
        (
            'url' => 'http://test.com'
        ),'Anchor' => array
        (
            'anchor' => 'Test Anchor'
        ),'PostsUrl' => array(
            'id' => $postUrlId['PostsUrl']['id']
     )
);

$this->PostsUrl->saveAll('$urlAnchorData');
原文链接:https://www.f2er.com/php/136765.html

猜你在找的PHP相关文章