Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ErrorException [ 4096 ]
  • Full error message I am getting:

    ErrorException [ 4096 ]: Object of class Uploader\Model_Category could not be converted to string
    The code that is causing this:
    $offer = new Model_Offer();
    $offer->category = new Model_Category();
    
    $offer->save();
    

    Not sure why it's throwing this error - I thought this error was related to passing ORM data to a view? Thanks
    Fuel v1.2
    Model is extending \Orm\Model in a module
    

    COREPATH/bootstrap.php @ line 48
    43});
    44
    45set_error_handler(function ($severity, $message, $filepath, $line)
    46{
    47    load_error_classes();
    48    return \Error::error_handler($severity, $message, $filepath, $line);
    49});
    50
    51function setup_autoloader()
    52{
    53    Autoloader::add_namespace('Fuel\\Core', COREPATH.'classes/');
    
    COREPATH/classes/database/connection.php @ line 471
    466                return $value->value();
    467            }
    468            else
    469            {
    470                // Convert the object to a string
    471                return $this->quote((string) $value);
    472            }
    473        }
    474        elseif (is_array($value))
    475        {
    476            return '('.implode(', ', array_map(array($this, __FUNCTION__), $value)).')';
    
    COREPATH/classes/database/query/builder/insert.php @ line 168
    163                        // Use the parameter value
    164                        $group[$i] = $this->_parameters[$value];
    165                    }
    166                }
    167
    168                $groups[] = '('.implode(', ', array_map($quote, $group)).')';
    169            }
    170
    171            // Add the values
    172            $query .= 'VALUES '.implode(', ', $groups);
    173        }
    
    COREPATH/classes/database/query.php @ line 228
    223            // Get the database instance
    224            $db = \Database_Connection::instance($db);
    225        }
    226
    227        // Compile the SQL query
    228        $sql = $this->compile($db);
    229
    230        switch(strtoupper(substr($sql, 0, 6)))
    231        {
    232            case 'SELECT':
    233                $this->_type = \DB::SELECT;
    
    PKGPATH/orm/classes/query.php @ line 1145
    1140     */
    1141    public function insert()
    1142    {
    1143        $res = \DB::insert(call_user_func($this->model.'::table'), array_keys($this->values))
    1144            ->values(array_values($this->values))
    1145            ->execute($this->connection);
    1146
    1147        // Failed to save the new record
    1148        if ($res[0] === 0)
    1149        {
    1150            return false;
    
    PKGPATH/orm/classes/model.php @ line 1031
    1026                $query->set($p, $this->{$p});
    1027            }
    1028        }
    1029
    1030        // Insert!
    1031        $id = $query->insert();
    1032
    1033        // when there's one PK it might be auto-incremented, get it and set it
    1034        if (count($primary_key) == 1 and $id !== false)
    1035        {
    1036            $pk = reset($primary_key);
    
    PKGPATH/orm/classes/model.php @ line 975
    970                }
    971            }
    972            $this->unfreeze();
    973
    974            // Insert or update
    975            $return = $this->_is_new ? $this->create() : $this->update();
    976
    977            $this->freeze();
    978            foreach($this->relations() as $rel_name => $rel)
    979            {
    980                if (array_key_exists($rel_name, $this->_data_relations))
    
    

    APPPATH/modules/uploader/classes/controller/create.php @ line 105
    100        $new_offer->modified_by = '1';
    101        
    102        // Set the required parameters for Category:
    103        $new_offer->category->name = 'On the Fly Category';
    104        
    105        $new_offer->save();
    106    }
    107}
    
  • ** moved to the ORM forum **
  • From the error it looks like ->category is seen as a column name, and not as a relation?
  • Well, category is both.
    There is a column on the $offer titled 'category' that links to the 'category_id':
    protected static $_has_one = array(
            'category' => array(
                'key_from' => 'category',
                'model_to' => 'Uploader\Model_Category',
                'key_to' => 'category_id',
                'cascade_save' => true,
                'cascade_delete' => false,
            )
        );
    

    I tried with and without the Model_Category having many offers:
    protected static $_has_many = array(
            'offers' => array(
                'key_from' => 'category_id',
                'model_to' => 'Uploader\Model_Offer',
                'key_to' => 'category',
                'cascade_save' => true,
                'cascade_delete' => false,
            )
        );
    
  • You can't have both. As both columns and relations are properties of the model object, it is either a column or a relation. And a column has preference over a relation. So you're trying to assign a model object to a column property, which is converted to string when you want to insert the record. Hence the error.
  • But I removed it from the Model properties.
    It's a column in the table, but in the Model I'm only treating it as a identifier linking to the categories table. In my $_properties there is no 'category' listed.
    protected static $_properties = array(
    'offer_id',
    'sku',
    'barcode',
    'status'
    );
    
  • Still hammering away on this issue.. I'm sure I'm missing something obvious, but I can't seem to figure it out.
    I have made sure that my 'key_from' is a column in the table, but it's not a property in the model. It works on the other Models, but not in this particular situation.
    It wouldn't have anything to do with the pluralization of category would it?
  • Ok.. I finally figured this one out. I feel stupid, but I also didn't understand the reasoning here. I had to change the type from "has_one" to "belongs_to". I understand why belongs to makes more sense, but I don't understand how "has_one" doesn't work, but belongs_to does.. I would have assumed they both would have worked similarly. Anyways, sounds like I'm just stupid for not knowing that an item would "belong" to a category instead of "having" a category.

Howdy, Stranger!

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

In this Discussion