wp-orm icon indicating copy to clipboard operation
wp-orm copied to clipboard

Added column aliasing

Open jrfoell opened this issue 8 years ago • 0 comments

We often use tables that are outside of WP and are sometimes subjected to silliness such as column names with spaces that won't translate directly to PHP class properties. I added a rudimentary way of defining column aliases that seems to work well enough for Models and Queries in WP-ORM. The properties will all exist as the aliased names and will only get transformed when the queries happen.

Here's an example model definition using aliases:

`

'__kp_ProductUUID', 'sku' => '__kp_ProductID', 'title' => 'ProductName', 'description' => 'ProductDescription', 'short_description' => 'ShortDescription', 'distributor_discount' => 'DistributorDiscount', 'image_url' => 'Structure_ImageURL', 'meta_cas_no' => 'CAS_No', 'meta_chemical_name' => 'ChemicalName', 'meta_iupac_name' => 'IUPAC_Name', 'meta_synonym' => 'Synonym', 'meta_formula_html' => 'FormulaTextHTML', 'meta_formula_wt' => 'FormulaWt', 'meta_melting_point' => 'MeltingPoint', 'meta_purity' => 'Purity', 'meta_solubility' => 'Solubility', 'meta_appearance' => 'Appearance', 'meta_stability' => 'Stability', 'meta_storage_temp' => 'StorageTempShortTerm', 'meta_shipping_temp' => 'ShippingTemp', 'meta_msds_url' => 'MSDS_URL', 'meta_references' => 'References', ); return $aliases; } public static function get_primary_key() { return 'uuid'; } public static function get_table() { global $wpdb; return "{$wpdb->prefix}wpi_product_information"; } public static function get_searchable_fields() { return array(); } ``` } ` Also, I had to remove the (int) cast on the primary key as I've also encountered tables that don't use a numeric primary key :-/ Please test and give feedback!

jrfoell avatar Mar 01 '16 21:03 jrfoell