Reading directly from the Doctrine Searchable index table - database

I've got a Doctrine table with the Searchable behavior enabled.
Whenever a record is created, an index is made in another table. I have a model called Entry and the behavior automatically created the table entry_index.
My question now is: How can I - without using the search(...) methods of my model use the data from this table?
I want to create a tag cloud of the words most used, and the data in the index table is exactly what I need.

Doctrine generates table EntryIndex that should be available from Doctrine::getTable('EntryIndex').
Additionally Entry has EntryIndex relation that refers to index table and EntryIndex has Entry relation. The relation is standard one-to-many (1-n) relation between Entry and EntryIndex.

Related

create a unique constraint over multiple tables

I am creating a database where we want to combine data of several sites into one database. I now have an issue with the unique constraint for the samplepoint table. for each site the samplepointname must be unique. in the old system I enforced this with a unique constraint. The problem in the new system is that the siteID's are not stored in te table with samplepoints because these are enheritted from the parent of samplepoints (projects).
can I create a unique constraint that include the siteID stored in its parent, or should I create a siteID field in the table itself
I'm a bit confused by some of the phrasing of the question, so I'm going to lay out some clarifying assumptions based on what I think is my best read of it. Hopefully these assumptions actually match your situation.
In the original configuration, you had:
a single site
represented by a single pair of tables named "project" and "samplepoints"
a unique constraint over a field named "samplepointname"
a field named "siteID" in in a table named "project"
it had previously been unnecessary to add "siteID" to "samplepoints" because there was only one row in "project" and that one row's single "siteID" was always implied throughout the table "samplepoints"
And in the new configuration you have the following changes:
multiple sites
one row for each site in the table "projects"
a unique value for each field "siteID" in "projects"
You've stated that the field "sitepointname" within each site must be unique, but not globally. So I'm going to work with that.
Given these assumptions, you almost certainly will not merely want but need to add "siteID" to your table "sitepoints". This is because you can no longer simply read from "projects" and "sitepoints" at the same time without either joining them or adding a WHERE clause to filter down to the relevant site.
In fact, if your table "sitepoints" has already been populated without "siteID" you may well need to obtain the original tables from all of the different sites, empty that consolidated table, and repopulate it such that "siteID" correctly represents each independent site.
After you've added the new field "siteID", you'll remove the UNIQUE constraint on the field. You're going to replace that with what you'll use instead, and if you don't remove it all names will need to be unique across all sites rather than just within each site.
If you're simply executing commands directly, this will create that index:
CREATE UNIQUE INDEX unique_sitepointnames ON sitepoints (siteID, sitepointname);
The index name "unique_sitepointnames" is just an identifier, it can be whatever you wish, but that's my suggestion for it as it's clear and describes the purpose.
Rather than "UNIQUE" being a constraint on the column, "UNIQUE" is here a constraint on the index. Any more options to how the index is created is just optimization.

Why do entities need keys in Appengine datastore

What is the usage of keys in the appengine datastore: I am new to Appengine, any info on it would be great.
Comparison
To keep things simple, let's assume MySQL stores all the rows of a table in a single file. That way, it can find all the rows by scanning that file.
App Engine's datastore (BigTable) does not have a concept of tables. Each entity (~row in MySQL) is stored separately. [It can also have a individual structure (~columns).] Because entities are not connected in any way, there is no "default" method to go through all of them. Each entity needs an ID and must be indexed.
Key Structure
A key consists of:
App ID (the closest thing in MySQL is a database).
Kind (the closest thing in MySQL is a table).
ID or name (the closest thing in MySQL is a primary key).
(Optionally) Parent key (all the above of another entity). (Details omitted for the sake of simplicity.)
Please note that what is meant by the closest thing is conceptual similarity. Technically, these things are not related. In MySQL, databases and tables represent actual storage structures. In BigTable they are just IDs, and the storage is actually flat, i.e. every entity is essentially a file.
In other words, identity-wise, a key is to an entity as the database + table + primary key are to a row in a MySQL table.
Key's Responsibilities
An entity's key:
States what application the entity belongs to.
What kind (class, table) it is of.
By the means of the above and either a numeric key ID or a textual key name, identifies the entity uniquely.
(Optionally) What the parent entity of the entity is. (Details omitted for the sake of simplicity.)
Usage
So that you can retrieve all entities of a kind, App Engine automatically builds indexes. That means App Engine maintains a list of all your entities. More specifically, it maintains a list of your entities' keys.
Complex indexes may be defined to run queries on multiple properties (~columns).
In contrast to MySQL, every BigTable query requires an index. Whenever a query is run, the corresponding index is scanned to find the entities that meet the query's conditions, and then the individual entities are retrieved by key.
A common high-level use is to identify an entity in a URL, as every key can be represented as a URL-safe string. When an entity's key is passed in the URL, the entity can be retrieved unambiguously, as the key identifies it uniquely.
Moreover, retrieving an entity by its key is strongly consistent, as opposed to queries on indexes, which means that when entity is retrieved by its key, it's guaranteed to be the latest version.
Tips
Every entity stored in BigTable has a key. Such a key may be programmatically created in your application and given an arbitrary key name. If it's not, an numeric ID will be allocated transparently, as the entity is being stored.
Once an entity is stored, its key may not be changed.
The optional parent component might be used to define a hierarchy of entities, but what it's really important for is transactions and strong consistency.
Entities that share a parent are said to belong to the same entity group.
Queries within a group are strongly consistent.
Just to reiterate, retrieving an entity by its key or querying an index by a parent key are strongly consistent. Retrieving entities in other ways (e.g. by a query on a property) is eventually consistent.
Glossary
Entity - a single key-value document.
Eventual consistency - retrieving an entity (often a number of them) without the guarantee that the replication has completed, which may result in some entities being an old version and some being missing, as they have not yet been brought from the server they were stored on.
Key - an entity's ID.
Kind - arbitrary textual name of a class of entities, such as User or Article.
Key ID - a numeric identifier of a key. Usually automatically allocated.
Key name - a textual identifier of a key.
Strong consistency - retrieving an entity in such a way that its latest version is retrieved.
(I intentionally used MySQL in the examples, as I'm much more familiar with it than with any other relational database.)
Please read https://developers.google.com/appengine/docs/java/datastore/#Java_Entities ... you may want to delete your question and ask again after you have studied this documentation section.
(This is meant to help you, not complain.)

How to fetch data objects using web service in backbone.js

I have a relational database where two tables are linked by a key. Depending on a search criteria in the parent table, the web service will return a list consisting of child table records.
For example, if I have the parent table as "Version" with a column v_type and primary key v_id which references child table "Reference" with primary key ref_id and foreign key v_id. I need to fetch the list of records of table type "Reference".
How do I parse the list obtained?
How do I write the template and view?
A very simple example or syntax will be very helpful.
I think you can start by looking at Backbone-relational, that is where all database relational questions do get simple answers/solutions.

ORMLite foreign constraint that is not linked to the PK

In ORMLite, how can I define a constraint (foreign) to another table, which is not linked by it's integer id, but by any other field, i.e. a varchar/string field, which isn't the PK.
For example, referring to the ORMLite sample code, where an 'order' entity is linked to an 'account' entity. In the example, the order is linked to the account by it's id column (I guess by default), which is the PK.
Instead of setting up the constraint from 'order.account_id' to the 'account.id' column, how can I set it up from something like 'order.account_name' to 'account.name' column instead?
I was looking for something like a foreignColumnName annotation, but unfortunately it doesn't exist, for a one-to-one relationship.
Here's the java code of the ORMLite examples:
Order.java
Account.java
I couldn't find any info in the documentation.
(The reason why I need it is that I have an existing db, not created by ORMLite automatically, which has obviously not been setup properly nor halfway normalized, but I need to work with that existing one, including it's existing column names and constraints.)
I was looking for something like a foreignColumnName annotation, but unfortunately it doesn't exist, for a one-to-one relationship.
Edit:
This feature was added to ORMLite in version 4.36. The concept of foreign objects was a new construct at the time. See the javadocs on the field here. To quote:
public abstract String foreignColumnName
Name of the foreign object's field that is tied to this table. This does not need to be specified if you are using the ID of the foreign object which is recommended. For example, if you have an Order object with a foreign Account then you may want to key off of the Account name instead of the Account ID.
NOTE: Setting this implies foreignAutoRefresh() is also set to true because there is no way to refresh the object since the id field is not stored in the database. So when this is set, the field will be automatically refreshed in another database query.

In generating a Linq to SQL class, how can I define a complex relationship?

I have two views: one is a normal view built from one table with an integer ID and other columns for the record (let's call it View1). I have another View (View2), which has an integer ID column and a second column named "table" (type: varchar). That second column contains the name of the table to which the ID column is related: So, if View2 contains an ID of 999 and its "table" column contains the value "View1", that means the record referenced is ID 999 from View1.
Far as I can tell, DBML only allows for one-to-one or one-to-many relationships based on explicit column references; I'd rather express the relationship as a one-to-one based on the ID column AND View1.table being equal to "View2".
Is this possible? I know I can simply do an outer join in the linq query, but I'd rather avoid that if possible. Thanks!
It's not possible. The linq2sql mapper allows for mapping explicit foreign key relations, but if you don't actually have a foreign key relationship in the database, it's not possible for L2S to "infer" the relation in any way.

Resources