Laravel 4 - the model is not saved

The presence of this code:

class SearchIndex extends Eloquent {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'searchindex';

    //no timestamps
    public $timestamps = false;

    //mass-assignment
    public $fillable = array('url', 'content', 'version', 'digest');
    public $guarded = array();

    public static function updateIndex($url, $version, $content)
    {
        self::retrieveSearchEntry($url, $version)->updateContent($content)->save();
    }

    public static function retrieveSearchEntry($url, $version)
    {
        try
        {
            return self::withoutContent()->where('url', $url)->where('version', $version)->firstOrFail(array('url', 'version', 'digest'));
        }
        catch(\Illuminate\Database\Eloquent\ModelNotFoundException $e)
        {
            return new SearchIndex(array('url' => $url, 'version' => $version));
        }
    }

    public function updateContent($content)
    {
        $hash = md5($content);
        if (!isset($this->digest) || ($this->digest != $hash))
        {
            $this->content = $content;
            $this->digest = $hash;
        }
        return $this;
    }

    public static function search($content)
    {

    }
}

If I call updateIndex, providing a new combination of url + version, an entry is created.
If I call updateIndexby providing an existing url + version, pair , the record is not updated with new content . I see that this has something to do with the fact that I omit the "content" field (reason: it is huge, and I want to set it, not get it in this function).

a question . How can I not get the content field, but be able to set it when saving?

+4
source share
1 answer

. . "id" firstOrFail(). , id , SQL ( ), id - NULL, 0 .

+2

Source: https://habr.com/ru/post/1534949/


All Articles