1
irmtfan
Xoops Core class criteria limitations LEFT JOIN and WHERE EXISTS
  • 2012/10/15 6:49

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


The criteria class in xoops core can provide some API for better coding.
it is located in xoops255/class/criteria.php

for example you can add clauses like this:
$mycriteria = new CriteriaCompo();
$mycriteria->add(new Criteria('uid''(' implode(','$userid) . ')''IN'));
$mycriteria->add(new Criteria('subject'NULL'IS NOT NULL'), 'OR');


but i found some limitations in it:
1- Can not add a table by "LEFT JOIN"
2- Can not accept "EXISTS" and "NOT EXISTS" in WHERE clause.
see this:
http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html

i need to add WHERE NOT EXISTS(criteria) in newbb.

So my question is am i right and there is no way for the above in the current class?

Also if you find other limitations please express because maybe we can send it to feature request.

2
zyspec
Re: Xoops Core class criteria limitations LEFT JOIN and WHERE EXISTS
  • 2012/10/15 15:33

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


irmtfan,

There is some join functionality available through the object handler functions by using the '...byLink' methods in the object handler. There are a couple of 'unique' steps required to get them to work as expected. Look at the comments in ./class/model/joint.php. You will have to initialize the 'table_link', 'field_link', and 'field_object' vars before you can use the '...byLink' methods.

Can you send me an example of the query you want to use that contains the "EXISTS" (or "NOT EXISTS")? I can look to see if I can find a way to make it work. I'm not sure there's a 'clean' implementation without extending the XoopsModelRead class.

3
irmtfan
Re: Xoops Core class criteria limitations LEFT JOIN and WHERE EXISTS
  • 2012/10/16 4:48

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


Thank you zyspec!
You are the master of XOOPS CORE API.
I take a look at that class but it seems it also have some limitations. it just accept one LEFT JOIN and no INNER JOIN.
as for WHERE EXISTS i decide to not use it in newbb and use some other more efficient queries.

XOOPS CORE and Modules are full of examples of "LEFT JOIN"
in newbb i have this:
// this code in class/post.php
        
$sql 'SELECT p.*, t.* '.
                
' FROM ' $this->db->prefix('bb_posts') . ' AS p'.
                
' LEFT JOIN ' $this->db->prefix('bb_posts_text') . " AS t ON t.post_id = p.post_id";

// other codes in viewpost.php in different places
$criteria_post = new CriteriaCompo(new Criteria("p.forum_id""(".implode(",",$access_forums).")""IN"));
$criteria_post->setSort("p.post_id");
$criteria_post->setOrder($order);
        
$criteria_post->add(new Criteria("p.approved"1)); // irmtfan uncomment

                   
$join ' LEFT JOIN ' $xoopsDB->prefix('bb_reads_topic') . ' AS r ON r.read_item = p.topic_id AND r.uid = ' $read_uid ' '// irmtfan corrected add AS
                
$criteria_status_post = new CriteriaCompo();// irmtfan new criteria
                
$criteria_status_post->add(new Criteria("p.post_id""r.`post_id`"">")); // irmtfan corrected - should use $value="r.``" to render in XOOPS/class/criteria.php
                
$criteria_status_post->add(new Criteria("r.read_id"NULL"IS NULL"), "OR");// irmtfan corrected - should use "IS NULL" to render in XOOPS/class/criteria.php
            
$criteria_post->add($criteria_status_post); // irmtfan add the status criteria to post criteria

$sql and $join are hardcoded in the above codes.
There are 2 LEFT JOIN:
the first left join select only one row from bb_posts_text table (very huge table in my website it is 400MB)
the second left join only select one row from bb_reads_topic.( maybe large number of rows one row = one topic per user)

As you can see queries in newbb are complicated.
an example of above query is:
0.210036 SELECT p.*, t.* FROM bb_posts AS p LEFT JOIN bb_posts_text AS t ON t.post_id p.post_id LEFT JOIN bb_reads_topic AS r ON r.read_item p.topic_id AND r.uid 1 WHERE (p.forum_id IN (1,2,4) AND p.approved '1' AND (p.post_id r.`post_id` OR r.read_id IS NULL) ) ORDER BY p.post_id DESC LIMIT 05


As you can see we can consider that running time high as 0.210036 seconds

so the real and specific question is:
is there any better query than above? can we achieve better performances by using XOOPS CORE getByLink($criteria)?

Login

Who's Online

164 user(s) are online (140 user(s) are browsing Support Forums)


Members: 0


Guests: 164


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits