Jump to content

Using a repository to extract data from DB


Recommended Posts

Hi,

I was reading the official docs and i saw that the encouraged way for interacting with the DB on a module is by creating a repository and declare it as a service.

https://devdocs.prestashop.com/1.7/modules/concepts/hooks/use-hooks-on-modern-pages/#using-doctrine-dbal-to-retrieve-data

My doubt is in which folder should this repostiory be created, I assume I can do it within my module folder right?

Docs code declares that it should be created on '/src/Repository', and this may seem dumb but, I assume it's reffering to create a folder inside my module called 'src'? I'm confused since on the doc about folder module structure that isn't a common option (https://devdocs.prestashop.com/1.7/modules/creation/module-file-structure/).

Can someone give some insight as to what does the documentation mean?

Thanks

 

Link to comment
Share on other sites

Hi @GaimZz,

the docs are a little bit outdated on this point, it depends whether you plan to use Composer to manage your classes or not.
If you don't, it's almost impossible to declare and/or use the PrestaShop 1.7 services in your module.

If you do... well, we don't have docs "yet" to help you, but you can take a look at this module: https://github.com/friends-of-prestashop/masterclass

Hope it helps,

Edit: some docs here => https://devdocs.prestashop.com/1.7/modules/concepts/controllers/admin-controllers/#example-using-psr-4-namespacing

Mickaël

Edited by Mickaël Andrieu (see edit history)
Link to comment
Share on other sites

Thank you for your insight!

I'd like to ask another question about managing my classes with composer. Once I have the composer.json file declared as docs explain, is there anything else needed or will it be ready to install?

Thanks

Link to comment
Share on other sites

You have one extra task to do: generate the autoloader and the dependencies (if you have some).

  • 0 dependencies: composer dump-autoload (at the root of your module)
  • x dependencies: composer install (at the root of your module)

Or, if you plan to manage your module itself as a dependency of your project:

  • composer require your/module (will be installed in modules folder with its autoloader and dependencies)

When you ship your module with  a release of this module, you can optimize the generated autoloader: take a look at the Composer's documentation => https://getcomposer.org/doc/articles/autoloader-optimization.md

Mickaël

Link to comment
Share on other sites

  • 1 month later...
On 3/5/2019 at 9:11 PM, Mickaël Andrieu said:

You have one extra task to do: generate the autoloader and the dependencies (if you have some).

  • 0 dependencies: composer dump-autoload (at the root of your module)
  • x dependencies: composer install (at the root of your module)

Hi Mickaël,

Sorry for the late question but, doesn't this mean that if I want to distribute this module it wouldn't work unless 'composer dump-autoload' is done, therefore modules using this feature wouldn't be able to be distributed right?

Is this just ignored or does prestashop automatically regenerate if needed on install or something like that?

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...