1

有两个表 Contents 和 contentpreviews。我想从一个动作中添加两个表中的数据。更多内容表的 last_insert_id 将保存到 contentpreviews 表中。怎么可能处理?

$this->data['Content']['user_id']     = $uid;
$this->data['Content']['content_category_id'] = $catname;
$this->data['Content']['title']         = $contname;
$this->data['Content']['description']           = $contdesc;
$this->Content->create();                             
$this->Content->save($this->data['Content']);
/*****************CONTENT SAVE END*****************/

if(!empty($arxml[$arrFirstNode]['Webpreview'])){
$webPreViewItem = count(array_values($arxml[$arrFirstNode ['Webpreview']));                       
$webPrevfileName  = array_values($arxml[$arrFirstNode]['Webpreview']);

for($wpi=0; $wpi<$webPreViewItem; $wpi++){
   $this->data['contentpreviews']['content_id']   = $this->Content->getLastInsertId();
   $this->data['contentpreviews']['name']     = $webPrevfileName[$wpi];
   $this->data['contentpreviews']['is_wap']      = 1;
//$this->Contentpreview->create();            

//$this->Contentpreview->saveAll($this->data['Contentpreview']); 现在如何将数据保存到内容和 contentpreviews 表中?

4

1 回答 1

0

CakePHP 允许您一次保存多个模型关联。此链接将为您提供帮助。

首先,您必须创建一个关联的数组,如参考链接中给出的那样。然后您应该必须使用以下代码将数据保存到 contents 和 contentprviews 表中。

$this->Content->saveAssociated($this->requst->$data, array('deep' => true));

您还可以使用 saveAll() 方法在单次查询中将数据保存到多个相关表中。

于 2012-07-19T11:10:58.683 回答