1
votes

I'm new to Phalcon and would like to create a PHP web-service on my WAMP server. I have a table called "coreswings" in MySQL database and it represents the cores and wings of a large building. There are five fields: abbr, name, type, busyFrom, busyTo.

Following the tutorial on http://docs.phalconphp.com/en/latest/reference/tutorial-rest.html , I can route my requests to my desired functions, but the phql, "SELECT * FROM coreswings", doesn't work and returns me fatal errors.

index.php
<?php
/*###########################################################################
########## Set up connection to be used by model CoresWings, start ##########
###########################################################################*/

// use Loader() to autoload the model
$loader = new \Phalcon\Loader();

$loader->registerDirs(array(__DIR__.'/models/'))->register();

$di = new \Phalcon\DI\FactoryDefault();

// set up the database service
$di->set('db', function(){
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "localhost",
        "username" => "user",
        "password" => "user_pw",
        "dbname" => "map"
    ));
});

// create and bind the DI to the application
$app = new \Phalcon\Mvc\Micro($di);

/*#########################################################################
########## Set up connection to be used by model CoresWings, end ##########
#########################################################################*/

/*#########################################################
########## create routes according to api, start ##########
#########################################################*/

// get all cores and wings
$app->get('/coresWings', function() use ($app){
    $phql = "SELECT * FROM coreswings";
    $coresWings = $app->modelsManager->executeQuery($phql);

    $data = array();
    foreach($coresWings as $coreWing){
        $data[] = array(
            'abbr' => $coreWing->abbr,
            'name' => $coreWing->name,
            'type' => $coreWing->type,
            'busyFrom' => $coreWing->busyFrom,
            'busyTo' => $coreWing->busyTo,
        );
    }

    echo json_encode($data);
});

// testing purpose
$app->get('/testing', function(){
    $data = array(
        'function' => 'tesing',
        'data' => '001'
    );

    echo json_encode($data);
});

/*#######################################################
########## create routes according to api, end ##########
#######################################################*/

$app->handle();

?>

When I access the URL http://localhost/FYP/001/api/coresWings, the following errors are shown:

( ! ) Fatal error: Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'Table "cores_wings" doesn't exist on database when dumping meta-data for CoresWings' in D:\Program Files\wamp\www\FYP\001\api\index.php on line 39 ( ! ) Phalcon\Mvc\Model\Exception: Table "cores_wings" doesn't exist on database when dumping meta-data for CoresWings in D:\Program Files\wamp\www\FYP\001\api\index.php on line 39

Of course I don't have a table called "cores_wings", but my phql is "SELECT * FROM coreswings". Please tell me if I have done anything wrong. Thanks so much.

#

mysql> describe coreswings; +----------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+-------+ | abbr | varchar(63) | NO | PRI | NULL | | | name | varchar(1024) | YES | | NULL | | | type | enum('core','wing') | NO | | NULL | | | busyFrom | time | YES | | NULL | | | busyTo | time | YES | | NULL | | +----------+---------------------+------+-----+---------+-------+

Model: CoresWings.php

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

class CoresWings extends Model{

    public function validation(){
        // building type must be "core" or "wing"
        $this->validate(new InclusionIn(
            array(
                "field" => "type",
                "domain" => array("core", "wing")
            )
        ));

        // building abbreviation must be unique
        $this->validate(new Uniqueness(
            array(
                "field" => "abbr",
                "message" => "Abbreviation of a building must be unique"
            )
        ));

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

?>
2
Hmm whats your CoresWings model? - jodator
Added the description of table coreswings and the php model. Thanks. - chunchiu Chan

2 Answers

0
votes

You can use Phalcon\Mvc\Model::getSource() method. It is table name map with manual.

0
votes

If you want minimal configuration then you should follow some PhalconPHP's conventions.

One of them is that it for CamelCaseModel PhalconPHP uses camel_case table.

If you want to set different tabel for model then you should set it's source:

class CoresWings extends Model{
    public function initialize() {
        $this->setSource('coreswings');
    }
}