Update number form field - doctrine-orm

I have a field named 'starting_price' in my table where I store money. I store money as an integer in cents and when i store it I multiply it with 100 so it can be stored in cents. For example, 10$ is stored as 1000 in database but when rendering it I divide it with 100 and also use number_format in twig. Now I want to add update functionality and I want to divide it with 100 before rendering it but it doesn't work. I have this in my form:
->add('starting_price', NumberType::class,[
'label' => 'Starting price',
])
and this in my twig:
{{ form_row(form.starting_price) }}
When I add /100 in my twig file it returns an error
An exception has been thrown during the rendering of a template ("Warning: A non-numeric value encountered").
This shows 12000 cents and I want it to show 120

You can use a Form Data Transformer: Doc here
$builder
->get(
'starting_price',
NumberType::class,
[
'label' => 'Starting price',
]
)
->addModelTransformer(new CallbackTransformer(
function ($data) {
return $data / 100;
},
function ($data) {
return $data * 100;
}
)
);

Related

DynamoDB query performance is low

First of all some specs:
number of entries: 110k
total size of table: 700MB
number of columns per data set: can be up to 450
data set size: can be up to 25kB
read/write capacity: "on demand"
Problem: when trying to query for some rows by a column it takes easily up to 10 seconds or more.
The column we query by is an UUID column (index exists), not unique, used kind of like an external ID. So we say give me all records with that UUID and we expect up to ca. 1000 rows.
Even if I remove our application completely out of the equation (testing directly in the AWS management console) it makes no difference, still very poor performance (means also about 10 seconds or more).
So my question: do you have any ideas or concrete tips that I should check/test/adjust to improve the performance?
After request, here's the example code in PHP (reduced to relevant parts):
We use the official aws/aws-sdk-php package.
do {
// Marshal a native PHP array of data to a DynamoDB item.
$transformedValue = $this->getDynamoArrayFromNativeArray(
[':uuid' => $uuid]
);
$params = [
'TableName' => 'our-table-name',
'IndexName' => 'our-uuid-index',
'KeyConditionExpression' => 'our-uuid-column = :uuid',
'ExpressionAttributeValues' => $transformedValue,
];
if ($queryResult !== null) {
$lastEvaluatedKey = $queryResult['LastEvaluatedKey'];
$params['ExclusiveStartKey'] = $lastEvaluatedKey;
}
$queryResult = $this->client->query($params);
// (push results to some array)
} while ($queryResult['LastEvaluatedKey'] !== null);
Example data set:
{
"_id": "82ee23ce-d7ff-11eb-bf92-0aa84964df0a",
"_meta": {
"creation_date": 1624877797,
"uuid": "820025c0-d7ff-11eb-a5f4-0aa84964df0a"
},
"some_key.data.id": 63680,
(couple of hundred more simple key => value pairs, nothing special, no huge values or anything like that)
Read capacity chart for the index in question:
Query latency chart:

How open modal pages conditionally?

I am using Oracle APEX 21.1. I have a table visits with a column visit_type. visit_type has only values 1 or 2. There are 2 dialog pages(1 and 2). I need to create a query that returns a link that opens page 1 when visit_type = 1 and opens page 2 when visit_type = 2. Should I select a string with <a href=""</a> tag or use APEX_PAGE.GET_URL API. Either way, kindly, give me an example.
Here is a typical format
apex_string.format(
'<a class="t-Button t-Button--hot t-Button--simple t-Button--stretch" href="%s">%s</a>'
, apex_page.get_url
(p_application => 'YOUR_APP'
,p_page => t.visit_type
,p_items => 'p'||t.visit_type||'_id'
,p_values => t.visit_id
)
, 'Link text'
) as link_target
Don't forget to not escape special characters in your column definition.

How can i give limit in django?

I'm making a project and want to use limit for fetch data how can i use limit if there are any function or any way i can give limit to my fetching data
i expect the output of (2019, 12, 27)(2019, 6, 30) to be (2019, 12, 27) but it fetching all records
def maintenancefunction(request): #maintenance page function
if 'user' not in request.session:
return redirect('/login')
else:
if request.session.has_key('user'):
abc=request.session['user']
today = date(2019,1,1) # today= date.today.().strftime('%d/%m/%Y')
next_date=today.strftime('%Y-%m-%d')
lastdate= today + timedelta(days=180)
new_date= lastdate.strftime('%Y-%m-%d')
duedate=maintanance_table.objects.values_list('maintanance_todate').filter(user_email=abc).order_by('maintanance_todate').reverse()
# # newduedate=duedate.strftime('%Y-%m-%d')
print("DueDate:",duedate)
checkstatus=maintanance_table.objects.filter(user_email=abc).filter(maintanance_status="PAID").order_by('maintanance_todate').reverse()
if checkstatus:
lastdate = lastdate + timedelta(days=180)
new_date = lastdate.strftime('%Y-%m-%d')
else:
lastdate=lastdate
new_date= lastdate.strftime('%Y-%m-%d')
return render(request,"maintenance.html", {'abc':abc,'new_date':new_date})
else:
return render(request,"login.html")
return render(request,"maintenance.html")
You can add range at the end of the query like [1-10] and if you want the first record then just put [0] at the end of the query. If you want specific record then put its number at the end of the query like [5] or [3] etc.
duedate=maintanance_table.objects.values_list('maintanance_todate').filter(user_email=abc).order_by('maintanance_todate').reverse()[1-10]
checkstatus=maintanance_table.objects.filter(user_email=abc).filter(maintanance_status="PAID").order_by('maintanance_todate').reverse()[1-10]

In Rails, how do I display a string value from matching database value with id in HelperClass?

I have states stored in my database with numeric values... as in:
["Alabama", "1"],
["Alaska", "2"],
["Arizona", "3"],
["Arkansas", "4"],
ETC....
When I want to display "Alaska" because the number 2 is being output, how do I do this?
I tried something like this in my controller:
def show
#contact = Contact.find(params[:id])
#state = contact.location_state
end
And have the following in my "show.html.erb":
<%= #state %>
In the case you have to deal with an array of arrays you can get the specific number in the second position of every array accessing to it, with the index [1]:
places = [
["Alabama", "1"],
["Alaska", "2"],
["Arizona", "3"],
["Arkansas", "4"]
]
p places[0][1]
# => "1"
If you have a single one, you don't need to specify the precise array inside the main array of arrays, just with:
place = ["Alabama", "1"]
p place[1]
# => "1"
If you're sending the #state object and want to work with it in your views you can iterate and show the second value corresponding to the number you want to show:
places.each{|place| p place[1]}
# => "1"
# => "2"
# => "3"
# => "4"

Opencart list categories inside manufacturers

I have searched for hours but could not find an answer to this, or a module to help.
We are building a store and our client needs the ability to navigate the store by manufacturer. Is there any way that the manufacturer page can list the categories and subcategories.
There seems two ways to do it.
Add brands while adding categories in admin section.
Get all categories inside the brands by join operation while viewing the manufacturer.
Are there any modules available to link up categories with manufacturers so that I can display categories inside the manufacturer page.
Or the only way is to query all the products inside the manufacturer and get the categories out of it... I guess it is not a good solution.
So any suggestions would be a great help.
Thanks.
I figured a way to find the categories that belongs to a manufacturer. The second options seems better.
Here is the function that I added to catalog/model/catalog/manufacturer.php
public function getManufacturerCategories($manufacturer_id) {
$query = $this->db->query("
SELECT
DISTINCT c.category_id,cd.name
FROM
". DB_PREFIX . "manufacturer m
LEFT JOIN ". DB_PREFIX. "product p ON (m.manufacturer_id = p.manufacturer_id)
LEFT JOIN ". DB_PREFIX. "product_to_category p2c ON (p2c.product_id = p.product_id)
LEFT JOIN ". DB_PREFIX. "category c ON (c.category_id = p2c.category_id)
LEFT JOIN ". DB_PREFIX. "category_description cd ON (cd.category_id = p2c.category_id)
WHERE
p.status = 1
AND m.manufacturer_id = '".(int)$manufacturer_id."'
AND c.status= 1
");
return $query->rows;
}
Here is the output array
stdClass Object (
[row] => Array
(
[category_id] => 20
[name] => Desktops
)
[rows] => Array
(
[0] => Array
(
[category_id] => 20
[name] => Desktops
)
[1] => Array
(
[category_id] => 24
[name] => Phones & PDAs
)
)
[num_rows] => 2 )