I have just started learning CakePHP and Need help in this situation.
I have one table(Student) and corresponding model (Student) which has one property called 'Educational Qualification' which can have multiple values (MBA, MCA, BCA, BA, PHD) so i have created a master table which contains list of all the degrees.
Now When a Student is going to register himself/herself he has to choose multiple options in qualifications.
I want to get DegreeMst data from database in my View as multiple options.
My question is that in this manytomany relationship Do i have to create a new model DegreeMst and then create ManytoMany relationship with Student model .
Or Can i do this without creating new Model DegreeMst
First and foremost, if you find yourself needing to fetch data from a table without creating a model in an MVC framework, there's a good chance you're doing something wrong.
If you've really only got 5 possible values (MBA, MCA, BCA, BA, PHD), I'd probably just make them 5 separate tinyint(1) fields in your table, like has_mba, has_mca, etc. Then just show each in your view as a separate checkbox. That's probably the simplest way.
If you don't want to do that, and you definitely want a separate DegreeMst table, then what you'll want is a hasAndBelongsToMany relationship.
You'll need a separate join table, something like students_degreemsts, with a student_id and a degreemst_id columns. You don't need to create a Model for the joins table.
Once it's set up, the code in your view to output the checkboxes will be something like:
echo $this->Form->input('Student.DegreeMst',array('label'=>'Select your degrees','multiple'=>'true'));
UPDATE
You'll need 3 database tables, and 2 models. You know about Many-to-Many (ie, what Cake calls hasAndBelongsToMany) relationships and joins tables?
In your database, you'll need 3 tables, named according to CakePHP convetions:
students (id, name, address, other_field)
degrees (id, name, another_field)
students_degrees (id, student_id, degree_id)
That last table is your joining table, and it should contain those 2 foreign keys to the students and degrees table. In CakePHP, you don't need a Model or Controller for the joining table.
You obviously do need a model for your Students table, which is where you'll define your hasAndBelongsToMany relationship:
public $hasAndBelongsToMany = array(
'Degree' => array(
'className' => 'Degree',
'joinTable' => 'students_degrees',
'foreignKey' => 'student_id',
'associationForeignKey' => 'degree_id',
),
);
You also need a Model for your degrees table, but you don't neccessarily have to define a hasAndBelongsToMany relationship again in that model, unless you need it.
Related
Suppose, you want to create a rest api method for different car brands (BMW, AUDI, MERCEDES).
You also want to create a new car, including the car brand as property.
How can I validate if the car brand exist, before creating a new car in the table?
From the front-end, you want a dropdown list (select / option).
From the backend you want to validate the input.
What is the best practice for this?
The get car brands method will read the dynamodb table too often, creating hot partitions.
The API call to the DynamoDB table when attempting to create a new car brand would be to use a Conditional Put. That way you would say "only insert this new car brand if it does not exist already".
For the read of list of car brands, if this is something in your UI that is presented thousands of times per second, then i'd have an out of band lambda function that updates the list only when a new car brand is input (use DynamoDB Streams). I would put that list X number of items all with the same data, but different PKs. For example: pk = "CarBrandList_0" all the way through pk = "CarBrandList_5". In your code to get the car brand list, you give it the PK and a random number between 0 and 5. You have now avoided creating hot keys
I need to generate a report that groups by two fields the records of a table in an Axapta 3.0. The source table I have has this structure:
And the report I want to get looks like this:
What sections and properties should the report have in order to create such a design?
Thank you very much.
If you have backing tables for the fields 'LOTE' and 'DIARIO' then the easy option is to use them, the query should look like this, all inner joined:
LoteTable
DiarioTable
YourTable
Remember to set Relations to Yes on the two first tables (or define the relations your self).
If you do not have backing tables, you can create them as views on your table.
The LoteTable view should contain the following fields:
Lote
Proveedor
CountOfRecId (Aggregation Count)
The CountOfRecId field will make the view a group by on the first two fields.
I am struggling in the creation of a report including four tables.
Those tables should be the same for each account. I use just one dataset. When I run the report, the data is good, but tables listing appears wrong.
First all BALANCE DETAIL tables for all accounts selected are listed, then all DEBIT TRANSACTION tables for all accounts selected are listed etc.
For each account the page should show up just like this
Please give me an easy-to-understand answer since I am really a beginner in this sector.
It might be easier to this with 5 separate datasets, but I think you can do it with just on also.
Create on 'main' table on the dataset. use 4 detail rows, 1 column
Group it on accountnumber
In each detail of the main table, insert a new table with the same dataset.
Group those sub-tables also on accountnumber
Add a filter to the subtables. set the subtable accountnumber equal to the outer table account number (you can use the expression builder, but it should read something like this: row['ponum'] equals row._outer["ponum"] )
Good luck!
I am new to Asp.Net, MVC3, Linq, and everything else related to it. I'm very used to PHP and ColdFusion so pardon my ignorance! Essentially what I am trying to do is re-create the situation in ColdFusion where I can perform the equivalent of a cfoutput's group attribute. I'm so used to just calling a stored procedure and then doing
<cfoutput group="id">
and then another inner cfoutput for any columns that have non-distinct data. Works like a charm! Not so much in Asp.Net.
I would like to stay with using my stored procedure, which is returning a join from two tables with a one-to-many relationship. For example's sake let's say I have 3 columns: a full name, a title, and a graduation year. The graduation year is the column from the joined table, so my result from the stored procedure looks like this:
Jim Professor 2005
Jim Professor 2008
Jim Professor 2011
I am sending this to the View. I am assuming it's the View's job to then group the data based on one of the columns (probably the full name). I want to output an HTML table with 3 columns and in this situation I would have ONE row:
Jim Professor 2005, 2008, 2011
I have googled tons of examples that use this thing called a group key. This does not seem to help me because I'm not interested in just outputting one value "Jim" (or whatever the grouped value is), I need both "Jim" and "Professor" values to be output for each row. My thinking is I would need 2 foreach loops, the outer loop displaying the fullname and title and the inner loop going through all possible matches for the graduation years. I cannot seem to get the graduation years in a group, especially with this IGrouping syntax. The key can only store one value and I need every value on that row, I only really need one or two values to be iterated over. Should I try and create a custom view model after I perform a secondary linq grouping and then send that to a strongly typed view?
EDIT:
Ok, I have code that works but it seems very inefficient as I basically have to re-define all of the columns/values that I have from my stored procedure. It almost makes me want to forget stored procedures and just use LINQ for everything. It seems what I was asking for is a kind of "group on multiple columns" and link helped immensely.
var records = db.getRecords();
var groups = from r in records
group r by new
{
r.id
}
into row
select new ListVM()
{
id = row.Key.id,
fullname = row.Select(x => x.fullname).First(),
title = row.Select(x => x.title).First(),
degrees = row.Select(x => x.degree_name).ToList(),
majors = row.Select(x => x.major_name).ToList()
};
return View(groups);
I of course had to create a ViewModel for this to work. In my view then I can use for loops to iterate over the degrees and majors lists. Is this the best way to do this? I just generally need more than just the group key to display my entire row of information, and only want to iterate over lists once or twice in a 20 column row, as opposed to only displaying the group key once and iterating over everything in most examples I see.
I'm not that big specialist with Linq and MVC, but faced with your problem I would:
Deal with data preparation in controller/model, after being taught that view should be concerned with displaying things only.
I would use knowledge from these topics to solve your particular problem:
a) grouping by multiple columns:
Group By Multiple Columns
b) Concatenation as an aggregate function:
Using LINQ to concatenate strings
c) Using aggregates and grouping by multiple columns
How to write a LINQ query combining group by and aggregates?
Once you have data in your view model, just display it.
I believe I've finally found out how to solve what I was looking for. A "group join" seems to solve my problem with ease. The information I found on this page solved it: http://geekswithblogs.net/WillSmith/archive/2008/05/28/linq-joins-and-groupings.aspx
i'm using Drupal 6
I have this table relation and I've translated into CCK complete with it's relation.
Basically when I view a Period node, I have tabs to display ALL Faculty nodes combined with Presence Number.
here's the table diagram: http://i.stack.imgur.com/7Y5cU.png
Translated into CCK like these:
CCK Faculty (name),
CCK Period (desc,from,to) and
CCK Presence(node-reference-faculty, node-reference-period, presence_number)
Here's my simple manual SQL query that achieve this result: http://i.stack.imgur.com/oysd3.png
SELECT faculty.name, presence.presence_number FROM Faculty AS faculty
LEFT JOIN (SELECT * FROM Presence WHERE Period_id=1) AS presence ON faculty.id=presence.Faculty_id
The value of 1 for Period_id will be given by the Period Node ID from the url argument.
Now the hardest part, is simulating simple SQL query above into Views. How can I make such query into Views in Drupal-6 or Drupal-7 ?
thanks for any helps.
The main issue, which I think you've noticed, is that if you treat Faculty as the base for your join, then there is no way to join on the Presence nodes. Oppositely, if you treat Presence as the base, then you will not see faculties that have no presence number.
There is no easy way, using your currently defined structure, to do these joins in views.
I would say your easiest option is to remove the 'node-reference-faculty' field from the presence node and add a node-reference-presence field to the faculty. Since CCK fields can have multiple values, you can still have your one-to-many relationship properly.
The one downside of this is that then you need to manage the presence-faculty relationship from the faculty nodes instead of the presence nodes. If that's a show stopper, which it could be depending on your workflow, you could have BOTH node-reference fields, and use a module like http://drupal.org/project/backreference to keep them in sync.
Once you have your reference from faculty -> presence, you will need to add a relationship in Views. Just like adding a field or a filter, open the list of relationships and find the one for your node-reference field.
Next, you will need to add an argument for period id and set it up to use a node id from the url. The key thing is that when you add the argument, it will ask which relationship to use in its options. You will want to tell it to use your newly added presence relationship.
You don't really need to do a subquery in your SQL like that. This should be the same thing and won't make mysql try to create a temporary table. I mention it because you can't really do subqueries in Views unless you are writing a very custom Views handler, but in this case you don't really need the subquery anyway.
Ex.
SELECT f.name, p.presence_number
FROM Faculty AS f
LEFT JOIN Presence AS p ON f.id=p.Faculty_id
WHERE p.Period_id=1;
I wrote an article about how to achieve a similar outcome here. http://scottanderson.com.au/#joining-a-views-query-to-a-derived-table-or-subquery
Basically how to alter a Views query to left join on a sub-query.