I have a self referencing table as covered in the doctrine documentation (which is already in heavy use throughout the application and therefore changing to nested tree isn't a current option).
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html#one-to-many-self-referencing
The table structure looks like this:
+----+----------+--------+
| id | parentID | Name |
+----+----------+--------+
| 1 | null | Name 1 |
| 2 | 1 | Name 2 |
| 3 | 1 | Name 3 |
| 4 | 2 | Name 4 |
| 5 | 1 | Name 5 |
--------------------------
Up until now in our forms we've been using the entire entities content our form, ie
$builder->add('hierarchyid', 'entity', array(
'class' => 'AcmeTestBundle:Hierarchies')
Which works fine, but now I want to be able to amend this to set points in the table, ie something like:
->add('hierarchyid', 'entity', array(
'class' => 'AcmeTestBundle:HierarchiesTest',
'query_builder' => function(HierarchiesTestRepository $repo)
{return $repo->findBy??????}
But I've hit a mental block how to complete this, any ideas?
One option I've considered is I already have code that builds an array collection of these entities, which is used elsewhere, but I'm unfamiliar with how I should go about implement a doctrine arraycollection in a form type.
private function createNodeArray($node)
{
$this->hierarchyArrayCollection->add($node);
foreach ($node->getChildren() as $hierarchy)
{
$this->createNodeArray($hierarchy);
}
}
query_builder option should return QueryBuilder object and I assume your findAllById() method returns actual data. You need to change that ;)
Other than that, you should probably add property option to indicate which field is to be displayed in list...
For anyone interested the solution I went with in the end was to generate an array and use choice field type rather than entity,such as this:
->add('hierarchyid', 'choice', array(
'choices' => $hierarchy)
Related
I'm trying to display custom names for the Row data fetched using IN condition in Kusto.
Below is the table structure-
Below is the query I've used-
customEvents
| project Action=customDimensions["ActionInvoked"]
| where Action in (
"viewSelected",
"myProjectsSelected",
"watchlistSelected"
)
| summarize count() by tostring(Action)
| sort by count_
| render columnchart
The output to the query is as below-
As noticed in the output highlighted column names ("viewSelected","myProjectsSelected" & "watchlistSelected") are being rendered as is.
These are not User friendly and I'd like to change it.
NOTE: I'm just a day old to Kusto, so my query might be bad. Please feel free to change it to a better one if needed.
You'll need to replace your original values in the Action column by the names you want to be displayed. The easiest way to do it is to use the case function like this (look at the | extend Action = case(...) part):
customEvents
| project Action = tostring(customDimensions["ActionInvoked"])
| where Action in (
"viewSelected",
"myProjectsSelected",
"watchlistSelected"
)
| extend Action = case(
Action == "viewSelected", "Default Views",
Action == "myProjectsSelected", "Custom Views",
"Watchlist")
| summarize count() by Action
| sort by count_
| render columnchart
By the way, note that I moved the tostring higher up in the query, for convenience.
And here's how the new query works on your data:
datatable(Action:string) [
"viewSelected",
"myProjectsSelected",
"watchlistSelected",
"watchlistSelected",
"viewSelected"
]
| extend Action = case(
Action == "viewSelected", "Default Views",
Action == "myProjectsSelected", "Custom Views",
"Watchlist")
| summarize count() by Action
Output:
Action
count_
Default Views
2
Custom Views
1
Watchlist
2
I want the following table structure, to store an auto increment a row's pid* attribute
| id | timstamp | pid*
| 00000000-1111-2222-93bb-0371fcb45674 | 0 | 1
| 00000000-1111-2222-93bb-ee742a825e88 | 1 | 2
| 00000000-1111-2222-93bb-bfac8753c0ba | 2 | 3
PutItem -> autoId() | timestamp() | max(pid) + 1 = 4 ??
For PutItem operation, Is something like the following 1) possible, and 2) acceptable in DynamoDB land?
"pid" : $util.dynamodb.toDynamoDBJson(records.findMax(pid) + 1) # just pseudo code
3) How might one implement the above using DyanmoDB resolver mapper template?
Use case:
I'm trying to use AWS DynamoDB to back GraphQL, managed by AWS AppSync, the following is the request mapping template for Mutation.createFoo
{
"version" : "2018-05-29",
"operation" : "PutItem",
"key" : {
"id": $util.dynamodb.toDynamoDBJson($util.autoId()),
},
"attributeValues" : {
"timestamp" : $util.dynamodb.toDynamoDBJson($util.time.nowEpochMilliSeconds()),
"pid" : $util.dynamodb.toDynamoDBJson($ctx.args.input.pid), # currently client provided but it is not acceptable
...
}
}
The primary key id is an UUID auto-generated by DynamoDB which is fine. But our use-case requires a incrementing pid for each new Foo in our FooTable. The business model requires at least for show a unique pid, while under the hood, queries like GetItem the UUID and timestamp will be used instead and business as usual.
I'm also weary to call for a change in business model because of an implementation detail issue.
References:
Resolver Mapping Template Reference for DynamoDB
I have a table like below in Room, in a Android application, I use Raw Query to get data. Can it be sorted by second value in array sorting_field?
---------------------------------------------
| id | other_fields | sorting_field |
---------------------------------------------
| 1001 | … | ["24","0.02","2"] |
---------------------------------------------
Initially I did the sorting part in Repository with Transformations.switchMap, inside the function a MutableLiveData> and applied Collections.sort.
It worked like a charm:
Collections.sort(list, (o1, o2) -> Double.compare(Double.valueOf(o1.sorting_field().get(positionInList)), Double.valueOf(o2.sorting_field().get(positionInList))));
After Paging implementation, I took the sorting logic out, moved to queries builder and here I am.
I am using Symfony3.4 and have the following relationship in the database between House and Type entities:
house
+----------------------------------------------+
| id | title | description |
+----------------------------------------------+
| 1 | some title 1 | some description 1 |
+----------------------------------------------+
| 2 | some title 2 | some description 2 |
+----------------------------------------------+
type
+----------------+
| id | name |
+----------------+
| 1 | shortstay |
+----------------+
| 2 | rent |
+----------------+
| 4 | sell |
+----------------+
house_types
+-----------------------------------+
| id | house_id | type_id | price |
+-----------------------------------+
| 1 | 1 | 2 | 1000 |
+-----------------------------------+
| 2 | 1 | 3 | 1000000 |
+-----------------------------------+
| 3 | 2 | 1 | 100 |
+-----------------------------------+
| 4 | 2 | 3 | 200000 |
+-----------------------------------+
Here are my entities:
House entity
class House extends EntityBase
{
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\HouseHasTypes", mappedBy="houses", cascade={"persist","remove"})
*/
protected $hasTypes;
Type entity
class Type extends EntityBase
{
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\HouseHasTypes", mappedBy="types", cascade={"persist","remove"})
*/
protected $hasHouses;
HouseHasTypes entity
class HouseHasTypes extends EntityBase
{
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\House", cascade={"persist"}, fetch="EAGER")
* #ORM\JoinColumn(name="house_id", referencedColumnName="id", nullable=true)
*/
protected $houses;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Type", cascade={"persist","remove"}, fetch="EAGER" )
* #ORM\JoinColumn(name="type_id", referencedColumnName="id",nullable=true)
*/
protected $types;
/**
* #var int
*
* #ORM\Column(name="price", type="integer")
*/
protected $price;
I did not quite get the difference between ManyToOne and OneToOne relationship. Which one should mine be?
Also, what would be the easiest way to handle inserting new data into the 2 entities (House and HouseHasTypes). Right now I can render the checkboxes for each row in type table as follows:
$builder
...
->add('hasTypes', EntityType::class, array(
'required' => true,
'class' => Type::class,
'expanded' => true,
'multiple' => true,
))
And the in the controller I am planning to make a foreach(){} for each of the checked Types and setHouse() and setPrice() there. Is there any other more efficient way to do this?
How can I render as many textboxes as there are the abovementioned EntityType checkboxes to define a price for each Type selected? I am currently hardcoding three html input text elements and hiding them with javascript based on checked checkboxes. If I make a custom form, how can I tell it to render 3 text inputs (since there're 3 rows in the entity table)
Sorry for asking so many questions. Any help would be much appreciated
Your database model seems correct.
To define you the OneToMany, ManyToOne and OneToOne relationship, I made you little examples:
Imagine we have two entities: USER and ORDER.
OneToMany:
An user can do 0, 1 or many orders right ? It means that One user can be linked To 0, 1 or Many orders.
USER(id:1) --> ORDER(id:1)
--> ORDER(id:3)
--> ORDER(id:8)
ManyToOne:
An order can be done by one and only one user right ? It means that Many orders can be linked To One user.
ORDER(id:1) --> USER(id:1)
ORDER(id:3) --> USER(id:1)
ORDER(id:8) --> USER(id:1)
OneToOne:
Now imagine we have two others entities: USER and EMAIL.
An user can have one and only one email address and an email address can have one an only one user right ? It means that One user can be linked To One email address and One email address can be linked To One user.
USER(id:1) --> EMAIL(id:6)
USER(id:3) --> EMAIL(id:7)
USER(id:8) --> EMAIL(id:1)
Hope I was clear.
For your others questions, Symfony will automatically insert data on form submit if you have correctly defined your getter and setter in entities no needs to do a foreach
Normally,form shows the select code though,
I have two tables which has Many-to-one relation.
for example
MainTable
ID | name |job
1 | Mr.A |1
2 | Mr.D |1
3 | Mr.C |3
jobTable
ID | name
1 | doctor
2 | engineer
3 | teacher
then ,I am making the form to input Maintable,normally
symfony uses select box for this kind of choice,but I use radio button like this.
$form = $this->createFormBuilder($mainTable)
->add('job','entity',array(
'class' => 'AcmeTopBundle:jobTable','expanded' => true))
Then I would like to preselect the one button.
There I found hint.
How to pre-select a form radio item with Symfony 2?
it says to use
'data' => 1
But my choices is jobTable entity.
How can I put the jobtable entity?
'data' => ???
You have to pass the entity record it self as data. So, from a Controller :
$em = $this->getDoctrine()->getEntityManager();
// If you want to preselect record with ID #1
$preselectedData = $em->getReference('AcmeTopBundle:jobTable', 1);
$form = $this->createFormBuilder($mainTable)
->add(
'job','entity',array(
'class' => 'AcmeTopBundle:jobTable','expanded' => true,
'data' => $preselectedData
)
)
// ...
;
Its easier to set the default value in your entity/value object. The form component take care of them and preselect it.