paginate an union in Cakephp 3 - cakephp

I'm trying to paginate a union in cakephp 3. I have already found and read How do you modify a UNION query in CakePHP 3?, but am probably too new to cakephp to understand properly what is meant by "use a custom finder". I have read the relevant section in the cakephp book, but still fail to see how this helps to apply the epilog() to the query. Maybe someone could show a complete example?
I have two models Customers and Suppliers. I am trying to builds a search function that returns matching entries from both models, including pagination and sorting.
public function index($q=null) {
$this->loadmodel('Suppliers');
$this->loadmodel('Customers');
$customers=$this->Customers->find('all')->select(['type'=>"'Customers'",'id'=>'id','name'=>'name','phone'=>'phone'])
->where(['name like'=>'%'.$q.'%']);
$suppliers=$this->Suppliers->find('all')->select(['type'=>"'Suppliers'",'id'=>'id','name'=>'name','phone'=>'phone'])
->where(['name like'=>'%'.$q.'%']);
$customers->union($suppliers);
$results=$this->paginate($customers);
$this->set(compact(['q','results']));
}
As detailed in How do you modify a UNION query in CakePHP 3?, this results in the order,limit and offset only being applied to the first select.
Removing the paginate() and applying order,limit and offset manually via epilog() works, but than I'd have to recreate the whole pagination mechanism in my code. While this is my current plan in case I can't find a better solution, I don't believe this would be the right way to do it.
Could someone (#PhantomWatson, #ndm) show an example, how the suggested custom finder can solve this?

Related

Implementing guided navigation using vespa

I'm trying to implement guided navigation using vespa. At present, I am using grouping to implement it. I've written a query like this.
SELECT * FROM entity where sddocname contains 'entity' | all(group(category) each(group(item_category) each(output(count()))));
In the above example category is like "electronics","automobiles" etc and "item_category" is like "mobile phone","car"etc.
I don't want to select any documents, I just wanted to see the grouped data. Is there a better way to solve this problem?
Use limit 0 to remove the plain hit list. Example here https://docs.vespa.ai/documentation/tutorials/blog-search.html
limit 0 in yql or just specify &hits=0

SuiteCommerce Advanced - Show a custom record on the PDP

I am looking to create a feature whereby a User can download any available documents related to the item from a tab on the PDP.
So far I have created a custom record called Documentation (customrecord_documentation) containing the following fields:
Related item : custrecord_documentation_related_item
Type : custrecord_documentation_type
Document : custrecord_documentation_document
Description : custrecord_documentation_description
Related Item ID : custrecord_documentation_related_item_id
The functionality works fine on the backend of NetSuite where I can assign documents to an Inventory item. The stumbling block is trying to fetch the data to the front end of the SCA webstore.
Any help on the above would be much appreciated.
I've come at this a number of ways.
One way is to create a Suitelet that returns JSON of the document names and urls. The urls can be the real Netsuite urls or they can be the urls of your suitelet where you set up the suitelet to return the doc when accessed with action=doc&id=_docid_ query params.
Add a target <div id="relatedDocs"></div> to the item_details.tpl
In your ItemDetailsView's init_Plugins add
$.getJSON('app/site/hosting/scriptlet.nl...?action=availabledoc').
then(function(data){
var asHtml = format(data); //however you like
$("#relatedDocs").html(asHtml);
});
You can also go the whole module route. If you created a third party module DocsView then you would add DocsView as a child view to ItemDetailsView.
That's a little more involved so try the option above first to see if it fits your needs. The nice thing is you can just about ignore Backbone with this approach. You can make this a little more portable by using a service.ss instead of the suitelet. You can create your own ssp app for the function so you don't have to deal with SCAs url structure.
It's been a while, but you should be able to access the JSON data from within the related Backbone View class. From there, within the return context, output the value you're wanting to the PDP. Hopefully you're extending the original class and not overwriting / altering the core code :P.
The model associated with the PDP should hold all the JSON data you're looking for. Model.get('...') sort of syntax.
I'd recommend against Suitelets for this, as that's extra execution time, and is a bit slower.
I'm sure you know, but you need to set the documents to be available as public as well.
Hope this helps, thanks.

How to force two instance of the same app (DNN/2sxc) to read from the same stream?

Sorry if my question is silly but I'm new to DNN/2sxc, I've spent the whole day trying to figure this with no success..
I have two instances of the same app, one in the home page and the other on its own page, each one must have its own view template (I use Razor).
My problem is I cannot figure a way to make the two apps read the same data, so every add/edit/remove/re-sort in one of them will be reflected to the other, currently each app has its own data and therefore they are unusable in my case.
I've tried to use a 'EntityTypeFilter' inside a 'Data Query' and use it in both views (as in the News-Simple demo video), it worked and gave me all the items in the two views, but another two problems come with this solution:
1- now I'm unable to use the toolbar to (add/remove/reorder,.. etc) any of the items , as you can see in this image, which is a show-stopper for me,
note: this is the toolbar I use:
#foreach(var item in AsDynamic(Data["Default"]))
{
...
#Edit.Toolbar(target: item, actions: "new,edit,replace,remove,moveup,movedown,instance-list")
2- the 'Content Demo Item' is also visible in the list, but it is not that important since I can delete it and use one of the real data items as a demo item.
I appreciate any kind of help.
Thank you.
So the first thing you should know is the difference when using content-items as data (to query, etc.) and when using it as assigned-items (where each module-instance has a subset of items). Here's the blog that should help you understand the difference: http://2sxc.org/en/blog/post/12-differences-when-templating-data-instead-of-content
So when you want the "manually and easily control the exact items displayed, their ordering etc." you want to use the "content-assigned-to-instance" which also gives you the simple add, delete buttons, as these don't really delete anything, but just remove the assignment from the module-instance.
Now your case is a bit special, in that you want to re-use the exact same set in another module-instance. There are a few ways you can do this:
Same View
if it's exactly the same view etc. just duplicate the module using DNN-features (the add-existing-module-to-another page)
different view
if it's a different view (maybe more compact, etc.) you again have multiple options. The first is to mirror / duplicate using the dnn-feature, and just put an if-im-on-this-page-then-show-differently or inject another CSS. That's probably the easiest without any dev-know-how.
The harder, but possibly nicer way, is to actually to use a new template, and tell it to retrieve the items in the way they are configured in the other module - let's say module 1 is the original, module 2 has a different template wanting to access the items of module 1 in exactly the same order as given in 1. They way to do this is simple, but requires a few lines of C# code in Module 2.
You need to create a new ModuleDataSource (https://2sxc.org/en/Docs/Feature/feature/4542) object and tell it that it's from Module 1. If you've never done this, it's basically that your code can create a query just like the visual designer, but you have more control - see the wiki https://github.com/2sic/2sxc/wiki/DotNet-DataSources-All. The Module-Data-Source in the visual-query-designer doesn't allow you to "switch" modules (a advanced setting we may add in the future) but the object has a ModuleId property which you can set before accessing the data, making it "switch" to that Module. here's the Pseudo code in your Module#2 razor...
var otherModData = CreateSource<ModuleDataSource>();
otherModData.ModuleId = 1;
foreach(var itm in AsDynamic(otherModData["Default"])) {
...
}
That should do it :)

Display results of unknown SOQL query

I have a requirement that I'd like to know if it is possible. I have visualforce page which will display the results of a SOQL query, however the SOQL query is dynamic and could be a query on Custom Objects or Standard Obejcts. I'm currently hard coding a SOQL query into the controller class whilst trying to figure out how it will work. I intend to display the results of the SOQL query in a pageBlockTable or dataTable in the Apex.
Is this possible in Salesforce, if so could anyone give me an example of how it would work both in Visualforce and Apex?
You should read about "dynamic references" (also called "dynamic bindings"). A good starting point: http://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf_sample_standard.htm
Basically if you have String fieldName = 'AccountNumber';, later in Visualforce you can refer to it directly:
<apex:outputField value="{!a.AccountNumber}"> or dynamically:
<apex:outputField value="{!a[fieldName]}">.
This is similar to having a.AccountNumber or a.get('AccountNumber') in Apex. Check out for example getting Value of a field by its Name in apex salesforce if you've never seen it.
You'll have to be careful around them because if your base object won't be account it will fail (for example there's no Contact.AccountNumber field). The example above deals with Accounts only but it'd be a good intro anyway.
Once you familiarize yourself with the basic idea you can explore world of fieldsets - predefined groups of fields that should come together - you can use them both for displaying and querying data, basically they're more powerful than hardcoded list of strings with the field names (like in first link) but still same idea.
At least you'll know now what keywords to look for ;)

Salesforce.com visualforce between 2 objects

In salesforce I need to create a visualforce page that includes the fields of 2 objects. The first object is the QUOTE object. The second objects is a custom object with several fields.
I want to create a visualforce page that shows the records of both QUOTE and the new object. Can I do this without creating a custom controller? If no any hints on the code for this new controller?
Can I do calculations between fields in a visualforce page?
Ideally I want this page to appear as soon as the QUOTE is set to ACCEPTED
1: You can't do this without a custom controller unfortunately, unless one object is related to the other and you're just happy displaying it as a related list on the parent object's page. For calculations you could use rollup summaries for some basic sums etc..
As for a custom controller, have a look at field sets for a super easy way to get fields into a VF page, you essentially configure groups of fields on your objects and then you can stick those groups onto a page with minimal markup.
2: For fields with complex calculations you'll want to do the sums in the controller and then expose the results through variables onto the page in the usual manner.
3: Not really possible without creating a custom edit page in the first place — you'd be better off having a button on the quote page to open up the Visualforce page, that page can simply display an error if the quote is not yet accepted. There are some other alternatives that might work though, like using a forumla field to generate a link to the page when the status is as you desire.
I'm happy to elaborate on any of this, but the fact that you're asking about number 2 would suggest to me that you don't have much experience developing on the platform (not a dig, just an observation), so unless you're comfortable coding in other environments you could find this quite tricky. That said, you're on stackoverflow so I'm thinking you probably know a little about coding at least!

Resources