Yii2: add field to autogenerated CRUD (automatically?) - php

Yii2 has a feature of generating CRUD for table with gii.
It creates several files: controller, model, search model and views.
What if after generating CRUD I need to add more fields to the table? I see that I need to change:
Model's
phpdoc
rules()
attributeLabels()
Search model's
rules()
search() (tweak ->andFilterWhere() calls)
Views:
index (grid columns)
_form (model inputs)
_search (search inputs)
view (attribute rows)
A lot of work. Is there a way to do it easier/automatically? I understand that I can just regenerate CRUD with gii, but in this case all my other tweaks would be overwritten (lost).
Maybe there exist some other CRUD solution for Yii2, that does not hardcode all fields and allows table schema changes to appear automatically in the views/models? Would be glad to know about it.

If you dont want to overwrite file or add changes manually , you have below option:
In Gii, after clicking preview button you can see list of files.
If file has changes or already in directory than diff button displayed.
On clicking diff button you can see changes in file.
From this model (window) you can modify file as your need or you can copy/paste code in/from temporary file and manage.

Simple way is regenerate model and crud after adding fields to table.
If you have modification in crud, you can generate in GUI files without saving and by copy paste move new column code.

Related

Add extra field on "File Upload" widget of OctoberCMS

I want to add an extra field to the Upload Files widget,
how I can do that?
What I actually have:
I changed DB (hardcoded on phpMyAdmin)
This is an updated _config_form.htm
And I have added this to FileUpload.php
Error on saving:
The problem was on the name of the new field...
So If change "data" to for example "params" all will work great:
You should not edit any core files directly, better way would be to create plugin, inside plugin create formwidget that extends from FileUpload.php give your widget an alias and use that alias instead of fileupload.
To add another field before $file->save() just override onSaveAttachmentConfig() method and that's it.
Problem with FileUpload is that it reads config from partial and not from config so it's hard to extend view that's pesented to users.
At least it was case with me, a had to add tags, taglist field and image crop inside multi image upload widget.
It was really complicated.

Yii controller setup with multiple conditions

In Yii crud, I've setup a Model, View, and Controller based on my db table called Form. I've modified the controller and views to my liking thus far:
index.php/form/all (index)
index.php/form/new (create)
index.php/form/2 (view)
index.php/form/2/edit (edit)
index.php/form/2/delete (delete)
Now I'd like to setup some subpages that will be dynamic. The url patterns are below. How do I set this up inside of the FormController.php?
index.php/form/2/fields/all
index.php/form/2/fields/new
index.php/form/2/fields/1/edit
index.php/form/2/fields/1/delete
BTW - Fields is a separate db table with a separate yii model. Though I'd like to not create a controller for it if I don't need to.
Thanks Neophile, I ended up adding a fields controller. Here is a link to my finished solution: Yii URLManager rule for multiple controller action parameters

CakePHP : Multiple views from One Controller

I am working in CakePHP for the 1st time. I need to create multiple views for a single controller.
Eg: I have a settings table.
Schema of settings table
1.ID
2.Name
3.Type
I have created its model and controller using cake bake. But i have multiple views from where the data goes into the settings table. My data of designations, departments, qualifications, projects and many other things go into the type field of the settings table with their names as entered.
So when i m creating the model and controller thru cake bake it is creating view as per the settings table, whereas i need view pages as per types, i.e Create Designation, Create Departments, Create Projects and also view, edit and delete files for them.
Pls help me find a way to achieve that..
I think you are looking for
$this->render('viewfilename');
create as many views as you want and based on your requirement send then in specific view from controller.
For example:
public function add($type) {
if ($this->request->is('post')) {
...
}
$this->set(............);
switch ($type) {
case 'designations':
$this->render('add_designations');
break;
case 'departments':
$this->render('add_departments');
break;
case 'qualifications':
$this->render('add_qualifications');
break;
}
}
and make view files as add_designations.ctp, add_departments.ctp, add_qualifications.ctp etc in view folder.
You can add Views by creating a .ctp file in the respective Views Folder (Views/"Modelname"/add_department.ctp)
In your "Modelname" Controller you just add
function addDepartment() {
// Logic here
}
But if you just want to set the type, you can create a normal add.ctp and create a Selectbox with all the different possible Types.
You need to read again how the pattern Model View Controller (MVC) works.
If you want to create a new department, you probably want to use the departmentsController associated with the Department model.
In each controller you will have the actions associated with it. This way Cake Bake will generate the add, delete and edit code for each of your controllers.
Of course, you can create them by your own in the controller you prefer making use of the model you wish. But don't expect Cake bake to work differently :)

Add new entities in OpenCart

I was wondering if I can add new entities (classes/tables) in OpenCart to store information that is not included in the default functionality. To be more precise, I would like to add subscription (3/6/12 months) related information, as described here: OpenCart subscription model (x months)
If yes, can I just add admin pages for the new class? Would something like: How to create a custom admin page in opencart? work?
You would need to create a
Template file (.tpl)
Controller file (.php)
Model file (.php)
Language file (.php)
All files are named the same with different extensions and located in their respective folders
You will also need to add more columns in the database named with the additional settings you ant to store.
In addition to your page specific setup you will also need to add a link to your page in the header file, in the menu.
You will use the model file to insert, update and delete data within the database. The controller controls what functions within the model are used.
Once created, In admin/ user group permissions you need to give access o the new pages
Take a look at the existing files to see how it works. Make a copy of one of the more simpler 4 files, rename, then start to modify them.
If you only want to add more settings to a existing page then you can add them to the existing 4 files and add columns in the database.
Hope that helps get you started, Good luck!

How should I go about capturing a custom form in Magento that is stored with the order?

I am working on a module to add a new step called Rental into the Onepage Checkout module in magento. The problem is not adding a step it is the data capture.
I want to be able to take the custom form I have created and store it with the rest of the order information so when I click view order in the backend, it shows the information in the form as a seperate box.
Preferably I want to seperate the concerns by creating a standalone module.
How would you go about doing this? What would be your personal strategy for implementing this?
Firstly, it should all depend on what data 'Rental' would be. Could it fit/be appropriately placed inside a product's custom option? Or do you need to create a custom sales attribute to store the data in?
If you want the latter, you should use Magento Module Creator by SilkSoftware to create a module to implement sales attributes.
Also: a good thread / scenario in using sales attributes: Magento: Setting a custom attribute on the sales/order_shipment model
Secondly, is capturing the form data -- this is assuming you already have a block/form created and showing on the checkout page. I highly encourage you name the fields in the following semantic:
<input type="text" name="rental[text_data]" />
<input type="text" name="rental[text_data2]" />
Where text_data and text_data2 are your desired variable names.
An order is submitted / saved finally via a Controller action called saveOrderAction in Mage_Checkout_OnepageController or whatever Controller class you are using currently for your checkout page. Don't edit the corresponding PHP Class file for this found at app/code/core/Mage/Checkout/controllers/OnepageController.php ! Since you want to separate this into another module, you should try and rewrite this controller then so that your custom Controller class inside your module that extends the aforementioned base class (Mage_Checkout_OnepageController) should be used by Magento in its stead.
A good tutorial on rewriting controllers can be found here.
You can then do either of the two options from here:
1.) you could copy the entire saveOrderAction function and create routines to save Rental data there. if you followed the naming semantic I suggested, you should be able to retrieve the form data doing so:
<?php
// app/code/local/Namespace/Module/controllers/Checkout.php
class Namespace_Module_Checkout extends Mage_Checkout_OnepageController {
public function saveOrderAction() {
.....
$this->getOnepage()->getQuote()->save();
$rental = $this->getRequest->getParam('rental');
$text_data = $rental['text_data'];
$text_data = $rental['text_data2'];
someSaveRoutine($text_data, $text_data2);
//..or someSaveRoutine($rental);
.....
}
}
2.) secondly, you could replace the above code with a custom event dispatch
$rental = $this->getRequest->getParam('rental');
Mage::dispatchEvent('checkout_onepage_controller_saveorder', array('rental'=> $rental));
then make an appropriate observer to save your custom data somewhere.
I know this isn't much, but it's a start. I will look into editing this to elaborate more in the near future.

Categories