Zend Select with self join overwriting fields - sql

Posts and comments are stored in the same table. So to get each post and its comments we do this:
$posts = $this->select()->setIntegrityCheck(false)
->from(array('post' => 'Posts'), array('*'))
->where('post.idGroup = ' . $idGroup)
->where('post.idTopic IS NULL')
->order('post.date DESC')
->limit($resultsPerPage, $resultsPerPage * ($page - 1))
->joinLeft(array('user' => 'Users'), 'post.idUser = user.idUser',
array('idUser', 'fname', 'lname', 'profileUrl', 'photoUrl'))
->joinLeft(array('comment' => 'Posts'), 'comment.idTopic = post.idPost')
->query()->fetchAll();
The problem is that the resulting array is flat and the comment data overwrites the post data, this is an example of what is returned:
[1] => Array
(
[idPost] => 13
[idTopic] => 11
[idGroup] => 1
[idUser] => 84
[postContent] => Hello my name is Mud.
[postUrl] => 13/hello-my-name-is-mud
[postVotes] =>
[postScore] =>
[date] => 2009-07-21 16:39:09
[fname] => John
[lname] => Doe
[profileUrl] => john-doe
[photoUrl] => uploads/userprofiles/0/84/pic84_14
)
What we would like the result to be is something more like this:
[1] => array(
[post] => array(
[0] => array(
idPost => 12,
postContent => This is a post...,
idGroup => 1
...
)
),
[user] => array(
[0] => array(
userName => JohnDoe
...
)
),
[comments] => array(
[0] => array(
idPost => 15,
postContent => This is a comment...,
idGroup => 1
...
),
[1] => array(
idPost => 17,
postContent => This is another comment...,
idGroup => 1
...
)
)
)
Any hints to other solutions is also very welcome.
Thanks.

If you alias all the columns in the second join to posts (like idPost as child_idPost... etc), you'll get many rows that are the parent row with the columns of the second row. Thats about the closest you'll get. You can then grab the parent data from the first row, and then loop through the subsequent rows to get your one-to-many data.
Otherwise, just do two queries, one for the parent, one for the children. It may be faster than creating that large result table anyway.

Zend does not make your preferred form easy, but it might be possible. Note, however, that you are asking the database server to do far more work than you actually want, because the post and user information are duplicated for each comment. Justin is correct that a second query is easier and probably faster. However, I can provide some pointers toward a solution.
To start, consider what you would get by using the Zend_Db::FETCH_NUM fetch mode:
Array(
[0] => Array(
[0] => 12
[1] =>
[2] => 1
[3] => 84
[4] => This is a post...,
[5] => 12/this-is-a-post
[6] =>
[7] =>
[8] => 2009-07-21 16:39:09
[9] => 84
[10] => John
[11] => Doe
[12] => john-doe
[13] => uploads/userprofiles/0/84/pic84_14
[14] => 15
[15] => 12
[16] => 1
[17] => 79
[18] => This is a comment...,
[19] =>
[20] =>
[21] =>
[22] => 2009-07-21 17:40:10
),
[1] => Array(
[0] => 12
[1] =>
[2] => 1
[3] => 84
[4] => This is a post...,
[5] => 12/this-is-a-post
[6] =>
[7] =>
[8] => 2009-07-21 16:39:09
[9] => 84
[10] => John
[11] => Doe
[12] => john-doe
[13] => uploads/userprofiles/0/84/pic84_14
[14] => 17
[15] => 12
[16] => 1
[17] => 127
[18] => This is another comment...,
[19] =>
[20] =>
[21] =>
[22] => 2009-07-20 10:31:26
)
)
Then somehow you have to come up with the mapping of column numbers to table and column names:
Array(
[0] => post.idPost
[1] => post.idTopic
[2] => post.idGroup
[3] => post.idUser
[4] => post.postContent
[5] => post.postUrl
[6] => post.postVotes
[7] => post.postScore
[8] => post.date
[9] => user.idUser
[10] => user.fname
[11] => user.lname
[12] => user.profileUrl
[13] => user.photoUrl
[14] => comment.idPost
[15] => comment.idTopic
[16] => comment.idGroup
[17] => comment.idUser
[18] => comment.postContent
[19] => comment.postUrl
[20] => comment.postVotes
[21] => comment.postScore
[22] => comment.date
)
This is the part where it gets tricky, because the table part of that is strongly specific to the database interface, and not always possible. Because it is tied to the result set, the adapter will also be the best place to obtain it; that means hacking Zend classes, possibly by providing your own adapter class. Depending on your database, the information might come from:
PDO: PDOStatement->getColumnMeta
Mysqli: mysqli_fetch_field_direct
Other adapters might not have a way to obtain the table name, unfortunately.

Yeah we've had the same issue, just finished blogging about it: http://www.kintek.com.au/blog/zend-db-and-joins/
You need to name each field uniquely, it's a known bug in Zend: http://framework.zend.com/issues/browse/ZF-6421?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

Related

Shopify - started getting 404 exceptions when updating variants

i've had my first Shopify store running for about a month now, using a PHP batch script to either create new products and variants, or to update as and when needed from an existing web-based product management system. All seems to have been running fine but i've just noticed that i'm getting an exception thrown when updating variants. I'm sure this has been working fine previously (been able to update a variant as needed) so I could do with a bit of help figuring out why this is failing now.
Here's an example:
ShopifyApiException Object
(
[method:protected] => PUT
[path:protected] => /admin/variants/4294967295.json
[params:protected] => Array
(
[variant] => Array
(
[price] => 8.00
[sku] => 2217
[id] => 2217
[product_id] => 21880
[inventory_management] => shopify
[inventory_policy] => deny
[inventory_quantity] => 1
[option1] => Plain \/ EX
[option2] => Strictly Rhythm
[option3] => SR12216
[weight] => 330
[weight_unit] => g
)
)
[response_headers:protected] => Array
(
[http_status_message] => Not Found
[http_status_code] => 404
[server] => nginx
[date] => Fri, 21 Aug 2015 14:27:03 GMT
[content-type] => application/json; charset=utf-8
[transfer-encoding] => chunked
[connection] => keep-alive
[vary] => Accept-Encoding
[x-sorting-hat-podid] => 3
[x-sorting-hat-shopid] => 2234339
[x-sorting-hat-podid-cached] => 1
[x-sorting-hat-shopid-cached] => 1
[status] => 404 Not Found
[x-frame-options] => DENY
[x-shopid] => 2234339
[x-shardid] => 3
[x-shopify-shop-api-call-limit] => 1/40
[http_x_shopify_shop_api_call_limit] => 1/40
[x-stats-userid] => 0
[x-stats-apiclientid] => 152673
[x-stats-apipermissionid] => 3265975
[x-xss-protection] => 1; mode=block; report=/xss-report/97471013-3a3c-4b82-a94d-868f9b7ac073?source%5Baction%5D=update&source%5Bcontroller%5D=admin%2Fproduct_variants&source%5Bsection%5D=admin
[set-cookie] => request_method=PUT; path=/
[x-request-id] => 97471013-3a3c-4b82-a94d-868f9b7ac073
)
[response:protected] => Array
(
[errors] => Not Found
)
[message:protected] => Not Found
[string:Exception:private] =>
[code:protected] => 404
[file:protected] => /var/www/MY-SERVER/httpdocs/shopify_tools/shopify.php
[line:protected] => 70
[trace:Exception:private] => Array
(
[0] => Array
(
[file] => /var/www/MY-SERVER/httpdocs/shopify_tools/batch_upload_items.php
[line] => 347
[function] => call
[class] => ShopifyClient
[type] => ->
[args] => Array
(
[0] => PUT
[1] => /admin/variants/4294967295.json
[2] => Array
(
[variant] => Array
(
[price] => 8.00
[sku] => 2217
[id] => 2217
[product_id] => 21880
[inventory_management] => shopify
[inventory_policy] => deny
[inventory_quantity] => 1
[option1] => Plain \/ EX
[option2] => Strictly Rhythm
[option3] => SR12216
[weight] => 330
[weight_unit] => g
)
)
)
)
)
[previous:Exception:private] =>
)
I've realised the problem - hopefully this may help someone else.
I was storing the Shopify Variant ID in an INT column in my database and Shopify Variant IDs have just recently passed the INT limit of 4294967295 - switching to BIGINT and reimporting all the Variant IDs solved the problem.

How to get store link with Bigcommerce API

How on installation step I can get store url? Because store hash is not the thing I really need I guess, as then I can't access is somehow by link.
if you are using the bigcommerce api, you can get the store information: https://developer.bigcommerce.com/api/stores/v2/store_information according the documentation
the api response give you:
stdClass Object
(
[id] => [your-store-identifier]
[domain] => [youstore-domain-name].mybigcommerce.com
[name] => test
[address] =>
[phone] =>
[admin_email] => test#domain.com
[order_email] => test#domain.com
[language] => en
[currency] => USD
[currency_symbol] => $
[decimal_separator] => .
[thousands_separator] => ,
[decimal_places] => 2
[currency_symbol_location] => left
[weight_units] => LBS
[dimension_units] => Inches
[dimension_decimal_places] => 2
[dimension_decimal_token] => .
[dimension_thousands_token] => ,
[plan_name] => Professional Application Developer Store
[logo] => Array
(
)
[is_price_entered_with_tax] =>
[active_comparison_modules] => Array
(
)
)

eBay API OutOfStockControlPreference with quantity 0 returns 'Quantity is not valid'

Following the instructions for OutOfStockControlPreference, I've set it to true, and even verified using ShowOutOfStockControlPreference.
Next, my call is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<ReviseInventoryStatusRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>-- removed --</eBayAuthToken>
</RequesterCredentials>
<InventoryStatus>
<ItemID>**********</ItemID>
<Quantity>0</Quantity>
</InventoryStatus>
</ReviseInventoryStatusRequest>
eBay returns a 515 error:
Array(
[ReviseInventoryStatusResponse] => Array(
[Timestamp] => 2014-10-28T16:05:19.176Z
[Ack] => Failure
[Errors] => Array(
[ShortMessage] => Quantity is not valid.
[LongMessage] => The quantity must be a valid number greater than 0.
[ErrorCode] => 515
[SeverityCode] => Error
[ErrorParameters] => Array(
[0] => Array(
[Value] => **********
)
[1] => Array(
[Value] => Array(
)
)
)
[ErrorClassification] => RequestError
)
[Version] => 891
[Build] => E891_UNI_API5_17051033_R1
)
)
This is unsupported in the eBay sandbox, but it works for eBay live.

how to prevent to steal my video using amazon cloudfront

i have created distribution id.It has following property
Array (
[id] => E3VYXDXV1O9WXQ
[status] => Deployed
[time] => 1365430752
[domain] => d1fhielx2etku.cloudfront.net
[enabled] => 1
[origin] => avijit_bucket.s3.amazonaws.com
[originAccessIdentity] =>
[defaultRootObject] =>
[cnames] => Array
(
[mycustomvideo1.com] => mycustomvideo1.com
)
[trustedSigners] => Array
(
)
[comment] => New distribution created )
What will be next step.How do i access video

Create Recurring profiled failed in sandbox

I have working on SANDBOX. I am getting error :
[0] => Array
(
[L_ERRORCODE] => 11502
[L_SHORTMESSAGE] => Invalid Token
[L_LONGMESSAGE] => The token is invalid
[L_SEVERITYCODE] => Error
)
This is what i passing in CreateRecurringProfile :
[REQUESTDATA] => Array
(
[USER] => "API_USER"
[PWD] => "API_PASS"
[VERSION] => 74.0
[BUTTONSOURCE] => AngellEYE_PHPClass
[SIGNATURE] => "API_SING"
[METHOD] => CreateRecurringPaymentsProfile
[TOKEN] => EC-8F723718H3002730R
[SUBSCRIBERNAME] => Tester Testerson
[PROFILESTARTDATE] => 2012-09-06T00:00:00\Z
[DESC] => Item 1 Name
[AUTOBILLAMT] => 1
[BILLINGPERIOD] => Month
[BILLINGFREQUENCY] => 1
[TOTALBILLINGCYCLES] => 0
[AMT] => 10.00
[CURRENCYCODE] => USD
[EMAIL] => hide#address.com
[BUSINESS] => Testers, LLC
[FIRSTNAME] => Tester
[LASTNAME] => Testerson
)
NOTE: I have removed SANDBOX API Credential. To show here.
Please give me solution what is missing here or what is fault in my posting request.
Thanks for advance
Did you include L_BILLINGTYPE0=RecurringPayments in your SetExpressCheckout and DoExpressCheckoutPayment API calls?