I need some help. What I am trying to do is show the locations of a device belonging to a specific project.
On the projects page I have a button which goes to the google map. For projects I have a Projects model and controller.
I've got 2 tables, a table containing the location of a device and a project table. I want to display all device locations belonging to a project on a google map. The google maps part is working and I managed to show all device locations on 1 map, only problem is I can't seem to link the devices to the project. I need some help on how to achieve this.
In the Location table ive got a foreign key to the projects table. this is a many-to-onerelation. A project can have multiple devices, but a device can only be linked to 1 project. I have defined the relation as follows in the location model:
[BelongsTo("ProjectID")]
public Project project
{
get { return _project; }
set { _project = value; }
}
In the Location controller I use the following code(theres more but for the moment its not relevant):
public ActionResult Index()
{
return View(DeviceLocation.FindAll());
}
This simply lists all locations in the location table. What I need is to list all locations where the ProjectID = the ID of the projects table. I'm coming from PHP/MYSQL and there you would simply join the 2 tables. How do I achieve this with asp mvc?
You should read up on LINQ and how to use it to query your data.
Something like this should help:
public ActionResult Index()
{
return View(DeviceLocation.Where(d=> d.ProjectId == myProjectId));
}
Related
I have 3 custom objects with a Master-Detail Relationship and Lookup Relationship.
CustomA__c (related CustomB__c) <-> CustomB__c <-> CustomC_c (related CustomB_cc)
I´ve built a Visualforce page with HTML table to replicate a PDF document and a Custom Controller Extension, so far so good.
But I´m just a beginner with apex coding.
The problem is that I need to replicate the document as it is, when there are no related records for CustomA__c or less then 5, it should still show the full table (the empty rows). Max. rows/related records on the document is 5, no second page needed.
Currently I´m trying to accomplisch that by using apex:variable and apex:repeat as I´ve seen some examples, but perhaps there is also another solution. For the Visualforce page I already wrote the code for the rows with data and another apeax:repeat for the empty rows.
But I´m really strugling with the controller, i know i need to iterate over the list, the code that i already wrote is also put together out of examples as i just don´t understand it yet good enough.
Any help would be appreciated! Thanks, Josip
public with sharing class CustomAController {
public CustomA__c customa{get; set;}
public CustomA__c ca{get; set;}
public CustomAController (ApexPages.StandardController controller) {
ca = (CustomA__c )controller.getRecord();
customa= [SELECT Id, Name FROM CustomA__c WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];
}
public List<CustomB__c > getrelatedCustomB() {
List <CustomB__c > cbList = New List<CustomB__c >(5);
for(CustomA__c acc:[SELECT Id, Name, (SELECT Id, Name, ... , CustomCfield__r.Name FROM CustomBs__r ORDER BY Name LIMIT 5) FROM CustomA__c WHERE Id = :customa.Id]){
for(CustomB__c cb:acc.CustomBs__r)
cbList.add(cb);
}
return cbList;
}
}
You can dramatically simplify your code by writing a direct query on the child object instead of a parent-child SOQL query.
public List<CustomB__c > getrelatedCustomB() {
return [SELECT Id, Name, ... , CustomCfield__r.Name
FROM CustomB__c
WHERE CustomA__c = :customA.Id
ORDER BY Name
LIMIT 5];
}
There's no need to iterate in Apex here.
I'm am in the middle of creating my first, substantial .NET MVC application. I have come across a problem and I am not quite sure the proper way to go about it.
In my application I have quite a large database. For a number of features I need to select data from up to 5 tables and send it back to the view, and I am not quite sure how to go about it since view takes either Models or View Models?
I understand the concept of View Models quite well, but is creating one every time I need to send data from multiple tables the only solution to this? And if so could anyone tell me the best practices when doing it
Thanks in advance for any help
Yep, you'll have to have a View Models per view. I work on application with about 600 views and we tried re-cycling view models and it ended up in tears. Now there is a model for each view (mostly).
To send data from multiple tables you'll need to run joins on your tables and select into a view model.
Here I presume you use Entity Framework:
public class ComplexViewModel
{
public String Name { get; set; }
public String Category { get; set; }
public String Level { get; set; }
}
var db = new MyDbContext();
var result = from name in db.Names
join category in db.Categories on name.CategoryId equals category.CategoryId
join level in db.Levels on category.LevelId equals level.LevelId
select new ComplexViewModel()
{
Name = name.Name,
Category = category.CategoryName,
Level = level.LevelName,
};
return result.ToList();
More examples of joins can be found are recommended to review.
I'm creating an app with a backend in Laravel. The backend needs to manage a collection of objects which are downloaded to the app. The objects must be localised depending on the device language.
Is there a simple way to do this in Laravel? Perhaps an eloquent or Laravel-plugin? I'd like to avoid writing the localisation support myself.
(The built in localisation in Laravel is only for the interface, it doesn't support Eloquent objects)
You will need to write that on your own. First you will have to model your database tables to support multilanguage content and then in your model you will be able to say something like:
class Content extends Eloquent
{
public function scopeEnglish($query)
{
return $query->where('language', '=', 'en');
}
public function scopeSpanish($query)
{
return $query->where('language', '=', 'es');
}
}
class Post extends Eloquent
{
public function content()
{
return $this->hasMany('Content');
}
}
and then you can use it like:
$englishContent = Posts::find($id)->content->english()->get();
$spanishContent = Posts::find($id)->content->spanish()->get();
Glad To Help's answer seems perfect for multilingual site's with many languages.
I've found out that it's kind of clunky to do that when your site has just two/three languages.
What I've done is having two columns for each translatable fields.
for the title attribute I have in the database title_en and title_es.
After that, in the controller just set this method to do an automated translation.
public function getTitleAttribute()
{
$locale = App::getLocale();
$column = "title_" . $locale;
return $this->{$column};
}
Now when you call Post::find($id)->title it will automatically get the one for the current language.
Hope it helps.
Cheers!
I did similar but more universal
Schema::create('index', function(Blueprint $table) {
$table->increments('id');
$table->string('title_uk');
$table->string('title_ru');
$table->string('title_en');
$table->string('heading_uk');
$table->string('heading_ru');
$table->string('heading_en');
$table->string('photo');
$table->timestamps();
});
The model
public function TextTrans($text)
{
$locale=App::getLocale();
$column=$text.'_'.$locale;
return $this->{$column};
}
Now I for each language version as well as for each field will not prescribe a specific function, and cause all this:
$text=Index::find('1');
$text->TextTrans('title');
$text->TextTrans('heading');
There are some translation packages for Eloquent models and static language resources. You can combine them, it's up to your scenario.
These packages might be useful when you just want to translate your resources or outsource translation to 3rd parties (like customer or content creator) via extranet (kinda front-end), so these are storing your translation files in database:
https://github.com/barryvdh/laravel-translation-manager
https://github.com/joedixon/laravel-translation
In order to make your Eloquent model multilanguage, store it as JSON array. If you are creating a sort of CMS like application, you will need multilingual title or content body. Following packages might help to achive this:
https://github.com/Astrotomic/laravel-translatable
https://github.com/spatie/laravel-translatable
Following the #user1067281 answer I found a great advanced tutorial for multi languages site.
You should totally check this out: https://medium.com/laravel-4/26cdc75e4810
Following grails domain class:
class MyClass {
Map myMap
}
Now for myMap, grails automatically creates a new table for the elements in the map. However if I add elements which are too long (e.g. 1024 characters), I get a DB error.
Can I somehow tell grails to make the respective column in myMap's table big enough to allow for larger Strings, or do I have to do this manually in the DB?
I already tried
static constraints = {
myMap(maxSize:1024)
}
which doesn't work (as expected because maxSize should refer to the Map's values and not to the Map itself).
If not via constraints, maybe there's a way to do it via
static mapping { ... }
?
An alternative approach I used successfully was to push the map out into a collection of a collaborator domain class.
class DynaProperty {
String name
String value
static belongsTo = MyClass
static constraints = {
value(maxSize:4000) //Or whatever number is appropriate
}
}
And then in MyClass:
class MyClass {
static hasMany = [dynaProperties:DynaProperty]
}
This is almost a map, and it gives you the ability to use dynamic finders to pull up an individual entry.
what are you trying to accomplish? Is there always the same number of things in the map? If there is you should define those properties on your class.
You can see the problem with your current approach -- there is no way to figure out what might be in the map until runtime, so how can grails possibly create a columns for it? Im surprised it even worked to begin with...
Today I've got a problem when I tried using following code to alter the model attribute in the controller
function userlist($trigger = 1)
{
if($trigger == 1)
{
$this->User->useTable = 'betausers'; //'betausers' is completely the same structure as table 'users'
}
$users = $this->User->find('all');
debug($users);
}
And the model file is
class User extends AppModel
{
var $name = "User";
//var $useTable = 'betausers';
function beforeFind() //only for debug
{
debug($this->useTable);
}
}
The debug message in the model showed the userTable attribute had been changed to betausers.And It was supposed to show all records in table betausers.However,I still got the data in the users,which quite confused me.And I hope someone can show me some directions to solve this problem.
Regards
Model::useTable is only consulted during model instantiation (see the API documentation for Model::__construct). If you want to change the model's table on the fly, you should use Model::setSource:
if ( $trigger == 1 ) {
$this->User->setSource('betausers');
}
The table to use is "fixed" when the model is loaded/instantiated. At that time a DB connection object is created, the table schema is being checked and a lot of other things happen. You can change that variable later all you want, Cake is not looking at it anymore after that point.
One model should be associated with one table, and that association shouldn't change during runtime. You'll need to make another model BetaUser and dynamically change the model you're using. Or rethink your database schema, a simple flag to distinguish beta users from regular users within the users table may be better than a whole new table.