【问题标题】:Sending Data to 2 MySQL tables - FuelPHP / PHP将数据发送到 2 个 MySQL 表 - FuelPHP / PHP
【发布时间】:2012-11-23 12:08:56
【问题描述】:

我正在适应 StationWagon(FuelPHP 应用程序),到目前为止它运行良好。

我已经对其进行了调整(在一些帮助下)以允许将多个图像上传到服务器。这也很好用。

然而,我认为如果我有 2 个表格 会更有意义:1) 文章2) ArticleImages 。我会使用外键将图像与文章相关联。因此,在发布文章时,它会将文章数据添加到“Articles”表中,并将每个图像移动到“ArticleImages”中的新行。

所以最终我的 'ArticleImages' 表可能是:

ID |图片网址 |文章ID

我的“articles.php”控制器的一部分:

<?php       

    public function action_add()
    {
    $val = Model_Article::validate('add_article'); //<-- maybe its just me but i never saw any similar to this in fuelphp sorry about this if im wrong

    // if your form validation is okay than continue with everyhing else
    if ($val->run())
    {
        $article = Model_Article::forge();
        // Custom configuration for this upload
        $config = array(
            'path' => DOCROOT.DS.'images',
            'randomize' => true,
            'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
        );

        Upload::process($config);

        // if a valid file is passed than the function will save, or if its not empty
        if (Upload::is_valid())
        {
            // save them according to the config
            Upload::save();

           //if you want to save to tha database lets grab the file name
            $value = Upload::get_files();  

            foreach($value as $files) {
               print_r($files); 
            }
            $article->filename = $value[0]['saved_as'];
         } 

        $status = (Input::post('save_draft') ? 0 : 1);

        if ( ! $val->input('category_id'))
        {
            $category_id = null;
        }
        else
        {
            $category_id = $val->validated('category_id');
        }

             $article->user_id = $this->user_id;
             $article->category_id = $category_id;
             $article->title = $val->validated('title');
             $article->body = $val->validated('body');
             $article->published = $status;


        if ($article->save())
        {
            Session::set_flash('success', 'Article successfully added.');
        }
        else
        {
            Session::set_flash('error', 'Something went wrong, '.
                'please try again!');
        }

        Response::redirect('articles/add');
    }

    $this->template->title = 'Add Article';
    $this->template->content = View::forge('articles/add')
        ->set('categories', Model_Category::find('all'), false)
        ->set('val', Validation::instance('add_article'), false);
    }

/* End of file articles.php */

【问题讨论】:

    标签: php mysql sql fuelphp


    【解决方案1】:

    您正在尝试在 Articles 和 ArticleImages 之间建立关系。一个ArticleImage属于一个Article,而一个Article有很多个ArticleImage。 FuelPHP 为您想要实现的目标内置了功能。看看FuelPHP docs on its Object Relational Mapper,尤其是Belongs To and Has Many关系。

    【讨论】:

      【解决方案2】:

      所以,当我几天前为您编写代码时,您只要求输入一个文件。

      没有冒犯,但你做错了......

          foreach($value as $files) {
            print_r($files); 
          }
       $article->filename = $value[0]['saved_as'];
      

      应该是

      foreach($value as $files) {
                 $articleimg = Model_Articleimages::forge();
                 $articleimg->image_row_name = $files['saved_as']
              }
      

      让你明白

      你在这里做了什么,$value = Upload::get_files(); 是的,这会获取所有元素,但是因为你需要循环遍历不需要的元素

      第二

      这个$value[0]['saved_as'] 只获取第一个图像名称,只是第一个,因为你现在处于循环中,所以你需要引用$files 变量,正如我在上面的示例中向你展示的那样,只是一个例子

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-09
        • 2013-05-16
        • 2013-12-21
        • 1970-01-01
        相关资源
        最近更新 更多