I have been using Codeigniter with Doctrine 1.2.3 and I am wondering is it possible to use own kind of service classes(like in java ee with hibernate). And how to make those right etc?
Like this:
class FeedbacktypeService {
public function getFeedbacksByName($value=''){
$q = Doctrine_Query::create()
->from("Feedbacktype f")
->where('f.name LIKE :name', array(':name' => $value));
return $q->execute();
}}
Is there better way to do this?
Thanks for your answers and opinions.
Doctrine Table classes provide dynamic finders for all properties on an object. If an object "Feedbacktype" has a property "name", you can do:
return Doctrine::getTable('Feedbacktype')->findByName($value);
It doesn't do LIKE matching, just equality. It will handle any property, and even combinations of AND and OR properties.
The docs are here: http://www.doctrine-project.org/documentation/manual/1_0/en/dql-doctrine-query-language:magic-finders
Related
Basically I'm writing an API using Web API 2 and Entity Framework on the backend.
The thing I'm unsure about is what to do in regards to foreign keys on my models.
Say I got a person property with a foreign key to an order property.
Sometimes the client needs the person property, sometimes it does not. How should I go about this?
Should I create 2 methods:
/api/person/1 <-- returns person without order
/api/personwithorder/1 <-- returns person with order
Seems like an awful lot of methods in my opinion.
I know the queryable attribute exists as well which provides support for the client to use the $extend argument to include properties at will - however I would rather not use the queryable attribute if I can avoid it.
What are your suggestions?
Off the top of my head, here are some options.
Option 1
Multiple methods in API
public Person GetPerson() { ... }
public Person GetPersonWithOrders() { ... }
Option 2
Pass a flag to the method:
public Person GetPerson(bool getOrders) { ... }
Option 3
You could use OData to allow the caller to query the data.
To expand on DavidG's option 3 of using OData, here is how you'd do it:
Install-package Microsoft.AspNet.WebApi.OData
Create a PeopleController inheriting from ODataController
Configure the Web API OData model like so:
modelBuilder.EntitySet<Person>("People");
Define your Get method as returning an IQueryable<Person>
In your calling code, add the expand clause to the URL to specify the child object you would like to expose, like this: /api/People(1)?$expand=Orders
There's a little bit more to it around registering the OData route, but this is all standard configuration that you can find in any sample project.
OData is really very flexible and takes care of tonnes of issues about how you should build your URLs.
I want to make that all my Entities created form an existing database inherits from the same interface.
I suppose this can be done through the templates. And I've seen that ugly .tt file, but there is no help (or I haven't found it).
There are any documentation, examples, ... of the templates?
There are any tips or pre-made templates for common paradigms, for example N-Layer Design or Domain Driven Desing?
Look for "T4 Templates". That will give you introduction to T4 Templates (.tt files).
With a bit of searching you can easily extend this template to your needings. I did this myself already, but with a template from EF4. I don't know if the templates differ.
I made a little helper functions for this:
string Interfaces(EntityType entity)
{
string interfaces = string.Empty;
if (entity.Members.OfType<EdmProperty>().Any(edmProperty => edmProperty.Name == "Guid" && ((PrimitiveType)edmProperty.TypeUsage.EdmType).PrimitiveTypeKind == PrimitiveTypeKind.Guid))
{
interfaces += ", IHasWritableGuid";
}
return interfaces;
}
The part where the template writes the actually entity class (this differs for sure in the new template) is somewhat below "Write EntityType classes." in the EF4 template.
<#=Accessibility.ForType(entity)#>
<#=code.SpaceAfter(code.AbstractOption(entity))#>
partial class
<#=code.Escape(entity)#> :
<#=BaseTypeName(entity, code)#>
<#= Interfaces(entity) #>
Here I just added a call to my interfaces method.
I know this is not the exact answer, but it should give you help with editing the template file yourself. Just bite yourself through it. :)
Assuming that I have a Scala case class that is persisted using the Salat/Casbah/Mongo stack, I want to set up pre-persistence validation logic like I could easily do in Rails using ActiveRecord hooks or in Java using JSR 303 bean validation.
Perhaps there is a better way to think about this in a functional paradigm, but I want to accomplish something like the following:
case class SomeItem(
id: ObjectId = new ObjectId,
someProperty: String) {
#PrePersistence
def validate() = {
//perform some logic
//fail document save in certain conditions
}
}
I am having trouble finding any documentation on how to do something like this in Salat. I do see a #Persist annotation but it seems focused on serializing specific values and not creating hooks.
It seems like one option is to override the save method in the SalatDAO for my case class. Does anyone have an example of this or know of a better, built-in way to handle validation tied to a pre-persistence event?
Thanks!
Salat developer here.
Yes, #Persist is simply for ensuring that fields that aren't in the constructor are serialized - this is particularly useful for manipulating data in MongoDB. One example is where you want to ensure that all the fields are populated with a value so you can sort sensibly, but the value is an Option which may not be present.
Unfortunately, the Java driver doesn't offer lifecycle callbacks like the Ruby driver :(
But what you want should be easy enough to do. Please file an issue at https://github.com/novus/salat/issues and describe how you would like the validation to behave - we can start a discussion and I can try to get something in for you in the 1.9.2 release.
Is there any good and, if possible, exhaustive documentation about ESQL in entity framework ?
I'm trying to make a select of an entity object with modification of a property using a method; something like this :
SELECT foo FROM context.foo WHERE foo.Price = AddTaxes(foo.Price)
There is msdn providing some documentation Entity SQL Language
You can also combine it with Linq2Entities with something like
context.foo
.Where("it.Price == #Price", new ObjectParameter[]
{ new ObjectParameter("Price", AddTaxes(price) } ).ToList()
Starting to play around with EF4 and I notice there are several different ways to query the DB.
Currently I created an .EDMX with my tables/objects created.
In code when I use the Where method, it wants me to pass in a string for the 1st param, ESQL I'm guessing. What I want to use is lambda expressions. All the tutorials show this, but isn't how I'm setup for some reason.
new Entities().Users.Where();
Ah, you must include the namespace System.Linq to get the extension methods.
Assuming your user entity has a property called LastName, you would write something like this to get all users with the
Entities().Users.Where(u => u.LastName == "Hansen");