We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Magic methods aren't working

My code throws an exeption "PhalconException: The method 'findFirstByTitle' doesn't exist on model 'Plant'"

How can I fix it? Code:

class Plant extends \Phalcon\Mvc\Model {

public $id;
public $title;

protected function beforeSave() {
    if(empty($this->id) && $plant = self::findFirstByTitle($this->title)) {
        $this->id = $plant->id;
        return false;
    }
}

}

P.S.: I also tried to use $this-> instead of self:: with no success. PHP version: 5.4.43 Phalcon version: 2.0.6

Thanks.



16.0k
Accepted
answer
edited Oct '15

try

self::findFirst("title = '$this->title'");


2.8k

Thanks, mate. Your solution doesn't provide escaping of a title, so I used this code: self::findFirst(array('title' => $this->title));