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

Trying to make Tutorial 7 Creating A Simple REST API, work

I'm trying to get the Tutorial 7 working, https://docs.phalcon.io/en/latest/reference/tutorial-rest.html and I can't figure out what to do with a code chunk in the tutorial. Specifically where it says, "Now, we must set up a connection to be used by this model and load it within our app:" Where does the code that follows go? It never indicates a file it should be put in or even added to an existing file. What do I do with that code?

Additionally, the docs never indicate what the schema is for the database. As best I can figure out, it should be:

Database: Robotics Table: Robots Primary Key: ID Fields: name, type, year

Any suggestions?

edited Apr '16

That code goes in your bootstrap file (public/index.php). Phlacon will register that "db" service in the Dependency Injector (the $di).

So when you see $di->set() functions they are setting another dependency. In this case, they are setting a database dependency.

Since there is only one database depencacy defined in this example ( which is called db, it can actaully be called whatever you want, but for simplicity, they called it db) the model will automatically use that depenacy as its db connection.

So when you do something like

YourModel::find();

It will connect to your db using your connection, and then find all the records and return you an array of resultsets.

Hope that helps a bit.



2.2k

It's using the micro app so there isn't a public folder at all let alone an index.php in the public folder. Should I just put this at the end of the models class file Robots.php?

Oh yes, I should have read more carefully. I usually write my API's in full phalcon apps. Old habbits.

edited Apr '16

No. You should just create folder models in directory above public and put your all models there. And load them with loader.