Filtering results based on a distinct column or field value - sql

I have an MVC 3 application running against an MS SQL 2008 database with a table named Documents. Documents are broken down by paragraph in the database. The Documents table has a DocText column containing the text of each paragraph, a DocTitle column containing the document title. My MVC 3 app has a search function that can search for a word or phrase in the DocText column or in the DocTitle column. Everything works fine except that if a particular document has the search word appearing in multiple paragraphs, my List returns multiple instances of that document. For example, if the user searches the word "Budget" and one of THE documents has the word "budget" in four different paragraphs, my returned list has that document listed four times.
What I want to achieve is to list each document that has the searched word. I only want to list the document by Title once, regardless of the number of times the search word appears in that document. The only column that is truly unique is the RecordID column, a primary key.
My controller:
public class SearchController : Controller
{
private GUICEEntities4 db = new GUICEEntities4();
//
// GET: /Search/
public ActionResult Index(string Keyword)
{
#region Keyword Search
if (!String.IsNullOrEmpty(Keyword)) {
var SearchDoc = db.Documents.Where(r => r.DocText.ToUpper().Contains(Keyword.ToUpper()) || r.Title.ToUpper().Contains(Keyword.ToUpper()) || r.DocNum.ToUpper().Contains(Keyword.ToUpper()));
ViewBag.CurrentKeyword = String.IsNullOrEmpty(Keyword) ? "" : Keyword;
return View(SearchDoc.ToList());
}
else{
return View();
}
#endregion
}
}
My View has the following:
#foreach (var item in Model) {
<tr>
<td>
<strong>AFI #Html.DisplayFor(modelItem => item.DocNum): #Html.DisplayFor(modelItem => item.Title)</strong>
<br />
#Html.DisplayFor(modelItem => item.DocSummary)
<br />
<span class="complianceitems">Number of compliance items:</span> (TBD)
</td>
<td>
<a href="/Documents/Index/#(Html.DisplayFor(modelItem => item.DocNum))">Checklist
Generator</a><br />
<a href="/UploadedDocs/#Html.DisplayFor(modelItem => item.DocFileName)" target="_blank">
Download PDF</a>
</td>
Any suggestions on how I can achieve my goal?
ADDED: Each document can be identified by the DocNum column which has a unique document number for that particular document. I've tried to iterate through the List to pull out each unqiue DocNum and then try to make that DocNum not appear again in the loop...but I was not successful.
The following SQL statement gives me the results I need. The statement assumes that the search word is "budget". I don't know how to get the same results using EF. Any suggestions?
SELECT DISTINCT DocNum, Title FROM Documents
WHERE
DocText LIKE '%budget%'
OR
Documents.Title LIKE '%budget%'
OR
DocNum LIKE '%budget%'

The issue here is in your EF query and not anything related to MVC. It's been a while since I've actively used EF but the simplest way would probably be to return just the RecordIds first.
var recordIds= db.Documents
.Where(r =>
r.DocText.ToUpper().Contains(Keyword.ToUpper()) ||
r.Title.ToUpper().Contains(Keyword.ToUpper()) ||
r.DocNum.ToUpper().Contains(Keyword.ToUpper()))
.Select(d => d.RecordId)
.Distinct();
I'm not exactly sure what you will do with each individual record from then on as there isn't enough information in your question. But this should help.
Update:
var foundDocs = new List<YourType>();
recordIds.ToList().ForEach(r => foundDocs.Add(db.TblDocLists.Single(l => l.TheDocNum == r)));
//I must point out that I'm not familiar with the latest versions of EF so this might be overkill.

Related

Laravel 5.5, Display news data from two tables

I am new to laravel and I experience some trouble. I try to obtain data stored in two different tables and display them:
News.php (model)
public static function Data($category) {
$perPage = config('var.news.perPage');
if ($category) {
$news = News::orderBy('id', 'desc')->where('category', $category)->SimplePaginate($perPage);
} else {
$news = News::orderBy('id', 'desc')->SimplePaginate($perPage);
}
return $news;
}
This is how I grab all data from News table which struct is:
id, title, body, created_at updated_at, created_by, updated_by, category
The category column contains values separated by comma, e.g. 1,2,3,4
Now, I have another table, News_Cat which has id, name columns.
In another method I try to grab the filters names against values stored in category column of News table
public static function getFilterNames($id) {
$filters = DB::table('News_Cat')
->select('News_Cat.name as name')
->leftJoin('News', DB::raw('CAST(News_Cat.id as nvarchar)'), DB::raw('ANY(SELECT(News.category))'))
->where('News.id', $id)
->get();
return $filters;
}
However, it completely does not work. What I try to achieve is to display filter name in view.blade as 'name' value for specified filter from News_Cat
#if($news->count())
#foreach($news as $article)
<a href="{{ route('news.show', $article->id) }}" class="item angled-bg" data-filters="{{ $filters }}">
<div class="row">
So as result I would get e.g. data-filters="news, update, hot, latest"> instead data-filters="1,2,3,4">
Thank you
You should use eloquent!
In your News Model
public function getFiltersAttribute(){
$categories = explode(',', $this->category);
return implode(', ', NewsCat::find($categories)->pluck('name')->toArray());
}
then in your view :
{{ $article->filters }}
will output news, update, hot, latest
BUT
You should use a pivot table between your categories and your news, it would be much easier.
This method can't allow you to eager load the relationship and make a request for each news
If you can't change your database structure, I can propose you this:
In the boot method of your AppServiceProvider:
Config::set('tags', NewsCat::all());
THEN
public function getFiltersAttribute(){
$categories = explode(',' $this->category);
return implode(', ', config('tags')->whereIn('id', $categories)->pluck('name')->toArray());
}
MANY TO MANY METHOD
I am using laravel naming convention for the table :
news, categories_news (the pivot), and categories
You will have 2 models : New and Category
In your New Model
public function categories(){
return $this->belongsToMany(Category::class)
}
in your Category Model :
public function news(){
return $this->belongsToMany(New::class);
}
if you are not using laravel naming conventions, you will have to customize these raltionship like this : https://laravel.com/docs/5.5/eloquent-relationships#many-to-many

Query returns Object(Builder), undefined property

I have following code
public function detailCustomer(Customer $customer)
{
$vehicles = DB::table('vehicles')
->selectRaw('*')
->where('cust_id', '=', $customer->id);
return view('customers.detail', array('customer' => $customer, 'vehicles' => $vehicles));
}
Where table vehicles consists of:
spz
cust_id <FK> //this is foreign key to customer->id
type
brand
In the customers.detail view, I tried to use following code to show data, but I get this error:
Undefined property: Illuminate\Database\PostgresConnection::$spz
Code:
#if (count($vehicles) > 0)
<?php $i = 1; ?>
#foreach ($vehicles as $vehicle)
<?php $i++; ?>
<td>{{$vehicle->spz}}</td>
<td>{{$vehicle->type}}</td>
<td>{{$vehicle->brand}}</td>
#endforeach
#endif
I have read this topic but seems it's not my problem because I use foreach to iterate through the object but seems I do not get the object from database into my $vehicles variable, because in the error page, it shows also something like this:
'customer' => object(Customer), 'vehicles' => object(Builder)
What makes me think that customer gets its object correctly, but vehicles gets Builder?? Really no idea what is wrong there. Any ideas?
Just to describe what am I doing in the project that I work on, I have a customer detail page where I click a button to add a vehicle to his detail page (profile page) and I send customer id as parameter to the function where I add vehicle into database, which works (vehicle is added correctly with the customer id). Now that problem shows up when I want to show detail page with vehicle information like the code above shows.
Hope its clear enough. Thanks for suggestions.
Try to add ->get() or ->paginate($YOUR_LIMIT_ONE_PAGE) in your Controller
public function detailCustomer(Customer $customer)
{
$vehicles = DB::table('vehicles')
->selectRaw('*')
->where('cust_id', '=', $customer->id)->get();
// ->where('cust_id', '=', $customer->id)->paginate($YOUR_LIMIT_ONE_PAGE);
return view('customers.detail', array('customer' => $customer, 'vehicles' => $vehicles));
}
And try to replace your foreach to this forelse
#forelse ($vehicles as $index => $vehicle)
<tr>
<td>{{$index}}</td>
<td>{{($vehicle->spz !== null) ? $vehicle->spz : '-'}}</td>
<td>{{($vehicle->type !== null) ? $vehicle->type : '-'}}</td>
<td>{{($vehicle->brand !== null) ? $vehicle->brand : '-'}}</td>
</tr>
#empty
<td colspan='4'>Data not found</td>
#endforelse

Laravel 4 and Eloquent: retrieving all records and all related records

I have two classes: Artist and Instrument. Each Artist can play one or more Instruments. And each Instrument can be assigned to one or more Artists. So, I've set up the following Classes:
Artist.php
public function instruments() {
return $this->belongsToMany('App\Models\Instrument');
}
Instrument.php
public function artists() {
return $this->belongsToMany('\App\Models\Artist');
}
Then I have three database tables:
artists: id, firstname, lastname, (timestamps)
instruments: id, name
artist_instrument: id, artist_id, instrument_id
I'm able to successfully retrieve a one artist and their associated instruments like so:
ArtistController.php
$artist = Artist::find($artist_id);
$instruments = $artist->instruments()->get();
return \View::make('artists')->with('artists', $artists)->with('instruments', $instruments);
I have 3 questions:
In my view, I can output the $artist like:
{{ $artist->firstname }}
and I can iterate through $instruments like:
#foreach ($instruments as $instrument)
<h2>{{ $instrument->name }}</h2>
#endforeach
but is it possible to iterate over $artist (I know there's only one — see #2) and for each $artist iterate over their $instruments?
In my controller, how would I get all artists and for each of those their associated instruments with the end goal of iterating through them in my view as described in #1.
Is it possible to only retrieve specific columns in the above example of ArtistController.php? I've tried this:
$artist = Artist::where('id', $artist_id)->get('firstname');
$instruments = $artist->instruments()->get();
return \View::make('artists')->with('artists', $artists)->with('instruments', $instruments);
but I get an error saying Collection::instruments() is undefined.
I'm assuming there's something not right in my model relationships. I've also tried defining my relationship in Artist.php with a hasMany (I think it makes more sense to say "Each Artist hasMany Instruments", but that gives me an error because it's expecting a table named artists_instruments and it's also trying to retrieve columns that wouldn't exists in that table (like name).
Your model relationships are fine.
Controller:
$artists = Artist::with('instruments')->get();
return \View::make('artists')->withArtists($artists);
View:
#foreach ($artists as $artist)
<h1>{{ $artist->firstname }}</h1>
#foreach ($artist->instruments as $instrument)
<h2>{{ $instrument->name }}</h2>
#endforeach
#endforeach

WHERE clause in .cshtml MVC 4

I have 3 tables in my database:
Categories: ID(PK), CreationDate
Languages: ID(PK), Title
CategoryDetails: ID(PK), CategoryID(FK), LanguageID(FK), Title, Description
I've also created a ViewModel in MVC 4 with all the tables, but when I'm trying to list the Category Details in the view (.cshtml), the WHERE clause doesn't work as I was expected.
I'm trying to list the Language not by key, but by Title.
#foreach (var item in Model.lstCategoryDetails)
{
<tr>
<td>#item.Title</td>
<td>#Model.lstLanguages.Title.Where(x => x.ID == #item.LanguageID)</td>
<td>#item.Description</td>
</tr>
}
Enumerable.Where returns another IEnumerable, even if this contains just one found item. So replace .Where( with .FirstOrDefault( or .Single( etc.
You also don't need the additional # on in the lambda filter.
Once you've done this, access the Title property of the selected Language
e.g.
#Model.lstLanguages.FirstOrDefault(x => x.ID == item.LanguageID).Title
Note that you'll get a NRE if the language isn't found.
you should say
#Model.lstLanguages.Where(x => x.ID == #item.LanguageID).First().Title
you can apply 'where,select etc...' to list, and return type is Ienumerator.
when you apply 'First() or FirstOrDefault()' returns to you element type of your list.
for exm. you have List<int> intList = (1,2,3,4,5,6).
and applying:
intList.where(p=>p == 4 || p== 5).FirstOrDefault();
returns to you '4' as type of integer.

using listdata only and get from another model in Yii

can i just have listdata by itself? The list doesn't list anything, all I keep getting is "Array" as the return.
I have:
$awards= $model->findAll('uid='.$uid);
return CHtml::listData($awards, 'award_id', '$data->award->name');
try this code:
$awards= $model->findAll(array('condition'=>'uid ='.$uid)); // get records from table
return CHtml::listData($awards, 'award_id', 'award_name'); // third parameter should the array value. Most of the case we use to display in combo box option text.
If you are getting from another table, then declare a variable in your award model.
public $awrd_name;
Then you have to get records using relation in criteria.
Output will be :
array("1" => "award1", "2" => "award2", "3" => "award3");
refer this link : http://www.yiiframework.com/forum/index.php/topic/29837-show-two-table-fields-in-dropdownlist/