Consider the following table structure...
Appointment
-----------
ID integer
Description nvarchar
StatusID smallint
Status
------
ID smallint
DisplayText nvarchar
Now, for good or for evil, we want this situation to map to a class that looks like this
class Appointment
{
public int ID {g;s;}
public string Description { g;s; }
public string Status { g; private s; }
}
I.e. we want to pull the normalised status display text straight into the entity.
The obvious answer is to create a Status entity and make the appointment class have a reference to that and map it in the normal way.
Don't create an entity class. Use an enum and EnumStringType as shown here. This is exactly what you want I think.
Related
I am using Eloquent to pull users out of my database, and i want to "join" the postal code from my users table with the postal code from my cities table, to retrieve the city name.
Heres a simplified version of my tables:
users =>
id
username
password
postal_code
cities =>
postal_code
city_name
In my User model i have a method called city() that declares the relationship with my cities table:
public function city() {
return $this->hasOne('cities', 'postal_code');
}
The problem now is when i try to do User::with('city')->find($user_id), the value being passed to the method is the primary key, the id, instead of the postal_code.
So my query ends up being (this is for the user with id 1):
select * from `cities` where `cities`.`postal_code` in ('1')
Is there someway to specify that i want to pass the postal_code instead?
There are a few things going on here. This is actually a belongs to relationship because the user holds the value that relates it to the city. The relationship would be better defined like this.
public function city()
{
return $this->belongsTo('City', 'postal_code');
}
The user only belongs to one city but the city has many users. In the City model you would have.
public function users()
{
return $this->hasMany('User', 'postal_code');
}
Now you will still have problems here because Laravel expects the relationships to use the primary key of the model. In order to get this working you will need to make the postal code the primary key for the City model.
class City extends Eloquent {
protected $primaryKey = 'postal_code';
}
This will effect every other place that you use a primary key with the City model but I think that should be OK given your current structure.
$city = City::find($postal_code);
I hate to throw a wrench into your plan here but in many places a city will have many postal codes. :)
Since the Relation class uses parent->getKey() (parent is User on your case) this will result in '1'.
I don't think changing the key for User to postal_code is good option ;)
So my guess is to give the users table a 'city_id' column and the cities an 'id' column, so things can work as designed.
An other would be not to return a relation, but something like.. return City::wherePostalCode($this->postal_code)->first();
i have a table
table Foo
(
id,
FooType,
FooTypeName,
)
FooType is an enum with fixed values hence i want to map it to
public virtual FooType FooType { get; set; }
How can i serialize the property in both columns but use only one to read and query?
Which mapping (xml, Mapping by Code, FluentMapping) doesn't matter
You could create FooTypeName as a string property projecting the value of FooType, and map it as readonly.
Alternatively, if you don't need FooTypeName at all, just ignore it in your model and keep it updated with a trigger.
Or, if you feel like coding a lot, create a user type that maps FooType to the two columns.
I have a Employee class, i am retriving employee data using nhibernate.It gives
complete column.(eq. sql - Select * ). But, if i want to have slected column (like EmployeeName and EmployeeID only),then i need to create a class with these two properties(e.g.Empl class).and using AliasToBeanResultTransformer i.e. Projection i can retrive. I want to know that is there any way in nhibernate (without creating sub classes(Empl class), or without hard coding hql query)so that i can retrive specific column values...
class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int ResidingInCountryId { get; set; }
public virtual Country ResidenceCountry { get; set; }
}
If you only want to project a few columns from a query then you can either
a) use a DTO although you mentioned you don't like this for some reason
b) if you REALLY want a list of anonymous objects then simply omit the result transformer which will result in a list of anonymous objects with the projected values. This would be sufficient for databinding.
Also you do not need to use just HQL for projectons, you can use Criteria, QueryOver or NH Linq.
Let's say I have this database design:
Items
Id
Name
Desc
ItemTags
ItemId
TagId
Tags
Id
Tag
And I want to map it to the following class
class Item
int Id
string Name
string Desc
IList<string> Tags
Please note that I don't want to declare a Tag class, I just want the Item class to have a list of strings that represent tags.
Is this possible?
Why dont you want a Tag class?it will be strongly typed and alot easier to implement...
Ref:
IList< Tag> Tags;
override the ToString() to write the "Tag" property if that makes it better..
I have some tables that represent various types. They usually just consist of an ID (int), and a name. Ideally I would have this in an enum. Is there a way to map a table like this onto an enum?
EDIT: How would I handle it if there were extra fields other than an ID and Name?
If it's just an id and a name, I generally do this:
public enum FootScent : int
{
Unknown = 0,
Mild = 1,
Sweaty =2,
SteppedInSomething = 3
}
and then on the LINQ entity property:
[Column("foot_scent_id", DbType = "Int NOT NULL")]
public FootScent Scent { get; set; }
For lookup tables with columns other than "id" and "name" that are needed, I usually just make a normal LINQ entity for them, though depending on your implementation it would probably be worth caching these to minimize trips to the DB.