I have a table called products, and a mapping table called related_products which maintains a parent-child relationship between products, e.g. product SKU_ID 1 has related products 2 & 3.
products
+-----------------+
|SKU_ID | name |
+-----------------+
| 1 | Blah
| 2 | Blah2 |
| 3 | Blah3 |
+-----------------+
related_products
+---------+------------+
|SKU_ID_1 | SKU_ID_2 |
+---------+------------+
| 1 | 2 |
| 1 | 3 |
| 3 | 2 |
+---------+------------+
ORM Associations:
OneToMany relationship between products.SKU_ID and related_products.SKU_ID_1
OneToOne relationship between related_products.SKU_ID_2 and products.SKU_ID
This works fine in my application, but when I look at the returned objects in can see that because of the circular nature of the relationships, it seems like I'm returning too much data. Example: If I get all related products belonging to product SKU_ID=1, I get products 2 & 3 as expected. From those product objects I can also get their related products, then the related products of their related products and so on and so on.
Is this a problem? and if so how can I restrict the 'depth' of the returned associations? or have I done the mapping incorrectly in the first place?
I think the association like this is fine.
Since Linq2sql is lazy loading by default, there is no need to restrict the dept. You will only retrieve the related products as soon as you are using them in your code. (In case you are worried about loading too much, just profile the sql and you will see when/to what level they are loaded).
You might run into problems in case you are eager loading using LoadWith LoadOptions on the DC. I would not know how to deal with that.
Thanks to Jim Wooley I know that LoadOptions are not allowed with circular references....
Related
I have an Asset entity, and I want multiple other entities (let's say Product and Page) to have many-to-many relations to Asset, without having multiple tables (product_asset, page_asset).
The reason I want this is because want to simply display all assets, without first having to find all entities that have a relation to the Asset entity.
The Translatable Doctrine Extension is a perfect example of what I mean. There's one Translation entity containing an ObjectClass property that stores object's class name. I've been looking through to code for some time now, but just can't seem to figure out how they actually make it work.
So concrete, how can I map entities based on their class name like the Translatable Extension does? To have a structure like this:
|----|--------------------|----------|-----------|
| id | objectClass | objectId | value |
|----|--------------------|----------|-----------|
| 1 | App\Entity\Product | 1 | test.jpg |
| 2 | App\Entity\Page | 1 | test2.png |
| 3 | App\Entity\Page | 2 | test3.gif |
|----|--------------------|----------|-----------|
Thanks in advance!
In the Translatable behaviour of the Gedmo Doctrine Extensions, it has instructions on "Personal Translations". Could someone clarify what personal translations are?
tl;dr
Personal translations are used when you want to handle the translation entity (with it's own table by yourself) instead of the default behaviour, by using a single table for all translations.
By using the default behaviour, you get a single table called ext_translations which holds all of your translation data. I'll give you an example based on the documentation of DoctrineExtensions.
Let's say we create an Article entity with 2 translatable fields - title and content. That would mean that we should have the following table structure of articles:
+----+-------+---------+
| id | title | content |
+----+-------+---------+
Now, by default the TranslatableListener sets en_us locale every time you create new entity, thus populating only articles table:
$article = new Article();
$article->setTitle('Title english');
$article->setContent('Content english');
Would lead to the following:
+----+---------------+-----------------+
| id | title | content |
+----+---------------+-----------------+
| 1 | Title english | Content english |
+----+---------------+-----------------+
By now, only articles get to be updated with new records, but when you want to translate those fields with different locale, our common table ext_translations get updated as well.
The table has the following structure:
+----+--------+---------------+---------+-------------+------------+
| id | locale | object_class | field | foreign_key | content |
+----+--------+---------------+---------+-------------+------------+
So, what happens when we update our record with some new translations:
$article->setTitle('My title');
$article->setContent('My content');
$article->setTranslatableLocale('de_de');
When we persist our updated entity, we get the following structure in ext_translations:
+----+--------+---------------+---------+-------------+------------+
| id | locale | object_class | field | foreign_key | content |
+----+--------+---------------+---------+-------------+------------+
| 1 | de_de | Bundle\Entity | title | 1 | My title |
| 2 | de_de | Bundle\Entity | content | 1 | My content |
+----+--------+---------------+---------+-------------+------------+
Now you know how the default behaviour works. It stores all of your translations (not just for single entity, all of them) in a single table.
But when you're using a personal translations you can store your (let's say for the sake of our example) Article translations to its own, separate table, article_translations.
If you are familiar with DoctrineExtensions provided by KnpLabs, then you've already seen what stands for PersonalTranslations. Link for their documentation about this subject can be found here.
Well hopes this can clarify things a bit for you. Let me know if you have more questions about this.
On a multilingual Drupal site, I am trying to create a View that lists all the terms for a particular Taxonomy, and include the count of nodes associated with that term, broken down by language. For the most part, it works, except for when I have content for a term in one language, but not another, it doesn't show correctly in that language.
Sample data:
Taxonomy: Education, Food, Travel
Languages: English, Korean
Nodes:
Name | Taxonomy | language
Education Test E | Education | en
Education Test E2 | Education | en
Education Test K | Education | ko
Food Test E | Food | en
Desired results:
Taxonomy | Language | Count
Education | en | 2
Education | ko | 1
Food | en | 0
Food | ko | 1
Travel | en | 0
Travel | ko | 0
What I'm getting:
Taxonomy | Language | Count
Education | en | 2
Education | ko | 1
Food | ko | 1
Travel | en | 0
Travel | ko | 0
In SQL, I've figured out the query I need:
SELECT l.language, td.name, COUNT(n.nid) AS Questions
FROM languages l
CROSS JOIN taxonomy_term_data td
LEFT OUTER JOIN field_data_field_qa_topic qat ON td.tid=qat.field_qa_topic_tid AND qat.deleted= '0'
LEFT JOIN node n ON qat.entity_id = n.nid AND n.language=l.language
WHERE td.vid=9
GROUP BY l.language, td.name
ORDER BY td.weight, td.name, l.language;
But I can't figure out how to get it working in Views. (And yes, I have the Views Cross Join module installed.)
Another option- I've created this query as a view in MySQL, but can't figure out how to expose the SQL view in Drupal Views.
Either method (creating a Drupal View or exposing the SQL view to Drupal) would be fine at this point- I just want to get it working. Thanks!
EDIT: Just to clarify what I'm trying to achieve, I want a list of ALL terms for this particular taxonomy (so users get a link to the corresponding page) AND a count of existing content pages for each term (including those that have 0). I would prefer to have a filter on just the current user's language, but I can also use it with all the languages and filter out in code.
Sorry if i misunderstood you, but if the thing you want to achieve is to get the count for the language the current user is using right or the current language the site is using than you must add the Filter Criteria - Content: Language and select the users or sites language.
Or you want to get a data table of all language / content list?
I am almost there with this but cannot seem to get this functionality going as planned.
I am creating a questionnaire using drupal content type. What I am trying to do is to create a table like structure as below in content type. The second and third column contain check boxes and first column data(i.e computer, internet) and first row(i.e Everyone have access , Nobody have access) are taxonomy terms . Is it possible to display like this in content type by using some modules in drupal? Anybody have any better suggestions?
| | Everyone have access | Nobody have access |
---------------------------------------------------------
| Computers | 1 | 2 |
---------------------------------------------------------
| Internet | 1 | 2 |
---------------------------------------------------------
| Fax | 1 | 2 |
---------------------------------------------------------
You can use the Term Level Field module. This module provides a field type for referencing terms with a level to an entity.
You may use Editable fields with Views module. Of course, if you didn't need such a display (table forms) you should use Views Bulk Operations modules.
If you need to do this in a node use Tableform module. But if you want to show nodes whike editing a node it is the same. A node should not be used for tasks just for content.
Imagine a user who is linked to a company:
User
- id
- username
- company_id
The company list comes from a procedure call (external db), no hands on the related table
EXEC getCompanies;
+----+-------+
| id | name |
+----+-------+
| 1 | comp1 |
| 2 | comp2 |
| 3 | comp3 |
+----+-------+
How would you integrate this to allow SonataAdmin to render a proper input selection for company in User editition, and display the company name in User list ?
Do you know where I could find some examples about this particular case ?
I did a custom company field type which fetchs his values from the procedure call, but I'm not sure it's the best idea and I'm not able to display the comany name in the list.
You could use a choice type and if there are too many companies to display, you can use GenemuFormBundle with Select2 library to have filtering select widget.