MODEL->save() error

Published 9 months ago by TomyLimon

Hi. I have strange situation. I have model Document and problem is

$doc = Document::find(2); $doc->save();

as result I'm getting error

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update documents set 0 = {"id":4,"document_id":2,"unit_id":107,"unit_name":"Balons no komp. materi\u0101la, 6,8 l / 300bar, 30 gadi, ar AZA ventili ar av\u0101rijas nopl\u016bdes

Problem is field list in SQL is taken from different table. I can't understand how/whie....

Best Answer (As Selected By TomyLimon)
thomaskim

I think the main problem is that he's overriding the getAttributes method. This breaks several internal methods.

I would rename getAttributes() to something else like getDocumentAttributes().

Dry7
Dry7
9 months ago (151,680 XP)

@TomyLimon show Document class

TomyLimon
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;
use App\Models\DocumentsAttribute;

class Document extends Model {
    public $timestamps = false;

    protected $fillable = [
        'document_number', 'document_date', 'type_id', 'document_status', 'owner_id', 'partner_id', 'delivery_address',
        'transport_company','transport_car', 'transport_driver', 'contract_type','payment_type', 'bank_id', 'sign_by',
        'language', 'currency',
        'created_by'
    ];



    public function getAttributes() {
        return DocumentsAttribute::where('document_id', $this->id)->orderBy("id")->get();
    }

}
Snapey
Snapey
9 months ago (896,855 XP)

I'm guessing this is returning 0

It possibly also ought to return an array?

    public function getAttributes() {
        return DocumentsAttribute::where('document_id', $this->id)->orderBy("id")->get();
    }
thomaskim

I think the main problem is that he's overriding the getAttributes method. This breaks several internal methods.

I would rename getAttributes() to something else like getDocumentAttributes().

Snapey
Snapey
9 months ago (896,855 XP)

@thomaskim I don't really get the pattern but I think he is storing multiple document types in one table, and using another table to store the names of the columns that have to be updated for any given document

TomyLimon

@thomaskim Many thanks. I change function name and all fine now.

Please sign in or create an account to participate in this conversation.