How do you call your own methods in a data model class in ThinkPHP5?

such as updating records, the official document is written like this. I now want to encapsulate this functionality in a class that inherits Model.

clipboard.png

clipboard.png

if you write this, you will report that there is no corresponding static method
self::allowField (true):: save ($companyInfo, ["id" = > $id]);

if you change self:: to $this- >, you will also get an error.

well, I don"t know how to write it. PHP Mengxin asks for advice.

Php
Aug.05,2021

function savedata($data,$id)
{
    return $this->allowField(['name','email'])->save($data,['id'=>$id]);
}

call

$res = $post->savedata(['name'=>'http://test','email'=>'testttttimg'],1);

execute sql statement

UPDATE `posts`  SET `name` = 'http://test' , `email` = 'testttttimg'  WHERE  `id` = 1

first take a look at the allowField method in Model.php in TP5

    /**
     * 
     * @access public
     * @param string|array $field  true
     * @return $this
     */
    public function allowField($field)
    {
        if (is_string($field)) {
            $field = explode(',', $field);
        }
        $this->field = $field;
        return $this;
    }

can you take a look at your model code? it feels like inheritance is not well written. Have you written the namespace correctly?

Menu