Php Codeigniter 3模型函数无返回变量

Php Codeigniter 3模型函数无返回变量,php,sql,codeigniter,Php,Sql,Codeigniter,我正在做一个项目,用户可以发布/发布他们自己的故事,并阅读其他人的故事。很简单 这是我的名为publish的控制器方法: public function published() { $story = array('author' => $this->session->userdata('username'), 'title' => $this->input->post('title'),

我正在做一个项目,用户可以发布/发布他们自己的故事,并阅读其他人的故事。很简单

这是我的名为publish的控制器方法:

  public function published()
   {
    $story = array('author' => $this->session->userdata('username'),
                    'title' => $this->input->post('title'),
                    'synopsis' => $this->input->post('synopsis'));

    $new_storyid = $this->story_model->new_story($story);

    if($new_storyid != NULL)
   {
    $genre = $this->input->post('genre');
    for($temp=0;$temp<count($genre);$temp++)
    {
        $genres[$temp] = array('story_id' => $new_storyid,
                    'story_genre_name' => $genre[$temp]);
     }
    $insert_genre = $this->story_model->new_story_genre($genres);
    $tag = $this->input->post('tags');
    for($temp=0;$temp<count($tag);$temp++)
    {
        $tags[$temp] = array('story_id' => $new_storyid,
                    'story_tag_name' => $tag[$temp]);
    }

    $content_warning = $this->input->post('content_warning');
    for($temp=0;$temp<count($content_warning);$temp++)
    {
        $content_warnings[$temp] = array('story_id' => $new_storyid,
                'story_content_warning_name' => $content_warning[$temp]);
    }





    //$chapter = array('story_id' => $new_storyid,
    //'chapter_number' => 1, 'chapter_title' =>  $this->input->post('chapter_title'),
    //'chapter_content' => $this->input->post('chapter_content'),
    //'chapter_number' => 1, 'date_added' => mdate('%Y-%m-%d %h-%i-%s',time()));    
    //$result = $this->story_model->add_chapter($chapter);
    //if($result){
    //redirect('account/userprofile_published_stories');}
    }
    }
我没有为我的标签和内容警告插入添加其他2个函数,因为我现在很困惑。一切都很好,插入了我的类型

我的桌子如下所示:

在上述方法中插入一个故事时,我要做的第一件事是在我的故事表中插入一个新的故事行,并返回
new\u storyid
变量

在新的
storyid
之后,我添加了流派、标签、内容警告,然后是章节

我的问题是,我应该在插入类型、标记和内容警告的方法中返回什么

我忘记了这一部分,因为到目前为止,我编写的每个模型方法都会返回控制器中所需的变量。我的第一个想法是,如果插入成功/失败,则返回一个
TRUE/FALSE
变量,但除非有特殊情况,因为我已经处理了数据,所以100%确定插入成功。我是否应该返回
TRUE/FALSE
并添加if语句,如:

if($insert_genre){
//insert tags here
  if($insert_tags){
    //insert content warning here
       if($insert_content_warning){
       //insert chapters here
       //redirect to view here
       }
  }
}
或者我可以不归还任何东西吗?如果是的话,这是一种正确的方式吗

编辑:我忘了提到我还没有在所有插入之前添加表单验证规则。因此,我的函数将嵌套在多个if语句中。 我刚刚编辑了我的模型方法:

public function new_story_genre($genre){
$inserted = 0;
foreach($genre as $row){
$this->db->insert('story_genre', $row);
$inserted += $this->db->affected_rows();}
if($inserted == count($genre)){
return TRUE;}else{ return FALSE; }
}

上面比较了插入的行数和传递到方法中的行数。每次插入一行时,它都会向插入的变量中添加1。因此,如果我的控制器将3行传递给该方法,那么插入的变量也应该是3才能成功插入。

我认为您总是返回一些内容是正确的。无论什么原因,错误都会发生,即使您已经验证了数据(您永远不知道),也最好对错误进行解释。编码实践表明,多个嵌套的ifs是不好的实践。我个人的一个偏好是,在函数的最后一行之前,检查整个链中的失败,而不是成功(如果它走得那么远,那么一切都是好的)

我通常使用这样的方案:

public function something() {
    if (!$insert_genre) {
        // add flash error message
        // redirect to controller
    }
    if (!$insert_tags) {
        // add flash error message
        // redirect to controller
    }
    if (!$insert_content_warning) {
        // add flash error message
        // redirect to controller
    }
    // yay, something went right!
}
在这种情况下,它是非常程序化的。最重要的条件应该是第一个,如果C依赖于A,那么A应该是第一个条件

无关的:


在这里很难理解你的一些文章,但你似乎也应该看看你是如何做的体裁。如果输入的流派已经存在于数据库中,您真的需要添加它吗?您是否应该使用一种关系,将id存储在主表中,并在显示时加入?

我的数据库中有标记、类型和内容警告表。在我的表单中,我使用foreach循环来打印它们。标签我用它来控制类型/标签/内容警告。这就是为什么我的另一个用于插入故事的表名为story\u tags、story\u genre、story\u content\u warning。为了区别。我建议你插入更新函数,而不是显示函数。我还得到了故障检查部分。因此,在我的控制器中,我只需要检查插入是否失败,如果是,则直接将其重定向到我的视图打印错误,从而中断函数,如果没有错误,则函数将继续。对吧?我不会更新任何插入流派/标签/内容的警告。我将它们插入到单独的表中,并带有相应的story_id。此外,由于表单本身不是输入文本和复选框,因此不必担心任何重复项。所以在我的故事类型表中。可能有多行类型的“恐怖”,但不同的故事id
public function something() {
    if (!$insert_genre) {
        // add flash error message
        // redirect to controller
    }
    if (!$insert_tags) {
        // add flash error message
        // redirect to controller
    }
    if (!$insert_content_warning) {
        // add flash error message
        // redirect to controller
    }
    // yay, something went right!
}