Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to add uploaded file information using model method "add" to data base table
  • Please if someone help me...
    I have two database tables 'products' and 'products_photos'
    By adding new products i want photo informations to be saved in the table 'products_photos'

    The result is :
    photos are uploaded,
    my table "products" is updated
    my table "products_photos" is empty
    First time met and used this model method add, assume something wrong is here
     
    public static function add($products_photos)
           {  print_r($products_photos);
              
             Model_Products_Photo::forge( $products_photos[0]);
     
     
           }  
     


              

     

  • HarroHarro
    Accepted Answer
    You create an object, but don't assign it to anything, and you don't save it either.

    $photo = Model_Products_Photo::forge( $products_photos[0]);
    $photo->save();
  • Thank you Harro, 
    i added like that you suggest, 
    but my table is still empty..

    Maybe that need to contain evey fields from file structure... is it needed to contain the same cols name like field from the file structure?
  • HarroHarro
    Accepted Answer
    forge() requires an assoc array as input.

    Any keys in that array that don't map to column names will be stored as additional object properties, so it doesn't matter that the array contains keys that do not map to columns.

    The array may not contain a primary key value.

    If save() doesn't save anything, you should get an error message or save() should return false. This may be the case if you try to save an empty object.
  • Thank you a lot...

    I still can not notice what is wrong...no any errors, i got message my product is saved.
  • HarroHarro
    Accepted Answer
    If you're input is invalid, the ORM will throw a database exception. If the input is valid from a database point of view, but the INSERT failed, save() will return false. If save() returns true, the INSERT was a success.

    If your input contains a PK value, you end up with an object with an invalid state. The ORM will attempt to do an UPDATE instead of an INSERT because the presence of the PK indicates an existing record. The UPDATE will run fine (but with 0 records updated) and save() will return true.

    Hence my remark that you may not forge a new object with an array that contains the PK value.

    Enable the profiler in your config/config.php, and enable DB profiling
    in your config/<environment/db.php. And then check what DB queries
    are executed.

  • Thank you Harro, 

    I resolved that already by myself. The problem was because i did not used the proper way to save two related models.
    I am completely new in FUEL and its documentation helped me.
  • HarroHarro
    Accepted Answer
    Ok, glad you've got it covered.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion