Phalcon sql query execution in model

Currently, I decided to look at Phalcon php as an alternative php framework for Codeigniter. I followed the tutorials on the website and it is very sweet how this works. I'm still trying to circle my head around a few things.

In my opinion, models are attached to the database and are mapped to a table in the database. I have a project where I need to use two or more databases. The project has a backend (one database) and several interfaces (another database). The easiest way is to run custom MySQL queries to retrieve data from multiple databases. I'm not sure how to do this with a model in Phalcon. I looked through stackoverflow, tried a few suggestions, but still no luck.

I would suggest that there should be some easy way to do this from the model, for example $ result = $ this-> query ("SELECT * FROM backend.users") → fetch (); but it does not work.

Here is what I have:

Controller:

class SignupController extends \Phalcon\Mvc\Controller{

  function indexAction()
  {

  }

  function registerAction()
  {
    $user = new Users();
    $result=$user->saveNewUser();
    print_r($result); // Nothing

    //$result=$this->db->query("SELECT * FROM phalcon.system_users")->fetchAll();
    //print_r($result); // Works

    $this->view->disable();
   }

}

Model:

class Users extends Phalcon\Mvc\Model
{
   public function saveNewUser()
   {
      return $this->db; // how to run the query???

   }
}

Bootstrap:

try {

//Register an autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
    '../app/controllers/',
    '../app/models/'
))->register();

//Create a DI
$di = new Phalcon\DI\FactoryDefault();

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

//Setup the view component
$di->set('view', function(){
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir('../app/views/');
    return $view;
});

//Setup a base URI so that all generated URIs include the "tutorial" folder
$di->set('url', function(){
    $url = new \Phalcon\Mvc\Url();
    $url->setBaseUri('/phalcon/');
    return $url;
});

//Handle the request
$application = new \Phalcon\Mvc\Application($di);

echo $application->handle()->getContent();

} catch(\Phalcon\Exception $e) {
     echo "PhalconException: ", $e->getMessage();
}

I liked how Codeigniter did it, but I'm not sure if Phalcon has an easy way to do this. Maybe I need to load an extension or library to do this in a model.

Thanks in advance!

+4
source share
2 answers

Thanks jodator,

But this is a little different that I need. I wanted to execute sql queries from Model.

, , . , - , mysql , . , , .

Model , BaseModel.php, :

class BaseModel extends Phalcon\Mvc\Model
{
   public $db;

   public function initialize()
   {
      $this->db=$this->getDi()->getShared('db');
   }   
} 

BaseModel Phalcon, , $db. initialize() $this- > getDi() → getShared ('db'), $this- > db. , BaseModel, . :

class Users extends BaseModel // Users extends out BaseModel and will have access to $db
{

   public function test()
   {
      //print_r(\Phalcon\Di::getDefault()->getShared('db')); // This is the ugly way to grab the connection.

      $result=$this->db->query("SELECT * FROM phalcon.system_users"); // Working now
      echo $result->numRows();
      print_r($result->fetchAll());
   }

}

. , , mysql (PDO) Phalcon. FETCH_ASSOC , , FETCH_ASSOC , , . . DI ....

//Setup the database service
$di->set('db', function(){
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "localhost",
        "username" => "root",
        "password" => "123456",
        "dbname" => "",
        'options' => [PDO::ATTR_CASE => PDO::CASE_LOWER, PDO::ATTR_PERSISTENT => TRUE,PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC],
    ));
});

, PDO:: ATTR_DEFAULT_FETCH_MODE.

- , .

, , :)

+4

config, :

//

$di->set('db', function(){ /* like in what you have */ });
// then the other one
$di->set('dbBackend', function(){ /* like in what you have */ });

db

public function initialize()
{
    parent::initialize();
    $this->setConnectionService('dbBackend');
    // or $this->setWriteConnectionService('dbB') and $this->setReadConnectionService('dbA')
}

$model->create()

, Pdo\Mysql Adapter.

$this->setSource() $this->setSchema().

+3

Source: https://habr.com/ru/post/1532916/


All Articles