DAO Best practices : Querying Parent / Children - database

Given you have a domain model with a simple parent / child relationship, and each entity has it's own associated DAO, eg:
Author
AuthorDAO
Book
BookDAO
If I want to add a DAO method for retrieving the books by a specific author, what's the best place for it?
class AuthorDAO
{
List<Book> getBooks(Author author);
}
or
class BookDAO
{
List<Book> getBooks(Author author)
}

I'd place a Book finder on the BookDAO. This is where I would expect it to be, regardless of its parameters. If you want to find a book, you don't want to scan all DAOs to find where the right finder is. Putting it somewhere else won't help the users of the API.

The BookDAO, because you want to get information about books. The Book is the main subject here.

Related

cakephp: blog tutorial tutorial extended

I am trying to extend the simple blog tutorial found in the documentation.
I have an index.ctp on /app/View/Users/index.ctp with all users being shown with a link on each username.... When i click on the username, I expect it to go to the view.ctp and show each user's blog posts and the number of posts each user has done.
I get an error here. Do I need to write a component for this? If so, how do I go about it?
class UsersController extends AppController {
public function view($id = null) {
$this->set('allposts',$this->Post->findByUserId($id));
}
}
So I get an error at this point because it cannot see the Post model in the user model. How do I go around this?
Thanks....
No, you don't need a component for that. If you have linked your models correctly (that's it, set belongsTos and hasMany(es) etc etc), then you have to access the post by concatenating.
$this->set('allposts',$this->User->Post->findByUserId($id));
Please see other questions regarding this.
Now, extending this a little bit since you seem a bit confused on what Components actually do. Components are a way to extending or adding functionalities to Controllers. If you want to, say, create a new Paginator, or call Facebook from your controller to get info, and there's no definite place to put that (users can have facebook info, but if you have another controller named "Friends", you might have to put some facebook functions there too).
Normally, if you think you need a component to have calls from one model to another, that's a sign you are organizing things wrongly (not a general rule, I'm open to examples on when this isn't true).

How does cakePHP naming convention work?

I'm relatively new to PHP. Started learning PHP, but then come across cakePHP, which is suppose to speed up development time.
After reading the documentation and blog tutorial I still don't understand the naming convention. I guess I won't know until I start to do some examples, but to get me started can someone please explain to me how cakePHP associate database tables to the controller/model layer?
The below code is an abstract from the tutorial. It is a controller method that passes the post id to the view layer. The database table is called "posts". $this->Post refers to the model class of Post, which correlates to the plural form of posts in the database.
public function view($id = null) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
OK I get that. Then, in the documentation it refers to the following correlation:
ReallyBigPerson and really_big_people
So it seems like the correlation actually follows the rule in English semantics. Does this mean that cakePHP has a list of singular and plural words hidden somewhere that it works from? For example can I use the below correlation without breaking the code?
This and these or Man and men or Foot and feet or Moose and moose or Goose and geese
Furthermore, if I have both singular and plural form of tables in my database, will it break the code, or will it just associate to the plural-formed table?
Just find it baffling... Why couldn't they just match the naming convention like for like with prefixes?
Inflector
CakePHP uses its Inflector class to determine the plurals of things.
Since the naming conventions dictate that model names are singular and tables names are pluralised, it uses the inflector to apply English semantics / rules to determine the plural.
If you need some help understanding the output of the Inflector, you can use the CakePHP inflector website.
Pluralisation Examples
Model name: Post
Table name: posts
Model name: User
Table name: users
Model name: Sheep
Table name: sheep
Model name: News
Table name: news
Model name: Radius
Table name: radii
Check the Inflector site to be sure.
Non-standard Table names
While CakePHP offers a standard rule set for naming and conventions, none of it is set in stone. If you want to change the name of the table used for a particular model, simply specify the table name in the model:
class Thing extends AppModel {
public $useTable = 'somethings';
}
Or, if you want a model that does not use a table:
class Post extends AppModel {
public $useTable = null;
}

How to implement such an application using MVVM

I'd like to implement a sort of Addressbook/Contactbook using a Datagrid (or a List) and the MVVM pattern.
Something like in Outlook/Thunderbird, where you've a list of your contacts displayed with a 2-3 main fields (name surname for example), and when you double-click a contact, then you get a new modal box that displays all the details of this specific contact.
In fact my scenario is much more similar to an application that manages Customers, Orders and Products. The user would have as main view 3 datagrids showed through 3 tabs, one shows the list of Customers, one the Orders and one the Products.
Then in each view, you can Add (through opening an extra dialog), Delete (under certain conditions) an object.
Each object has a relation with another one.
For example, in a Customer instance, I've a list of Orders for that Customer and for each Order a list of Products ordered.
Since a couple of weeks/months, I'm reading a lot of stuff about MVVM pattern on the net, but somehow, I get confused. Until now, I could find any sample like this. (perhaps, I searched wrong?)
I'd like to implement something like this using the MVVM pattern.
How could I organize such an application?
Could someone help, how to structure it?
Is there a sample somewhere?
Thx in advance for your help.
Fred
1) This video helped me with understanding the basics of MVVM.
2) Search on SO for "MVVM Master Detail".
3) "Delete (under certain conditions) an object": read about Commands and Relay Commands:
private RelayCommand _delete;
public ICommand Delete
{
get
{
return _delete ?? (_delete = new RelayCommand(action => DoDelete(), condition => CanDelete));
}
}
private bool CanDelete
{
get { return true; // your condition }
}
4) "Then in each view, you can Add (through opening an extra dialog)"
"Each object has a relation with another one" - you need to let other ViewModels know of changes. Typical solution is to use Mediator pattern. Please refer to following articles:
http://sachabarber.net/?p=477
http://marlongrech.wordpress.com/2008/03/20/more-than-just-mvc-for-wpf/
http://blog.galasoft.ch/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx
Edit: just found another nice and simple MVVM sample featuring sorting filtering and list navigation:
http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/
This article about Catel includes a "Person application". It is very simple, but allows you to manage a list of contacts. Maybe it's a starting point for you.

Insert/save client search/form entries into separate table prior to running code- CakePHP

I want to store the form entries into a table that will store what my users searched for, then actually execute the code on the form results.
I tried doing:
$this->Search->create();
$this->Search->save($this->data);
But I don't think it liked me using a model that doesn't belong to that controller (my guess this doesn't follow convention and fully utilize CakePHP).
The other idea I had was to create a new searches_controller, then run $this->data through the above code and redirect back to the Trips controller but it seems like that's not the optimal way of doing it either.
Any thoughts on how best to do this?
In procedural PHP, I would just do an INSERT query before.
Thanks for the help!
I think that having Model which doesn't belong to the specific controller is not so bad idea.
Actually you can put this logic in the beforeFilter() of the Trips model like that:
class Trip extends AppModel {
//....
function beforeFind(){
if($this->data){
$search = ClassRegistry::init('Search'); //adding instance of Search model
$search->save($this->data); //save the data
//Do extra things if needed
}
}
//....
}
This way you don't have "alien" model in your controllers, and utilise the recommendation "Fat Models, Skinny controllers" :)

Using relationship/foreign key with linq2sql?

Can anyone help?
I have a created a relationship between my Reservation(prim key) and Insurance(for key) tables and imported into linq2sql and checked my automatically created c# files and sure enough i have reservation.MyFieldNames etc etc PLUS reservation.Insurance which is my relationship but reservation.Insurance i can't see the fieldnames of Insurance - am i missing something?
Do i have to call a GetInsurances or something? I don't see anything.. In fact Insurance is of type Linq.entityset???
Also i was hoping to create my reservation table (which was nice and easy) and then automatically "INJECT" a Insurance (i.e. 1 to many) from Ilist or something..
Is this not possible,
Any help really appreciated
p.s. I did create my automatically created c# dto files via T4 Toolbox but this shouldn't make any difference. I just have a separate file for each entity..
Not 100% sure what you mean by "I don't see anything".
Assuming you have a Reservations table and an Insurance table which references the "reservations" - what kind of relationship is this? 1:1 ? 1:many ? Which way around?? Can you post a screenshot of your DBML designer surface? (upload it to www.tinypic.com and include the link as an image here in your question)?
The "Reservations" class will contain one instance of an "Insurance" (EntityRef<Insurance>) or a list (EntitySet) of "Insurance" objects (EntitySet<Insurance>) - depending on the nature of the relationship - and you should be able to navigate those in code, e.g.
string foo = myReservation.Insurance.CustomerName; // or whatever
You won't see those on the designer surface - that's just a link to another class somehwere on your design surface, right?
Same goes for the other way around - if the Insurance is associated with exactly one Reservation, you should be able (in code) to do:
string bar = myInsuranceObject.Reservation.MyFieldName1; // or whatever
So it's not quite clear to me which case you're really referring to - can you elaborate on your question a bit more and make it clearer??
Based on the NerdDinner sample, I'll try to elaborate. Check out the DBML design surface:
Here, you don't see any of the properties used to move back and forth between the objects. But you do see that the "RSVP" class has a "DinnerID" foreign key which links it to the "Dinner" class. So this is a 1:n relationship: one Dinner has n RSVP's, and every RSVP is for exactly one Dinner.
In your code, you see these created properties now - check out the RSVP class first:
You can see that the RSVP class has a EntityRef<Dinner> - a reference (link) to exactly one Dinner - that dinner that this RSVP is for.
On the other hand, the Dinner class has a whole list of RSVPs of all the geek planning to attend that dinner!
So in the Dinner class, you have a EntitySet<RSVP> - a whole list of attendees, which you can then navigate when you're working with your Dinner class.
Does this make things a bit clearer?
Marc

Resources