简体   繁体   中英

Objects with recursive properties in php

I'm using a class, moduleSelectQuery , to generate SQL queries in PHP.

Basically, this class breaks up the individual components of a SQL SELECT query, such as the table name, the fields to select, the WHERE conditions, etc.

However, this quickly becomes complicated with nested queries, such as an WHERE table1.field1 IN (SELECT table2.field2 from table2 WHERE table2.field3 = criteria)

Currently I have a property for moduleSelectQuery called $inWhereClause that is used to store the WHERE... IN(SELECT...) clause. Like the other properties (ie $tableName, $whereClause, $havingClause ), this is parsed together by its own function based on user input.

However, this parsing function is fundamentally limited. Even if I devote enough effort to it as I do parsing the $whereClause property, it can't have additional nested select statements.

I think one way to do this would be to set $inWhereClause to be another moduleSelectQuery object. This would mean that the parent moduleSelectQuery would have a property which was itself a moduleSelectQuery , ie it would be a recursive object. Is this possible/good practice in PHP? Are there other drawbacks?

I could see this being a possible solution. Visualize having a Person class. A person might have a child, which is a person. Doesn't seem unreasonable.

I wouldn't necessarily call this a recursive object, as the object doesn't reference itself, but rather that an object can have a property that is an instance of the same class.

It would, obviously need to be nested appropriately, as only sub-queries in the Where statement would need to be moduleSelectQuery object, where simple comparisons would be instances of strings, or integers.

The only drawbacks that can arrive from this approach are in the design of the class, and methods. I do not see any glaring performance issues, or maintainability issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM