What to call a method that finds or creates records in db - methods

This question might seem stupid but i nonetheless i believe it's a worth asking questioin.
I work on some web application when we use tags which are attached to articles.
When adding new article users provides a list of tags he wish to associate with his new article.
So when the form is submitted and my MVC controller processes request i have a string with tags from the form. I split the tags string and i get an array of tag words. Then i need a
list o tags ids from db. So i came up with a method that takes a list of tag words and checks if each tag already exists in db. If given tag is already in db then tag's id is appended to result array. If tag does not exist in db it is created and then id of just created tag is appended to result array.
My question is: what is the best name for such a method? This method creates tags only if necessary and returns list of tags' ids.
I tried this names, but none of them looks right to me:
fetchTagsIds(List tagWordsList)
createOrFindsTagsIds(List tagWordsList)
obtainTagsIds(List tagWordsList)
I need a name that really reflects what the method does. Thanks for your help :)

I'd drop the "s" in "Tags". So that the method is:
FetchTagIds(List tagWordsList)

IdsOfTags. That it creates them is an implementation detail (as is the fact that it uses a relational database in the first place). All that matters is that it gives you unique ids for tags (e.g. it could also hash them with a perfect hash function, or lookup an ID in the web.

If you're having trouble coming up with a name that accurately describes what your method does, that could be because you method does too much. Perhaps it would be better to refactor it into two:
findTagsByName(List tagNames) would return a list of Tag objects based on list of names you pass in
persistTags(List tags) - or saveTags(), or createTags() - would persist a list of Tag objects.

getTagIDs?
Get suggests that the code will do something to get an ID when one doesn't exist to fetch.

Related

Laravel - Model binding not working when I use accessors

I want to populate my select box from the Laravel IoC Container.
App\Http\Utilities\BillingHelper.php
views/billing/create.blade.php
views/billing/edit.blade.php
Create the table:
Now, instead of the value, i want to display some flags and currency symbols.
Should i use mutators?
Problem
If i use mutators, when i open the edit page, i see always the first value selected, from the BillingHelper, instead of the choosen one.
Any help? Thanks.
I know it should be a comment, but I have no reputation.
What if, on you edit page, you replace null on Form::select with $client->language and $client->currency.
I know that you area binding the values with Form::Model. But worth a try.
When you use mutators the matching won't occur anymore. You'll have to use a matching static array according to the values you'll return. (the flags)
You can make it work if you make a mutator for saving the data also and simplify again to the ['it', 'en', 'lv'], otherwise your saved data will differ and the initial mutator won't work the second time. You can still make a one-time-only test.
This is why:
Your form binding is using $bill->language to retrieve the actual stored data, and compare it with the values in your $bill::lang static array. If no match found, than the first value will be always selected.
Can you provide the the currency and language fields definition in the migration for the bill?
Also retrive your values from your bills DB and paste them here for language and currency. They must be in the defined static sets.
Laravel has a way of skipping the accessor/mutator by using
$model->getOriginal('data_field').
The "getOriginal()" gets the model's original attribute values.

How to load the jqgrid in a selector with context

In general we call the jqgrid as in$("#grid_loc").jqGrid({});
But i want to specify the context like $("#grid_loc",context).jqGrid({}). But this is not working. Can somebody help in this?
I have to load server side data using url option.
Infact i occured to have this, as i have tabs on my page.
In each tab, i have to have a jqgrid, not different grids but same grid with different data .
Here i am getting the tab context using var tabset = $("div.tabset");
newdivid = $("div[class*='active_tab']",tabset).attr("id");
var newmenudivid = $("#"+newdivid);
And
the grid code as
$("#grid_workflow", newmenudivid).jqGrid({....});
I have been trying to find out a way to do this. you can find some of my effort in the comments section of the link
how to develop same jqgrid in multiple tabs
i was successful with id overwriting for the same purpose. But that is not a good way though. So i am forced to have another approach ie. context
I suppose that you misunderstand some important things which corresponds to id attribute. The most important that all elements on the page having id attribute have to have unique value of the attribute. In other words the ids have to be unique over the whole HTML page.
So if you need create for example tree grids inside of tree tabs you have to define different id attributes for every grid. For example; grid_workflow1, grid_workflow2, grid_workflow3. If you create the tabs and grids dynamically then you can have some variable in the outer scope (for example global variable) and increase the value of the variable. You can construct id of the grid using some prefix (like "grid_workflow") and the value of the variable. In the way you can create multiple grids with unique ids. Many JavaScript libraries uses the way to generate unique id attribute. Ij you want you can use $.jgrid.randId() method which will returns you unique strings which can be used as ids.
Because of the syntax $("#grid_workflow", newmenudivid) you should understand one important thing. I would recommend never use it. The reason is very easy. It could help only if you have id duplicates. In all other cases if will works exactly like $("#grid_workflow") but slowly. The reason is easy to understand. Web browser hold internally the list if all ids on the page and if you use getElementById method directly of indirectly (in $("#grid_workflow")) the searching of the element with the required id will be like searching in the index in the database. So you will have best performance results. If you use $("#grid_workflow", newmenudivid) then you don't allow web browser to use the index of elements by id. So the usage of context will follow to slow searching throw all children elements of newmenudivid. So you should avoid usage of jQuery context with id selectors.

Kendo UI Datasource and Arrays

I am sending over a series of array values from a posted form to an MVC3 Controller. I was hoping the default modelbinder would be able to parse this but I'm having some difficulty with it.
The array is in the following format:
order[0].[type]=some value.
I think this is the reason the model binder is not parsing my values because I'm not getting anything populated in my model.
What would be another way to handle this?
Probably need to post more of your code so I can see what you are doing exactly. However saying this you need to pass the model to the view/partial view on the response you are trying to retrieve on the post request.
If not you will have to iterate through the Form Collection that will be returned and the Actions Methods type e.g. ActionMethodName(FormCollection form), one issue is name versus id its the name of the Kendo UI control that is used to get the value not the id.
1As far as I remember the right format was:
orders[0].OrderID=13;
orders[0].Name="test";
orders[1].OrderID=15;
orders[1].Name="again test";
The indexing should start from 0 and increase by 1.
Check this out: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

How to use helper inside model function in CakePHP

Now before you burn me at the stake hear me out!
I want some keywords of a product description field to link to other products (kinda like mediawiki links), however at some point I need to make these associations and link the keywords up, so I'll need to do a search on each curly-braced word I find in the description and produce a formatted version of the description to cut down on processing these keyword links every time the description is displayed.
For ease/consistency I am creating all product links with a custom helper, and all I need to do is pass the product row in and the helper products a link for me with any options I specify. The only this is, is that I need to now do this in beforeSave() so I can populate description_formatted.
At the minute, beforeSave() checks for the original description row, then calls a private method in the model which matches each keyword, queries the db for a matching row... that is as far as I've got.
Just like any other MVC, Cake makes big restrictions how to couple your classes. This is needed to keep script kiddies to shoot themselves in the foot. However, there is a niche workaround for cake if you really need to get along: http://book.cakephp.org/view/933/The-App-Class
How I would do this? With the helper I would replace all curly braces words into links and when the user hovers the linked word I would call an Ajax which will get the word description or the link or whatever you need to do. This way you request the description only when it's needed.
If you still insist to use the helper - it's just a class in PHP, so you can include it in your Model, create an object of the class and use it's functions.
The third option is to create your own class and use it both in Model and the Helper.

GET vs POST in AJAX?

Why are there GET and POST requests in AJAX as it does not affect page URL anyway? What difference does it make by passing sensitive data over GET in AJAX as the data is not getting reflected to page URL?
You should use the proper HTTP verb according to what you require from your web service.
When dealing with a Collection URI like: http://example.com/resources/
GET: List the members of the collection, complete with their member URIs for further navigation. For example, list all the cars for sale.
PUT: Meaning defined as "replace the entire collection with another collection".
POST: Create a new entry in the collection where the ID is assigned automatically by the collection. The ID created is usually included as part of the data returned by this operation.
DELETE: Meaning defined as "delete the entire collection".
When dealing with a Member URI like: http://example.com/resources/7HOU57Y
GET: Retrieve a representation of the addressed member of the collection expressed in an appropriate MIME type.
PUT: Update the addressed member of the collection or create it with the specified ID.
POST: Treats the addressed member as a collection in its own right and creates a new subordinate of it.
DELETE: Delete the addressed member of the collection.
Source: Wikipedia
Well, as for GET, you still have the url length limitation. Other than that, it is quite conceivable that the server treats POST and GET requests differently; thus the need to be able to specify what request you're doing.
Another difference between GET and POST is the way caching is handled in browsers. POST response is never cached. GET may or may not be cached based on the caching rules specified in your response headers.
Two primary reasons for having them:
GET requests have some pretty restrictive limitations on size; POST are typically capable of containing much more information.
The backend may be expecting GET or POST, depending on how it's designed. We need the flexibility of doing a GET if the backend expects one, or a POST if that's what it's expecting.
It's simply down to respecting the rules of the http protocol.
Get - calls must be idempotent. This means that if you call it multiple times you will get the same result. It is not intended to change the underlying data. You might use this for a search box etc.
Post - calls are NOT idempotent. It is allowed to make a change to the underlying data, so might be used in a create method. If you call it multiple times you will create multiple entries.
You normally send parameters to the AJAX script, it returns data based on these parameters. It works just like a form that has method="get" or method="post". When using the GET method, the parameters are passed in the query string. When using POST method, the parameters are sent in the post body.
Generally, if your parameters have very few characters and do not contain sensitive information then you send them via GET method. Sensitive data (e.g. password) or long text (e.g. an 8000 character long bio of a person) are better sent via POST method.
Thanks..
I mainly use the GET method with Ajax and I haven't got any problems until now except the following:
Internet Explorer (unlike Firefox and Google Chrome) cache GET calling if using the same GET values.
So, using some interval with Ajax GET can show the same results unless you change URL with irrelevant random number usage for each Ajax GET.
Others have covered the main points (context/idempotency, and size), but i'll add another: encryption. If you are using SSL and want to encrypt your input args, you need to use POST.
When we use the GET method in Ajax, only the content of the value of the field is sent, not the format in which the content is. For example, content in the text area is just added in the URL in case of the GET method (without a new line character). That is not the case in the POST method.

Resources