dgtal
1
Hi, i would like to add a button in a view to create a new object with pre-filled foreign key field.
For example:
Customer view for customer "John Smith", id=2.
->link-> new Order
->Order with the customer already filled (id=2)
I know i could create another action in the controller for that, but i would like to use the Create Order action and view.
How can i achieve that?
Thanks in advance
danteODST
(Danteodst)
2
I need to do exact the same, if u find the best way to do it plz share what u did… 
AlexInt
(Alex)
3
I don’t know if I understood you well, but I think you’re looking for this:
public function actionCreate($id) {
$model = new Order;
$model->customer_id = $id;
}
You can do exactly the same with any action you want.
danteODST
(Danteodst)
4
Iam new to yii and php like 3 weeks, soo i dont undertands how that code exactly works, can you plz explain a bit… 
sorry for my bad english btw 
AlexInt
(Alex)
5
You have you actionCreate and you access to it like this yourwebsite.com/controllerID/create so if you add a parameter called id to that actionCreate method, you will have to access to the url like this yourwebsite.com/controllerID/create/5 or yourwebsite.com/controllerID/create/id/5.
And with this piece of code:
public function actionCreate($id) {
$model = new Order;
$model->customer_id = $id;
}
You fill customer_id with the parameter you passed to the url.
By the way, that kind of url will work only if you have configured your urls as path.
danteODST
(Danteodst)
6
Thanks a lot Alex its working… 
dgtal
8
I want to add something for anyone who might have the same question:
You can also let users create the object without choosing the foreign key like this:
public function actionCreate($id = NULL) {
$model = new Order;
$model->customer_id = $id;
}
This way if anyone will use the url yoursite/yourobject/create it will work and the field won’t be pre-filled.