Search code examples
phpmysqlsqlfuelphp

Sending Data to 2 MySQL tables - FuelPHP / PHP


I am adapting StationWagon (FuelPHP app) and so far it's working really well.

I have adapted it (with some help) to allow multiple images to be uploaded to the server. This is also working great.

However, I am thinking it would make more sense if I had 2 Tables: 1) Articles and 2) ArticleImages. I would use a Foreign Key to associate the Images with the Article. So when publishing an article it would add the article data to 'Articles' table and move each image to a new row in 'ArticleImages'.

So ultimately my 'ArticleImages' table could be:

ID | ImageURL | ArticleID

Portion of my 'articles.php' controller:

<?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 */

Solution

  • So when i made the code for you back a few days a go you only requested, one file input.

    And no offense but you are doing it all wrong...

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

    should be

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

    To get you to understand

    what you did here, $value = Upload::get_files(); yes this gets all the elements but since you need to loop trouh the elements you dont need it

    Second

    this $value[0]['saved_as'] only grabs the first image name, just the first one, and since you are in a loop now you need to refer to the $files variable as i shown you in the above example, just an example