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

setSource in the model initializer

There is an update:

 - The _source and _schema properties have been moved from Phalcon\Mvc\Model to Phalcon\Mvc\Model\Manager. This allow set source/schema in the model initializer

But method Model::setSource() which can be used in initialize() is deprecated. Also the method Model::setModelSource(string $source) is undefined. But, by analogy for Model\Manager::setConnectionService (Phalcon\Mvc\ModelInterface $model, string $connectionService) and others, it should represents Model\Manager::setModelSource (Phalcon\Mvc\Model $model, string $source) and be able to use in initializer.

So what exactly this update doing and how to use it in practice?



98.9k

With this change you can set your model's source in the model initializer:

<?php

class Robots extends Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->setSource('le_robots');
    }
}

However, the recommended way to reduce the overhead is using the getSource method:

class Robots extends Phalcon\Mvc\Model
{
    public function getSource()
    {
        return 'le_robots';
    }
}


32.5k
edited Jul '14

I'm aware of your's first example. I'm just confusing by the fact that the new feature's method is already deprecated =) Because of not the same as "not recommended". Maybe is a typo in docs API indice?

https://docs.phalcon.io/en/1.0.0/api/Phalcon_Mvc_Model.html

protected Phalcon\Mvc\Model setSource ()
Sets table name which model should be mapped (deprecated)

protected Phalcon\Mvc\Model setSchema ()
Sets schema name where table mapped is located (deprecated)


98.9k

Yes, we need to remove the deprecation note from the API. thanks for pointing out that!



17.0k
edited Jul '14

if necessary indicate the prefix to table, that straight in \Phalcon\Db\Adapter\Pdo\Mysql impossible? only so?

public function getSource()
{
    return $config['db.prefix'] . '_robots';
}