Doctrine2 Sortable Update Position - jquery-ui

I am using Doctrine 2 with Symfony 2.3 and jQuery UI sortable.
I have a list of elements sortable with jQuery and the position is saved in the database through an Ajax request.
All seem to work perfectly except the data persistence... The position of the element and the other elements related to it in the database is wrong.
Example :
| ID | POSITION | TITLE
| 1 | 0 | Element 1
| 2 | 1 | Element 2
| 3 | 2 | Element 3
| 4 | 3 | Element 4
If I am moving ID 3 (position 3) to position 0, I get this result in the database:
| ID | POSITION | TITLE
| 1 | 2 | Element 1
| 2 | 2 | Element 2
| 3 | 0 | Element 3
| 4 | 4 | Element 4
I checked and the value inserted is right (0).
I am using this code to update the position:
$pyramid = $this->getDoctrine()->getRepository('MarquisWebsiteBundle:Pyramid')->find($id);
$pyramid->setPosition($position);
$em->persist($pyramid);
$em->flush();
It's working well if i am moving Element 1 from position 0 to position 1.
I am not using any SortableGroup on this table.
EDIT:
I am using StofDoctrineExtensionsBundle with the Gedmo DoctrineExtension and here is the configuration:
doctrine:
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
mappings:
gedmo_sortable:
type: annotation
prefix: Gedmo\Sortable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Sortable/Entity"
alias: GedmoSortable
is_bundle: false
gedmo_translatable:
type: annotation
prefix: Gedmo\Translatable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
alias: GedmoTranslatable
is_bundle: false
stof_doctrine_extensions:
default_locale: en_GB
translation_fallback: true
orm:
default:
sortable: true
translatable: true
And the entity (Pyramid.orm.yml):
Acme\DemoBundle\Entity\Pyramid:
type: entity
table: pyramid
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
position:
type: integer
unsigned: false
nullable: false
gedmo:
- sortablePosition
title:
type: string
length: 255
fixed: false
nullable: false
I don't think I need to change anything in the Pyramid.php class which looks like this:
/**
* Pyramid
*
* #ORM\Table(name="pyramid")
* #ORM\Entity
*/
class Pyramid
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="position", type="integer", nullable=false)
*/
private $position;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255, nullable=false)
*/
private $title;

I think you need to include the SortablePosition annotation:
/**
* #Gedmo\SortablePosition
* #ORM\Column(name="position", type="integer", nullable=false)
*/
private $position;

Related

What is the way to find the price with by the quantity in Rails?

Rails version: 7.0
PostgreSQL version: 14
What is the way to find the price by the quantity in the products table?
products table
min_quantity | max_quantity | price
1 | 4 | 200
5 | 9 | 185
10 | 24 | 175
25 | 34 | 150
35 | 999 | 100
1000 | null | 60
Expected result
3 ===> 200
50 ===> 100
2500 ===> 60
You can achieve that with a where condition checking that the given value is between min_quantity and max_quantity;
with products(min_quantity, max_quantity, price) as (
values
(1, 4, 200)
, (5, 9, 185)
, (10, 24, 175)
, (25, 34, 150)
, (35, 999, 100)
, (1000, null, 60)
)
select
price
from products
where
case
when max_quantity is null
then 3 >= min_quantity
else
3 between min_quantity and max_quantity
end
-- 200
But as you might have a null value for max_quantity when the min_quantity is 1000 then you'll need a way to handle that. So you can use a case expression to only compare the input with min_quantity.
If the same applies for min_quantity and it can hold null values, then another branch in the case expression might suffice.
As Rails doesn't have specific support for these situations, you'll be off to go with just "raw" SQL in your where;
where(<<~SQL, input: input)
where
case
when max_quantity is null
then :input > min_quantity
else
:input between min_quantity and max_quantity
end
SQL

How to write a nested object in Swagger 3.0 for components

So I am working on developing the components yaml file in the Data Transfer Object files so I can then reference them.
This is what I have so far:
/**
* #openapi
* components:
* schemas:
* VerifiedEmailAddressDto:
* type: object
* required:
* - email
* properties:
* _type:
* type: string
* email:
* type: string
* description: a users email.
* reset:
* type: boolean
* passwordRules:
* type: object
* properties:
* minLength:
* type: number
* maxLength:
* type: number
* minRequiredUppercase:
* type: number
* example:
* _type: VerifiedEmailAddressDto
* email: pablo+test_pab001#alunacare.com
* reset: false
* passwordRules:
*/
export class VerifiedEmailAddressDto {
readonly _type = "VerifiedEmailAddressDto";
readonly email: string;
readonly reset: boolean;
readonly passwordRules: { minLength: number; maxLength: number; minRequiredUppercase: number; minRequiredLowerCase: number; minRequiredSymbols: number };
constructor(email: string, reset: boolean, passwordRules: { minLength: number; maxLength: number; minRequiredUppercase: number; minRequiredLowerCase: number; minRequiredSymbols: number }) {
this.email = email;
this.reset = reset;
this.passwordRules = passwordRules;
}
}
That last property of passwordRules is an object inside this object. So it's a nested object, but what I have so far, does not give me this:
{
"_type": "VerifiedEmailAddressDto",
"email": "pablo+test_pab001#alunacare.com",
"reset": false,
"passwordRules": {
"minLength": 8,
"maxLength": 25,
"minRequiredUppercase": 1,
"minRequiredLowerCase": 1,
"minRequiredSymbols": 0
}
}
But honestly I am not sure how to complete this, I assume that this part:
* passwordRules:
* type: object
* properties:
* minLength:
* type: number
* maxLength:
* type: number
* minRequiredUppercase:
* type: number
is correct, but what to offer in the example is what I am stuck on and perhaps maybe even the above in this case might not be correct.
The idea is so I can eventually property reference it here:
/**
* #openapi
* /api/v2/auth/check_mail:
* post:
* tags: [Auth]
* description: This endpoint checks to see if an email is unique or is in use.
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* $ref: '#/components/schemas/VerifiedEmailAddressDto'
* responses:
* 201:
* description: Get permissions.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/VerifiedEmailAddressDto'
*/
this.app.post(p().api.v2.auth.check_email.$url, [
// TODO restrict number of queries by IP by period of time.
authMiddleware.validateEmailQuery,
credentialsController.verifyEmailAddress
]);
So I am able to get an empty object to appear for passwordRules like so:
/**
* #openapi
* components:
* schemas:
* VerifiedEmailAddressDto:
* type: object
* required:
* - email
* properties:
* _type:
* type: string
* email:
* type: string
* description: a users email.
* reset:
* type: boolean
* passwordRules:
* type: object
* properties:
* minLength:
* type: number
* maxLength:
* type: number
* minRequiredUppercase:
* type: number
* example:
* _type: VerifiedEmailAddressDto
* email: pablo+test_pab001#alunacare.com
* reset: false
* passwordRules: {}
*/
export class VerifiedEmailAddressDto {
readonly _type = "VerifiedEmailAddressDto";
readonly email: string;
readonly reset: boolean;
readonly passwordRules: { minLength: number; maxLength: number; minRequiredUppercase: number; minRequiredLowerCase: number; minRequiredSymbols: number };
constructor(email: string, reset: boolean, passwordRules: { minLength: number; maxLength: number; minRequiredUppercase: number; minRequiredLowerCase: number; minRequiredSymbols: number }) {
this.email = email;
this.reset = reset;
this.passwordRules = passwordRules;
}
}
but if I try to add its properties within the object like so:
passwordRules: {
minLength: 8
maxLength: 25
}
I get nothing, if I try to put the examples in like so:
* passwordRules:
* type: object
* properties:
* minLength:
* type: number
* example: 8
* maxLength:
* type: number
* example: 25
* minRequiredUppercase:
* type: number
* example: 1
* example:
* _type: VerifiedEmailAddressDto
* email: pablo+test_pab001#alunacare.com
* reset: false
* passwordRules: {}
I still get nothing.
In OpenAPI, an example nested object can be specified the same way as a root example object. E.g., as YAML key-value pairs.
So the following example spec:
...
* passwordRules:
* type: object
* properties:
* minLength:
* type: number
* maxLength:
* type: number
* minRequiredUppercase:
* type: number
* minRequiredLowerCase:
* type: number
* minRequiredSymbols:
* type: number
* example:
* _type: VerifiedEmailAddressDto
* email: pablo+test_pab001#alunacare.com
* reset: false
* passwordRules:
* minLength: 8
* maxLength: 25
* minRequiredUppercase: 1
* minRequiredLowerCase: 1
* minRequiredSymbols: 0
...
..looks like this in Swagger-UI:

Hive Sort Merge Bucket Map(SMB Map) Join

I ran the query with and without SMB join and got different results. Please help explain.
SET hive.enforce.bucketing=true;
create table dbaproceduresbuckets (
owner string ,
object_name string ,
procedure_name string ,
object_id double ,
subprogram_id double ,
overload string ,
object_type string ,
aggregate string ,
pipelined string ,
impltypeowner string ,
impltypename string ,
parallel string ,
interface string ,
deterministic string ,
authid string )
CLUSTERED BY (object_id) SORTED BY (OBJECT_ID ASC) INTO 32 BUCKETS;
CREATE TABLE dbaobjectsbuckets1(
owner string,
object_name string,
subobject_name string,
object_id double,
data_object_id double,
object_type string,
created string,
last_ddl_time string,
timestamp string,
status string,
temporary string,
generated string,
secondary string,
namespace double,
edition_name string) CLUSTERED BY (object_id) SORTED BY (OBJECT_ID ASC) INTO 32 BUCKETS;
**** load the table;
0: jdbc:hive2://xxxxxx:10000> select count(*) from dbaobjectsbuckets1 a, dbaproceduresbuckets b
0: jdbc:hive2://xxxxxxxx:10000> where a.object_id = b.object_id;
INFO : Hadoop job information for Stage-2: number of mappers: 3; number of reducers: 1
INFO : 2016-06-13 15:56:00,381 Stage-2 map = 0%, reduce = 0%
INFO : 2016-06-13 15:56:55,818 Stage-2 map = 1%, reduce = 0%, Cumulative CPU 122.6 sec
INFO : 2016-06-13 15:57:47,124 Stage-2 map = 7%, reduce = 0%, Cumulative CPU 326.86 sec
..........
INFO : 2016-06-13 16:05:01,246 Stage-2 map = 100%, reduce = 100%, Cumulative CPU 867.1 sec
INFO : MapReduce Total cumulative CPU time: 14 minutes 27 seconds 100 msec
INFO : Ended Job = job_1464280256859_0146
+--------+--+
| _c0 |
+--------+--+
| 54876 |
+--------+--+
****
set hive.auto.convert.sortmerge.join=true;
set hive.optimize.bucketmapjoin=true;
set hive.optimize.bucketmapjoin.sortedmerge=true;
set hive.auto.convert.sortmerge.join.noconditionaltask=true;
set hive.enforce.bucketing=true;
set hive.enforce.sorting=true;
0: jdbc:hive2://xxxxxxx:10000> select count(*) from dbaobjectsbuckets1 a, dbaproceduresbuckets b
0: jdbc:hive2://xxxxxxxx:10000> where a.object_id = b.object_id;
in the execution plan, I am seeing
| Sorted Merge Bucket Map Join Operator |
| condition map: |
| Inner Join 0 to 1 |
| keys: |
| 0 object_id (type: double) |
| 1 object_id (type: double)
**** but the result is showing
INFO : Hadoop job information for Stage-1: number of mappers: 32; number of reducers: 1
......
INFO : MapReduce Total cumulative CPU time: 4 minutes 8 seconds 490 msec
INFO : Ended Job = job_1464280256859_0150
+------+--+
| _c0 |
+------+--+
| 2 |
+------+--+
????? My question is why it only got 2 when I used SMB join?????? It is supposed to be 54876.
Thanks!
use sort by clause while inserting data into sorted table
or
set hive.enforce.sorting=true
before inserting data into sorted table

How to get sum of values in grid?

For example I have grid.
//grid for answers_for_online
var answersGridForOnline5 = new Ext.grid.GridPanel({
id : 'grid_for_stats',
store : storez3,
columns : answers_columns5,
});
my column:
var answers_columns5 = [{
id: "idz",
header: 'idz',
dataIndex: "idz",
renderer: fun_f
}];
and renderer function
function fun(n, j, k, m, h, i) {
var count = store.snapshot ? store.snapshot.length : store.getCount()
var cez = k.get("scale")
var ce = ( 2 / count ) * 100
return ce + " % "
}
Question: In database I have for example: scales (that user answered on scale-question)
id | scale
1 | 4
2 | 4
3 | 1
4 | 2
How i can sum scales (and group them of course) and put this in my grid?
For example in my grid i should get:
scale | scale %
1 | 25%
2 | 25%
4 | 50%
I advise you don't attempt to do it inside Grid/Store. Instead process the data before loading it to store - for example do it in database with GROUP BY statement.
To get the sum of values in a store, you can use Store.sum()

Doctrine 2 ManyToOne with multiple joinColumns

I'm trying to select the matching row in the product_item_sortorder table based on a productId and toolboxItemId from the product_item table.
In normal SQL that would be for a given productId:
SELECT pi.*, pis.* FROM product_item pi
LEFT JOIN product_item_sortorder pis
ON pi.productId = pis.productId
AND pi.toolboxItemId = pis.toolboxItemId
WHERE pi.productId = 6
I wrote the DQL for it as followed:
$this->_em->createQuery(
'SELECT pi
FROM Entities\ProductItem pi
LEFT JOIN pi.sequence s
WHERE pi.product = ?1'
);
Then I get following SQL if I output the $query->getSQL():
SELECT p0_.id AS id0, p0_.productId AS productId1, p0_.priceGroupId AS priceGroupId2, p0_.toolboxItemId AS toolboxItemId3, p0_.levelId AS levelId4, p0_.parentId AS parentId5, p0_.productId AS productId6, p0_.toolboxItemId AS toolboxItemId7 FROM product_item p0_ LEFT JOIN product_item_sortorder p1_ ON p0_.productId = p1_. AND p0_.toolboxItemId = p1_. WHERE p0_.productId = ? ORDER BY p0_.id ASC
As you can see the referencedColumnNames are not found:
LEFT JOIN product_item_sortorder p1_ ON p0_.productId = p1_. AND p0_.toolboxItemId = p1_.
Details of the product_item table:
+-----+-----------+---------------+
| id | productId | toolboxItemId |
+-----+-----------+---------------+
| 467 | 1 | 3 |
| 468 | 1 | 10 |
| 469 | 1 | 20 |
| 470 | 1 | 4 |
| 471 | 1 | 10 |
+-----+-----------+---------------+
Details of the product_item_sortorder table:
+-----+-----------+---------------+----------+
| id | productId | toolboxItemId | sequence |
+-----+-----------+---------------+----------+
| 452 | 1 | 3 | 1 |
| 457 | 1 | 4 | 6 |
| 474 | 1 | 20 | 4 |
+-----+-----------+---------------+----------+
ProductItem Entity
<?php
/**
* #Entity(repositoryClass="Repositories\ProductItem")
* #Table(name="product_item")
*/
class ProductItem
{
...
/**
* #ManyToOne(targetEntity="ProductItemSortorder")
* #JoinColumns({
* #JoinColumn(name="productId", referencedColumnName="productId"),
* #JoinColumn(name="toolboxItemId", referencedColumnName="toolboxItemId")
* })
*/
protected $sequence;
...
?>
ProductItemSortOrder Entity
<?php
/**
* #Entity(repositoryClass="Repositories\ProductItemSortorder")
* #Table(name="product_item_sortorder")
*/
class ProductItemSortorder
{
...
/**
* #ManyToOne(targetEntity="Product")
* #JoinColumn(name="productId", referencedColumnName="id")
*/
protected $product;
/**
* #ManyToOne(targetEntity="ToolboxItem")
* #JoinColumn(name="toolboxItemId", referencedColumnName="id")
*/
protected $toolboxItem;
...
}
?>
Your mappings are seriously wrong. You are using ManyToOne on both ends, how is this possible? You have both associations defined as "owning"-side, no mapped-by or inversed-by (See Association Mappings chapter). And you are using join columns of one association to map to many fields in another entity. I suppose you want to do something else, can you describe exactly your use-case?
How you would map your example in YAML (since #Hernan Rajchert's example is only in annotations):
ProductItem:
type: entity
manyToOne:
sequence:
targetEntity: ProductItemSortorder
joinColumns:
productId:
referencedColumnName: productId
toolboxItemId:
referencedColumnName: toolboxItemId

Resources