1

I'm introducing to Phalcon, a php framework, by following the tutorial: https://docs.phalconphp.com/en/latest/reference/tutorial-rest.html

I'm facing with this problem: I have this error in the code below

class Robots must be declared abstract or implement methods 'getConnectionService(), setForceExists() etc..'

<?php

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Message;
use Phalcon\Mvc\Model\Validator\Uniqueness;
use Phalcon\Mvc\Model\Validator\InclusionIn;

class Robots extends Model{


public function validation()
{
    // Type must be: droid, mechanical or virtual
    $this->validate(
        new InclusionIn(
            array(
                "field"  => "type",
                "domain" => array(
                    "droid",
                    "mechanical",
                    "virtual"
                )
            )
        )
    );

    // Robot name must be unique
    $this->validate(
        new Uniqueness(
            array(
                "field"   => "name",
                "message" => "The robot name must be unique"
            )
        )
    );

    // Year cannot be less than zero
    if ($this->year < 0) {
        $this->appendMessage(new Message("The year cannot be less than zero"));
    }

    // Check if any messages have been produced
    if ($this->validationHasFailed() == true) {
        return false;
    }
}
} 
?>

And even if I try to execute an HTTP request i get:

Cannot instantiate abstract class Robots

Any ideas?

cventr
  • 186
  • 1
  • 12
  • Add: use Phalcon\Mvc\Model; before declaring the class. – Cerad Aug 29 '15 at 18:31
  • Yes, sorry, I omitted that part. Uploaded it, but the problem is still there. – cventr Aug 30 '15 at 08:23
  • Have you defined your DB connection service from next tutorial step? – yergo Sep 02 '15 at 11:30
  • Yes. I think it is a problem of configuration between phalcon and phpStorm. Including in the path phalcon-tools/ide/2.0.7 instead of phalcon-tools/ide/phpStorm everything works well.. – cventr Sep 02 '15 at 14:16

1 Answers1

0

You probably didn't set your database service properly. Make sure that:

  • The service name is db (I've experienced problems changing the default services names)
  • You have set the db service in your main $di (some might instantiate another DI container and isolate the database service there)
  • Always use $di->setShared(...) for registering global services

Please provide more information and will be glad to help.

cvsguimaraes
  • 12,910
  • 9
  • 49
  • 73
  • As I said before, probably it's a configuration problem between phalcon and phpStorm. Including in the path phalcon-tools/ide/2.0.7 instead of phalcon-tools/ide/phpStorm everything works well.. – cventr Sep 04 '15 at 17:13
  • I've read that, but I hardly think so... `phalcon-tools/ide/2.0.7` are just stubs for auto-completion and `phalcon-tools/ide/phpStorm` just adds support for code generation ([more details here](http://stackoverflow.com/questions/12284141/how-do-i-use-phalcon-devtools-ide-phpstorm-in-phpstorm/12285007)). So, unless you following that tutorial using the command line to create the models and stuff, it's unlikely that those includes have something to do with it... – cvsguimaraes Sep 04 '15 at 18:25