Background
I was given the task of writing a Small Business online database. This database is to include a lot of info as well as info on their directors and branches. Since any business can have an unlimited amount of directors and branches, I need to create a database that is not limited to just one director and/or branch.
What do I have
Currently I have 3 tables.
smmes [id, company_name, trading_name, business_address, registration_number, tax_reference, vat_number, bbbee_status, employees, awards, created, modified]
ownerships [id, smme_id, name, surname, gender, age, race, disability, qualification, created, modified]
branches [id, smme_id, location, contact_number, contact_person, created, modified]
Note: smme_id is the id of the company in smmes that the branch or director belongs to.
And I have a view for the SMME's.
What is my question
I'm VERY new to cakePHP (in fact, this is my first app I'm creating with cakePHP). I want to know how I can make one form where a user can enter all this detail and then add the details for all directors and branches from one view. I would prefer that they do not have various views to go through to create all the details. Add to that, this one view should then save all the data to the correct tables with the correct smme_id.
Is this possible or should I rather leave cakePHP and write it manually.
You can load model on demand in your controller and then pass model specific data(received from posted form) to loaded model's save method.
public function detail(){
if($this->request->is('post')): // update only when form is posted
$this->loadModel('ownerships');
$owner_name= $this->request->data['Ownername'];
$ownerships_data = array('Ownership' = > array(
'name' = > $owner_name
//add other keys from posted form
)
);
$this->Ownership->saveAll($ownerships_data);
// load other models for saving posted data in related tables
endif;
}
Similarly load other models and pass fields from posted form as array to it's save method.
Suppose URL format is http://example.com/director/detail.So you would like to put above method(termed as action in MVC terminology) in app/controllers/directors_controller.php
Generally if URL format is http://somesite.com/abc/xyz it will look for xyz action in
app/controllers/abcs_controller.php
You can read more about cake conventions here
Related
Is it possible to reference additional columns apart from the 'Code' and 'Name' columns when using a domain attribute in an entity?
E.g. A person entity has a code of '1' and a name of 'Smith' and a Gender of 'Male'
In a customer entity there is a domain value referencing the person entity which displays the following 1 {Smith}. The users would like an additional read only attribute which would copy the Gender value of 'Male' into the customer entity based on the domain value. Can this be done using out of the box MDS UI?
I know this is duplicate data and breaks normal form but for usability this would be useful. It would be the equivalent of referencing additional columns in an MS Access drop down list.
Many thanks in advance for any help
This is not possible with the standard UI. One option would be to develop a custom UI where you can handle these kind of requests.
If you want to stick with the standard product I can see a workaround but this is a bit of a "dirty" one.
You can misuse (abuse) the Name attribute of the Person entity by adding a business rule to the Person entity that generates the content of the Name attribute as a concatenation of multiple attributes. You of course need an additional attribute that serves as a place holder for the original Name. The concatenated field will then show in your customer entity.
One question that does come to mind is why a user would like/need to see the gender of a person in a customer list? As you have a separate Person entity I expect you to have multiple persons per customers. What would the gender of one person - even if it is the main contact - matter?
I'm trying to create a simple 2 table database - table 1 holds ClientInfo and table 2 has ClientVisits - Relationship is on ClientInfo.ID->ClientVisits.ClientID. Then I have a form created thus for viewing the ClientInfo plus a child(sub?)table which SHOULD show all the records from ClientVisits where my Form ClientID = ClientVisits.ClientID.
Here is my form
Here is the child table with fields shown
Relationships
So I already have one record in ClientVisits for the currently chosen ClientID form record. But it doesn't show in my Table.ClientVisits. Other than the relationship I don't have any other link between the ClientID and the ClientVisits.ClientID field.
If I need to post further info please let me know, trying to describe this as well as I can - sorry if it's not making sense. Thanks.
You have to link both tables in your form.
In my example, main data of my form is a table called CLIENTES, where it shows all the information about a cliente. It would be exactly the same as your table ClientDetails. In this table, primary key is a field called DNI (it would be the equivalent of your ID field)
I got a second table called CONSULTAS MÉDICAS. This table is just a list of how many times this client comes to see us. It would be the same as your secondary table CLIENT VISITS. In this table, I got a field called PACIENTE, linked to my table CLIENTES. Let me show you.
Ok, now my form is done based on the data of my table CLIENTES, but I got a subform control, where I have linked the table CONSULTAS MÉDICAS
To make this work is pretty easy. Not filters or queries. Just linked child and master fields. To do this, you have to select properties of your subform control, and then go to DATA TAB
Just choose as main field your ID field from table CLIENT DETAILS and link it to child field CLIENT ID from table CLIENT VISITS
That should work for you.
Dear helpers and saviors !
I'm trying to create some dynamic forms with Silex.
My objective is to have the user create a state in a existing country. I would like him/her to select a region (like Asia, Europe, America...), then select a country to which link the newly created state.
I have some "classic" and simple forms extending AbstractType, in which the user can select the country directly. I created a choice list based on a call to a DAO (database) to find all available countries. I would like to add a region choice_list field to reduce the number of available country (and reduce the database charge as well even if it isn't my main goal).
I've seen http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data, but it seem that we can't create subform for elements that are "parents" of the main class (or that i did not understood how), saying that the link is not stored in my object i would like to create. What i would like to do here is to add a field for the 2-level above (state->country->region), not direcly linked (a state have a country, and a country have a region) region.
I do not manage to imagine or code it properly, or even simply displaying.
Please note that i'll have to do it on several levels (state, city, and some other classes), and i would like to find a quite generic way to do it (understand not a ready solution for this particular example).
Any ideas ?
Please excuse my english, and thank you very much for your help !
For reference, here are my DB links between objects :
Region : ID and Name
Country : Codes, ID, Name and link to Region's ID
State : Codes, ID, Name and link to Country's ID
City : Name, ID and link to State's ID
I managed to do what i wanted.
I created supplementary forms, asking for filling each of the "parent" objects.
So in addition to my Types forms, i now have a selection a Select forms, allowing me to select Region, then Country, then State.
I have a two aggregate roots in my domain, and therefore two repositories. We'll call them BookRepository, and AuthorRepository, for the sake of example.
I'm designing an MVC application, and one page has to display a table containing a list of authors, with each row showing the author's personal details. At the end of each row is a small button that can be clicked to expand the row and show a child table detailing the author's published books.
When the page loads, some ajax is executed to retrieve the Author details from an API controller and display the data in the table. Each property in an Author object maps almost directly to a column, with one exception, and this is where I'm having my problem. I want the button at the end of each row to be disabled, if and only if the author has no published books. This means that a boolean has to returned with each Author record, indicating if they have any published books.
My book repository has a couple of methods like this:
public IEnumerable<Book> GetBooksForAuthor(int authorId);
public bool AnyBooksForAuthor(int authorId);
and my Book class has a property called AuthorId, so I can retrieve a book's author by calling
authorRepository.GetById(book.AuthorId);
My problem is that in order to create a row for my aforementioned table, I need to create it like this:
IEnumerable<Author> authors = authorRepository.GetAll();
foreach (Author author in authors)
{
yield return new AuthorTableRow
{
Name = author.Name,
Age = author.Age,
Location = author.PlaceOfResidence.Name,
HasBooks = this.bookRepository.AnyBooksForAuthor(author.Id)
};
}
The above code seems correct, but there's a fairly heft performance penalty in calling this.bookRepository.AnyBooksForAuthor(author.Id) for every single author, because it performs a database call each time.
Ideally, I suppose I would want an AuthorTableRowRepository which could perform something like the following:
public IEnumerable<AuthorTableRow> GetAll()
{
return from a in this.dbContext.Authors
select new AuthorTableRow
{
Name = a.Name,
Age = a.Age,
Location a.PlaceOfResidence.Name
HasBooks = a.Books.Any()
});
}
I'm hesitant to put this in place for these reasons :
AuthorTableRowRepository is a repository of AuthorTableRows, but AuthorTable row is not a domain object, nor an aggregate root, and therefore should not have its own repository.
As Author and Book are both aggregate roots, I removed the "Books" property from the Author entity, because I wanted the only way to retrieve books to be via the BookRepository. This makes HasBooks = a.Books.Any() impossible. I am unsure whether I am imposing my own misguided best practice here though. It seems wrong to obtain Books by obtaining an Author via the AuthorRepository and then going through its Books property, and vice versa in obtaining an Author via a property on a Book object. Crossing aggregate root boundaries would be the way I'd term it, I suppose?
How would other people solve this? Are my concerns unfounded? I am mostly concerned about the (what should be a) performance hit in the first method, but I want to adhere to best practice with the Repository pattern and DDD.
I would stick to the first approach, but try to optimize things in the bookrepository method. For instance, you can load this information all in one time, and use in-memory lookup to speed this up. Like this you would need 2 queries, and not 1 for each author.
The way I solved this in the end was to create an Entity from a view in the database. I named the entity 'AuthorSummary', and made an AuthorSummaryRepository that didn't contain any Add() methods, just retrieval methods.
I am using Breeze (http://www.breezejs.com/) and to use the functionality I want it requires mapping to a complete entity and all of its fields. I have a "Person" entity, but it includes a Social Security Number field. I want to keep this SSN# field private so I would like to create an entity named SubSetPerson that is updateable, has navigation properties and only contains the columns I want (e.g. ID, FirstName, LastName, myNavigationProperty) and does not contain the SSN#. I am using database/model first. Is this possible?
If you are using database first, then you could create a view for that table which only selects the columns you want. Then update the EF model browser to include that view.
Try using a Master-Detail type structure for your person. The master table would contain the person's public information; ie name, birthdate, etc... The detail table would contain only the more sensitive information (SSN, etc...). Then depending on your needs you can load the detail or not.