I have a Magento form with a button and some fields. The button click calls a controller and in the controller I query the DB (using fetchAll) and create a csv file from the results.
Whats the best place to store the DB action and the CSV file creation? In a model maybe?
Thanks!
I would create a model, a resource model and a resource model collection for the data you need to export. In the collection I would define functionality that queries data from one or many (if necessary) tables . In the model itself I would create a function that fetches that collection and saves it into a CSV file. The controller action would contain only the model initialization and a call to the export function.
Related
I want to retrieve data from the database and create the dynamic route.
For example, in Laravel web route file we write :
Route::get('/{slug}', function($slug){
return $slug;
});
So, I want to create this code after retrieving data from the database. and based on the data I can create the dynamic route with params or passing model data to the view.
And also how do I create dynamic view files. Like for different layout or from the database data.
Thanks in Advance.
My Question is not belonging to how to use two models on same razor view ! basically I have a user table in which i have fields like(userid,name,email,password,gender,country,department,IsActive) and my form is working fine i am able to insert update and delete i have not use EF , what i did i create the table in sql server and create the model in my model folder , in my view i have put the required field validator for all these columns and they are mandatory to input while inserting or updating.
Now I want to have another view with another controller where i do not want to show all 8 fields instead want to show just these four columns( username,email,gender,IsActive)
when i am using the same model for the other controller and view then it loads the record correctly on index view ,but when i update the required it fires validation error as all my fields are mark as required so it ask to input the rest of four fields value as well.
I have tried to remove these un-necessary fields from model in controller code before saving using Bind([Exclude]"""") but it did not work.
I have tried modelstate.remove("") this approach works fine for all fields but my password field is still throwing validation error . someone says you need to use viewmodel and in viewmodel you have to put both of your model like the full model and small model, I want to ask how my small model would be mapped to my user table (as table["tableName"] this cannot be applied to two models and pointing to same table without primary foriegn key relation .
Share example please i am confused
modelState.Remove("Password")
This remove all model values which are un-necessary but not removing the password field which gives error while updating the
You have required fields missing data or some fields are not null when being saved. What you are trying to do is completely ok. You are basically using a Virtual Model. This is what I do. In the "get", you fix your model and send it to the view. You work with it and then when you submit, you receive the model in the "post", but it is missing several REQUIRED fields. So what you need to do is, retrieve the same record from the database, update the 3 or 4 fields from the view model and then save that model that you retrieved with the new data. This is one way. (VM = view model)
1. send VM to view
2. add data in the view
3. submit
4. receive VM in POST Controller
5. get same record from DB
6. update the particular fields using the VM data
7. save the updated record to database. This is the record you retrieved in 5.
Another way is to have all the missing fields in the model but not showing them in the view. However you need to mention them in the view, otherwise they will not post. so you need to use
#Html.Hidefor( x=> x.NameOfField)
This will ensure that the field is sent back for posting. Any field with a [Required]... is required. You cannot miss it!
How will your small model be mapped to the Database. It will not. You will transfer the data from the small model to the model that is mapped in the database... with ALL the fields. Either you get the missing values from the database or from the view by using Hidefor.
example
dbmodel.Field1 = vm.field1;
dbmodel.Field2 = vm.field2;
dbmodel.Field3 = vm.field3;
I hope this helps
I have on my homepage a small search form with a list of categories.
I use a helper to query the database to get all categories. This is fine and works great.
Now I want to update the categories select with an updated list of categories based on a performed search.
That means, I would need to call query inside the helper with a given $variable to run a new query and to return new data.
But this is my concern now.
I can't call a helper from within the PagesController, right?
So what is the right way to
- have a helper call to use on the fly in all views selecting DB Data?
- address an ajax call to a controller and to use the query from the helper?
Any advice will be highly appreciated!
Thanks!
Short answer:
You code needs to be moved in a Table class and not in a Helper class, if you need it both in the Controller and the View.
Long Answer:
The code you have created in your helper needs to be moved in the relevant Table class and that will fetch the data.
Then in both your view and the controller you will use the new method you created in your Table class.
For example in your CategoriesTable class you can add a method called:
public function fetchCategoryData(){
//TODO: Custom code that will fetch the category data like...
//return $this->find();
}
public function updateCategoyData($args){
//TODO: Custom code to update your category data
}
Now, in either the View or the Controller you can use this
$Categories = TableRegistry::get('Categories');
$Categories->fetchCategoryData();
//or
$Categories->updateCategoryData();
I have an excel file file that some data need to be inserted into Table1 and some other data into Table2 or Table3.
I Already have my routes,models, controllers for each table.
As I have to do one function to insert data into all tables, I don't know if I need to do it inside a Model. Also I don't know how to do it, how designate the right data to the right table.
Edit: More specific
I have a file input and a button inside my view. When I select the excel file with the input and press the button I need to read the data AND insert that data into my database. The problem here is that this data will go to different tables. I would know solve this if all data would go to a single table, but I have no idea how to divide the data to separte tables.
Should I call a function from controller inside the view ? If so which Controller use ? Or maybe a function from model, but again which Model should I use ? Since each table has it's own Model.
Do I need to use JQuery to make an Ajax then direct it to a certain route so that route would call a Controller's function that would make the inserts?
I Dont think so because all data manipulation should be inside a View, right ?
I am working on a custom Joomla MVC component.
My view has a form where the user enters an ID. I have retrieved the ID ($input_id) in the controller. Now I need to query the database to get the name WHERE ID = $input_id, then write the name to a different database table.
Can this all be done within the controller or do I have to pass my variables to the model somehow? Not sure of the correct way to achieve this within the MVC framework.
All data and data manipulation should be done in the model (e.g. model your data). The controller is there to determine the path of execution and which methods should be called (e.g. the manager aka controller determines what needs to be done).
Have a look at this tutorial which will help you understand MVC for Joomla better by taking you through the development of a simple component.