| ||||||||||
Shopping cart software Solutions for online shops and malls | ||||||||||
|
X-Cart Home | FAQ | Forum rules | Calendar | User manuals | Login |
Database Queries In Functions? | |||
|
|
Thread Tools | Search this Thread |
#1
|
|||||||
|
|||||||
Database Queries In Functions?
Does anyone know the best way to execute SQL queries inside a function call?
I need to query the database get results then process data from query A to build query B. Then once done I will return the results by leaving the function call. I am not sure of the safest way to query the database that won't break with X-Cart upgrades. I can't find a developer help document at http://kb.x-cart.com/ explaining the ways on working with database SQL queries in function calls. Any thoughts? Thank you! For example: Code:
__________________
Respectfully, Jeremy X-Cart 5.3 running on: * Web server: Nginx with PHP-FPM via FastCGI (Opcache enabled) * Database: Percona Server * OS: CentOS |
|||||||
#2
|
|||||||||
|
|||||||||
Re: Database Queries In Functions?
There are many 'built in' database functions in Xcart. Look in the file include/func/func.db.php
__________________
Richard Williams Rogue Wave Limited Initial Inventory Imports Daily Inventory Updates Daily Inventory Reports Drop Ship Support Order Export to Shipper/Supplier Shopping Feeds That Work Programming for X-Cart richard@roguewavelimited.com http://www.roguewavelimited.com |
|||||||||
#3
|
|||||||||
|
|||||||||
Re: Database Queries In Functions?
I believe Jeremy is asking about X-Cart 5 that does not have the include/func/func.db.php file.
Instead of working with direct SQL queries X-Cart uses Doctrine 2 ORM library that constructs such queries from objects. Please check this article on retrieving data from the database: http://kb.x-cart.com/display/XDD/search%28%29+method Also, you may explain here what query you need and I will try to describe how you can achieve it.
__________________
Alex Solovev, Qualiteam --- User manual Video tutorials X-Cart FAQ You are welcome to press "Thanks" button if you find this post useful Click here to learn how to apply patches X-Cart Extensions |
|||||||||
#4
|
|||||||
|
|||||||
Re: Database Queries In Functions?
Thanks for the feedback. It is correct I am trying to execute custom queries via X-Cart 5.
My queries are to custom tables I have added to the xcart database. As example how would you handle. Code:
Thank you!
__________________
Respectfully, Jeremy X-Cart 5.3 running on: * Web server: Nginx with PHP-FPM via FastCGI (Opcache enabled) * Database: Percona Server * OS: CentOS |
|||||||
#5
|
|||||||||
|
|||||||||
Re: Database Queries In Functions?
Hello Jeremy,
You should create a new Model class and a new Repository class for that model. This article should help you: http://kb.x-cart.com/display/XDD/Creating+new+entity+--+Introduction+of+editable+ItemsList+in+admin+area
__________________
Alex Solovev, Qualiteam --- User manual Video tutorials X-Cart FAQ You are welcome to press "Thanks" button if you find this post useful Click here to learn how to apply patches X-Cart Extensions |
|||||||||
#6
|
|||||||
|
|||||||
Re: Database Queries In Functions?
Thanks for the reply. It takes a lot of files just to get a database query to run.
I am still not completely sure how you would take the results from query A, parse it via PHP, then run query B, parse that information, then display to the browser (visitor). I guess one needs multiple Model classes. I wish I could just run a query via: Code:
Get results in a row Code:
Then from that: Code:
Code:
__________________
Respectfully, Jeremy X-Cart 5.3 running on: * Web server: Nginx with PHP-FPM via FastCGI (Opcache enabled) * Database: Percona Server * OS: CentOS |
|||||||
#7
|
|||||||||
|
|||||||||
Re: Database Queries In Functions?
For each table you need its own model class and the repository class. And you are not to create the SQL table manually - X-Cart 5 will create it from your Model class automatically.
Model classes define table columns and methods to work with values stored in a single entity, while the repository class provides methods to search/retrieve model classes. You should avoid using direct SQL queries as this makes your code less future-proof and less customization-friendly. PHP Code:
When using model and repository classes this will look as follows: PHP Code:
For more complex queries you may use a custom search() method (see the example in the article). If you have two linked tables you should look into using @ManyToOne, @OneToMany and @OneToOne relations. X-Cart 5 uses Doctrine 2 ORM library, so most of these will work for X-Cart 5 too: - http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html - http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html You may also look for real examples in the classes\XLite\Model\ directory.
__________________
Alex Solovev, Qualiteam --- User manual Video tutorials X-Cart FAQ You are welcome to press "Thanks" button if you find this post useful Click here to learn how to apply patches X-Cart Extensions |
|||||||||
|
|
|||
X-Cart forums © 2001-2020
|