SoftDelete Configuration : Phalcon 3.x

Hi,

I got an error while configuring SoftDelete in the base model. I am using phalcon 3.x

    namespace PhalconRest\Models;
    use Phalcon\Mvc\Model\Behavior\SoftDelete;
    use Phalcon\Mvc\Model;

    class ModelBase extends Model {
        public function initialize() {
           $softDelete = new SoftDelete([
                    'field' => 'is_deleted',
                    'value' => 1
                ]);
           $this->addBehavior($softDelete);
       }
    }

I did according to doc. Please let me know if I am missing any Interface or incorrect calls.

Raja K

Can you send an error?



10.6k
edited Feb '19

Hi,

Thanks. I have shared the code.

    namespace PhalconRest\Models;
    use Phalcon\Mvc\Model\Behavior\SoftDelete;
    use Phalcon\Mvc\Collection as Collection;;
    class ModelBase extends Collection {
      public function initialize() {
        $softDelete = new SoftDelete([
                    'field' => 'is_deleted',
                    'value' => 1
                ]);
        $this->addBehavior($softDelete);
      }
    }

Below is the error log.,

 [Mon Jan 21 18:35:09.754977 2019] [php7:error] [pid 5791] [client 103.253.168.194:30040] PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Phalcon\\Mvc\\Collection::addBehavior() must implement interface Phalcon\\Mvc\\Collection\\BehaviorInterface, instance of Phalcon\\Mvc\\Model\\Behavior\\SoftDelete given in /var/www/html/test/admin/backend/api/models/ModelBase.php:17\nStack trace:\n#0 /var/www/html/thanthiram/admin/backend/api/models/ModelBase.php(17): Phalcon\\Mvc\\Collection->addBehavior(Object(Phalcon\\Mvc\\Model\\Behavior\\SoftDelete))\n#1 [internal function]: PhalconRest\\Models\\ModelBase->initialize()\n#2 [internal function]: Phalcon\\Mvc\\Collection\\Manager->initialize(Object(PhalconRest\\Models\\User))\n#3 [internal function]: Phalcon\\Mvc\\Collection->__construct()\n#4 /var/www/html/test/admin/backend/api/controllers/UserController.php(111): Phalcon\\Mvc\\Collection::find()\n#5 [internal function]: PhalconRest\\Controllers\\UserController->listuser()\n#6 [internal function]: Phalcon\\Mvc\\Micro\\LazyLoader->__call('listuser', Array)\n#7 [internal function]: Phalcon\\Mvc\\Micro\\LazyLoader->callMethod('listuser', Array, NU in /var/www/html/test/admin/backend/api/models/ModelBase.php on line 17
edited Jan '19

As I understand you are using nosql ?

Try change Phalcon\Mvc\Model\Behavior\SoftDelete to Phalcon\Mvc\Collection\Behavior\SoftDelete



10.6k
edited Jan '19

Hi,

Yes. Sure, I have changed it and I got another exception from the below file at line 452.

https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Db/Adapter/MongoDB/Collection.php

PHP Fatal error:  Uncaught Error: Call to undefined function #0 /var/www/html/test/admin/backend/api/library/Phalcon/Db/Adapter/MongoDB/Collection.php(452): MongoDB\\Driver\\Manager->selectServer(Object(MongoDB\\Driver\\ReadPreference))\n#1 [internal function]: Phalcon\\Db\\Adapter\\MongoDB\\Collection->find(Array)\n#2 [internal function]: Phalcon\\Mvc\\Collection::_getResultset(Array, Object(PhalconRest\\Models\\User), Object(Phalcon\\Db\\Adapter\\MongoDB\\Database), false)\n#3

Raja K

edited Jan '19

Sorry, but I bad know nosql.

I just see what you are using Model\Behavior instead of Collection\Behavior

I think you should close whish post, and create new one with this error.



10.6k

Hi,

Thank you so much. I do.

Raja K