gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:44:56 25 4
gpt4 key购买 nike

我试图让我在 CakePHP 2.3 中的关联模型正确保存,但我遇到了问题。我正在存储帖子,我想知道这些帖子中有哪些链接。对于这些链接中的每一个,如果可用,我想存储 anchor 文本。我的数据库设置如下图所示。
Database Diagram
(来源:derekperkins.com)
anchor 模型

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, $validate = true, $fieldList = array()) {
$id = Url::find('first', array(
'fields' => array('id'),
'recursive' => -1,
'conditions' => array('url' => $data['url'])
));
if( $id )
$data['id'] = $id['Url']['id'];
return parent::save($data, $validate, $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, $validate = true, $fieldList = array()) {
$id = Post::find('first', array(
'fields' => array('id'),
'recursive' => -1,
'conditions' => array('external_post_id' => $data['external_post_id'])
));
if( $id )
$data['id'] = $id['Post']['id'];

return parent::save($data, $validate, $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, $validate = true, $fieldList = array()) {
$id = Post::find('first', array(
'fields' => array('id'),
'recursive' => -1,
'conditions' => array('external_post_id' => $data['external_post_id'])
));
if( $id )
$data['id'] = $id['Post']['id'];

pr($data);

return parent::save($data, $validate, $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
)
)

这个 $data 不正确,正确的数据是:
  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, $validate = true, $fieldList = array()) {
$id = Post::find('first', array(
'fields' => array('id'),
'recursive' => -1,
'conditions' => array('external_post_id' => $data['external_post_id'])
));
if( $id )
$data[$this->name]['id'] = $id['Post']['id'];

return parent::save($data, $validate, $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');

关于CakePHP saveAssociated 不通过模型数据保存 HasMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16468028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com