Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Database Queries In Functions?

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 10-01-2015, 12:39 PM
 
cleverwise cleverwise is offline
 

Advanced Member
  
Join Date: Jun 2014
Posts: 74
 

Default 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:
protected function doNoAction() { // Execute query A and get results // Now read queried results and build query B then run that query return $var1; }
__________________
Respectfully,

Jeremy

X-Cart 5.3 running on:
* Web server: Nginx with PHP-FPM via FastCGI (Opcache enabled)
* Database: Percona Server
* OS: CentOS
Reply With Quote
  #2  
Old 10-02-2015, 03:07 AM
  rogue's Avatar 
rogue rogue is offline
 

X-Adept
  
Join Date: Apr 2007
Location: Loveland, Ohio
Posts: 770
 

Default 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
Reply With Quote
  #3  
Old 10-02-2015, 03:41 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default 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
Reply With Quote
  #4  
Old 10-02-2015, 12:26 PM
 
cleverwise cleverwise is offline
 

Advanced Member
  
Join Date: Jun 2014
Posts: 74
 

Default 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:
select node_id,node_name,node_status from nodes where nodes_enabled='1';

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
Reply With Quote
  #5  
Old 10-05-2015, 05:32 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default 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
Reply With Quote
  #6  
Old 10-05-2015, 01:04 PM
 
cleverwise cleverwise is offline
 

Advanced Member
  
Join Date: Jun 2014
Posts: 74
 

Default 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:
$qb="select col1,col2,col3 from tableA where id='1'";

Get results in a row

Code:
foreach ($qb as $row) { $col1=$row[0]; $col2=$row[1]; // etc }

Then from that:

Code:
$qb="select colA,colB from tableB where id='$col1'";

Code:
foreach ($qb as $row) { $colA=$row[0]; // etc }
__________________
Respectfully,

Jeremy

X-Cart 5.3 running on:
* Web server: Nginx with PHP-FPM via FastCGI (Opcache enabled)
* Database: Percona Server
* OS: CentOS
Reply With Quote
  #7  
Old 10-05-2015, 08:19 PM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default 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:
$qb="select col1,col2,col3 from tableA where id='1'"

When using model and repository classes this will look as follows:
PHP Code:
$repo = \XLite\Core\Database::getRepo('\XLite\Modules\YOUR_ID\MODULE_ID\Model\YOUR_MODEL_CLASS');

foreach (
$repo->findById(1) as $entity) {
   
$col1 $entity->getColumn1(); // reading the $column1 property/column of the model
   
$col2 $entity->getName();     // reading the $name property/column of the model


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
Reply With Quote

The following user thanks qualiteam for this useful post:
cleverwise (10-06-2015)
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 10:11 AM.

   

 
X-Cart forums © 2001-2020