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

In Phalcon where do we have to write the sql queries?

Pls help me. In my phalcon, i need to do get some data joining two tables, but i dont know where do i have to write the query, in which file?

edited Apr '15

Anywhere, where you need the data, for instance in an action:

class MyController extends \Phalcon\Mvc\Controller
{
    public function someAction()
    {
          $data = $this->db->query("SELECT * ... ")->fetchAll();
    }
}

Maybe you don't write the query. If you have built your model (see https://docs.phalcon.io/en/latest/reference/models.html) including relationship definitions e.g. in Robots $this->hasMany("id", "RobotsParts", "robots_id"); and in RobotParts $this->belongsTo("robots_id", "Robots", "id");, you can access, in this case, the Robot xyz parameter from the RobotParts using $robot_part->robots->xyz. You can also retrieve the robot parts from for a specific robot using $robotsParts = $robot->robotsParts;. It's more conveneint than writing sql queries and if your table change you only need to change to model, not every instances you used a query.