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

Guys, please help me to understand Models and returning data to views

Hey,

So I have a few issues setting up my application.

First, I created a model and:

use Phalcon\Mvc\Model;

class TelegraphGroup extends Model {
    protected $id;
    protected $name;
}
  1. It wants me to implement getConnectionService, setForceExists etc.. nothing like that in documentation. What is it?
  2. When I try to instantiate a model in my controller "$model = new TelegraphGroup();" it says "Fatal error: Class 'TelegraphGroup' not found" Though in my loader.php I have this:
$loader->registerDirs(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->pluginsDir
    )
)->register();

Models are registered.. :/

Second, Lets say I have a form built within a view:

{% block content %}
    <form role="form" id="form-group-add" action="/groups/create">
        <div class="form-group">
            <label for="group-name">Название группы</label>
            <input type="text" class="form-control" id="name" name="name">
            <div class="form-error"></div>
        </div>
        <button type="submit" class="btn btn-default">Создать</button>
    </form>
{% endblock %}
  1. How can I use Phalcon filters and validation in controller if my form wasn't created with Phalcon's form object? I don't want to write weird validations with php and not using the pwoer of Phalcon....
  2. My form goes to /groups/create, which adds the record to database and now I want to go back to the same "add" view and tell the user that It's all right or there's an error, how can i properly redirect back with some new variable values injected into the view?

Thanks guys!



3.2k
Accepted
answer
edited Mar '14

.

It wants me to implement getConnectionService, setForceExists etc.. nothing like that in documentation. What is it?

Never saw that error - did you set a db service in the dependency injector?

$di->setShared('db', function() {
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => $config->database->host,
        "username" => $config->database->username,
        "password" => $config->database->password,
        "dbname" => $config->database->name,
    ));
});
When I try to instantiate a model in my controller "$model = new TelegraphGroup();" it says "Fatal error: Class 'TelegraphGroup' not found" Though in my loader.php I have this:

Can you please share your config and how you define it?

How can I use Phalcon filters and validation in controller if my form wasn't created with Phalcon's form object? I don't want to write weird validations with php and not using the pwoer of Phalcon....

You can use validation (https://docs.phalcon.io/en/1.2.6/reference/validation.html), either by creating a Phalcon\Validation object in the controller and then adding rules, or by defining your own class MyValidation and then calling MyValidation::validate() in the controller

My form goes to /groups/create, which adds the record to database and now I want to go back to the same "add" view and tell the user that It's all right or there's an error, how can i properly redirect back with some new variable values injected into the view?

I think you can use flashsession for that:

https://docs.phalcon.io/en/latest/api/Phalcon_Flash_Session.html

edited Mar '14

Yeah, my config:

<?php

return new \Phalcon\Config(array(
    'database' => array(
        'adapter'     => 'Mysql',
        'host'        => 'localhost',
        'username'    => 'webdb',
        'password'    => 'xxx',
        'dbname'      => 'xxx',
    ),
    'application' => array(
        'name'          => 'Xxx',
        'controllersDir' => __DIR__ . '/../../app/controllers/',
        'modelsDir'      => __DIR__ . '/../../app/models/',
        'viewsDir'       => __DIR__ . '/../../app/views/',
        'pluginsDir'     => __DIR__ . '/../../app/plugins/',
        'libraryDir'     => __DIR__ . '/../../app/library/',
        'cacheDir'       => __DIR__ . '/../../app/cache/',
        'vendorPath'     => __DIR__ . '/../../vendor/',
        'baseUri'        => 'https://catechize.com/',
    ),
    'steam' => array(
        'apiKey' => 'Xxx',
        'identity' => 'https://steamcommunity.com/openid/?l=english'
    ),
    'openid' => array(
        'domain' => 'https://www.xxx.com'
    )
));

And my services.php:

<?php

use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;

$di = new FactoryDefault();

// Store config in DI
$di->set('config', $config);

$di->set('url', function () use ($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);

    return $url;
}, true);

$di->set('openIdDecorator', function() use ($di) {
    $openId = new OpenIdDecorator($di);

    return $openId;
});

$di->set('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_',
                'stat' => true,
                'compileAlways' => true
            ));

            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));

    return $view;
}, true);

$di->set('db', function () use ($config) {
    return new DbAdapter(array(
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname
    ));
});

$di->set('modelsMetadata', function () {
    return new MetaDataAdapter();
});

$di->set('session', function () {
    $session = new SessionAdapter();
    $session->start();

    return $session;
});

P.S I can't fix the php highlight for second file.. it works weirdly here.

edited Mar '14

Guys seriously, what is this issue with Model that wants me to extend some weird methods and nto being able to expose Model in Controller? I'm using 1.3.0 btw..

I don't see anything wrong with your code, so i can only guess :) maybe your mysql user doesn't have enough permissions? As i was saying before, I do not have this problem, and my code is similar to yours, so the problem could be outside phalcon. I'm sorry i can't help more than that.