Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Add insert column and value in pre_save for Model_Crud
  • Hi everybody ! My problem is simple : I need to add a column/value pairs in the pre_save method of a Model_Crud object. For example :
    protected function pre_save(&$query)
    {  
     $query->set(array('foreign_key' => 10)); 
    }
    
    is not working.
    Because in this example an other "group" of values is created with the value "10". So I've got this error :
    Column count doesn't match value count at row 1 [ INSERT INTO `my_table` (`primary_key`, `foreign_key`) VALUES (null), (10) ]
    
    The query should be :
    INSERT INTO `my_table` (`primary_key`, `foreign_key`) VALUES (null, 10) 
    
    I made a very crappy workaround in the values method of the Database_Query_Builder_Insert class :
    public function values(array $values)
     {
      if ( ! is_array($this->_values))
      {
       throw new \FuelException('INSERT INTO ... SELECT statements cannot be combined with INSERT INTO ... VALUES');
      }
    
      // Get all of the passed values
      $values = func_get_args();
      
      if (isset($this->_values) && !empty($this->_values)) {
       $this->_values[0] = array_merge($this->_values[0], $values[0]);
      } else {
       $this->_values = array_merge($this->_values, $values);
      }
    
      return $this;
     }
    
    Indeed it works, but...
    So, thanks so much for your replies and your help ! Best regards, Aurel
  • I don't understand what you're trying to do here. $this->_values should be an assoc array with column->value pairs. So what does $this->_values[0] achieve?
  • Thanks for your reply, Harro Verton. The problem is that when I do this, for example :
    protected function pre_save(&$query)
    {  
     $query->set(array('foreign_key' => 10)); 
    }
    
    in the pre_save method, the return query is :
     INSERT INTO `my_table` (`primary_key`, `foreign_key`) VALUES (null), (10)
    
    instead of
    INSERT INTO `my_table` (`primary_key`, `foreign_key`) VALUES (null, 10)
    

    Because in the values() method of Database_Query_Builder_Insert, func_get_args() return an array, so $this->_values is not an assoc array with column->value pairs, but an array of an assoc array with column->value pairs
    array(
      0 => array('primary_key' => null)
    )
    
    maybe instead of
    array('primary_key' => null)
    

    So the array_merge return, after doing the first example (set foreign_key)
    array(
      0 => array('primary_key' => null)
      1 => array('foreign_key' => 10)
    )
    
    Sorry if my explanations are not so clear.
    I hope you'll understand..! Thanks. Aurel
  • The issue here is that a call to set() or values() adds a new record, it does not have the functionality to amend the last set of values defined. And there is no method that does. Instead of hacking the core, please create an issue at http://github.com/fuel/core/issues, so it can be fixed properly. The code defined values() as "Adds or overwrites values." which is clearly not the case, there is no option to overwrite...
  • Ok Harro. I have created an issue here : https://github.com/fuel/core/issues/1111 Thank you.

Howdy, Stranger!

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

In this Discussion