I want to define a class which returns vlaues for 3 dropdown.
Like my class should return 3 list which are dependent.
Like 1st dropdown is a field dropdown second is operator dropdown(=,#,>,<) 3rd is the corresponding value for the first dropdown.
How do i define a model
Related
When using an ActiveRecord to retrieve models it's possible to select a few columns. For example:
Product::find()->select('product_id,name')->all();
There are more columns than product_id and name in the database, but this example returns an array filled with all models, having only the product_id and name attributes filled. This is expected of course because I've passed them into select().
Now when looping through the found models and calling $model->getAttributes() it returns an array of all model attributes and their values (null for the not-selected attributes), including all columns that were not selected in the query but are in the model's attributes() function.
Is there a way to call a function similar to getAttributes() but returning only the populated attributes that were selected in the query: product_id and name?
I know it's possible to pass in exclusions to getAttributes() but I'd rather have the populated attributes returned based on the values I've selected in the ActiveRecord query.
In your case you should be able to use fields() method:
$attributes = $model->getAttributes($model->fields());
Note that fields() does not guarantee this behavior if you change model after find - unsetting (unset($model->product_id)) or setting ($model->some_field = 'some value') attributes will affect result of fields().
I have two models Item and ItemDetail and they have a one-to-many relationship
One-to-many because I store the item details as Entity–attribute–value Table, it is nothing but a table the columns and label and value that stores ItemDetails as following
id|item_id_fkey|Label |Value
1 |1 |Color |Black
2 |1 |Description |Item 1 Details
3 |1 |Size |2x4x6
4 |2 |Description |Item 2 Details
5 |2 |Weight |1000
When I create an Item it ask me Item->name and Item->Price, on Submit it calls the ItemsController#store and redirect to ItemDetailController#create with newly created item id.
ItemDetailsController#create redirects to a Form with lot more fields to for gathering item details (e.g. ItemDetail->Description, ItemDetail->Dimensions etc), and on submit it stores the ItemDetails model.
Problem:(Not as complex as the situation), The issue is I want it to be such that if user creates item in first form and does not update the item details in second form, item shouldn't present in Item table as well.
In another words two store methods should be one single transaction.
Solution 1:
You can put everything in one form (may require creating dynamic HTML elements) and make your controller's store() method check if there's a non-empty array of values for the ItemDetail model. If there are values, create the Item model and the ItemDetail model(s), abort otherwise.
Solution 2:
You can have your ItemDetailsController#store method accept an $itemId which represents the newly created Item model and, as above, check if there's a non-empty array of values for the ItemDetail model(s) then create or abort.
I am assuming the two forms you are referring to are on separate pages and therefore are two separate requests to the server.
You could save the information in the first form in an object and pass it to the session.
Controller for First Form:
public function handleFirstForm() {
//perform validation and any other logic
$input = Input::get('contentsOfFirstForm');
Session::put('contentsOfFirstForm', $input);
}
The array you put into the session will only be used if the customer proceeds to fill out the item details.
Controller for Second Form:
public function handleSecondForm() {
$input = Input::get('contentsOfSecondForm');
//only save if the second form is not blank
if($input!==null) {
//run any other logic
//Save First Form
$item = Session::get('contentsOfFirstForm');
Item::create([$item]);
//Save Second Form
$itemDetails = $input;
ItemDetail::create([$item]);
}
}
You may also want to do a check using model events to see if the first form successfully saved, and ONLY then, save the second form, to prevent cases where your second model successfully saved, but the first model for whatever reason could not be saved.
How to get attribute value of multiple selection in the Owebia shipping method?
My Owebia code below:
{count items where array_match_any(product.attribute.**shipping_restriction**.**value**', array('AU','NZ','AU,NZ'))}
But,I can't get the attribute value,I only can get the id num of attribute,
shipping_restriction is my custom attribute,which has a multiple selected menu.
It just return the index num,not 'NZ' or 'AU',it just return the index num,not 'NZ' or 'AU'
If I have changed the multiple selected menu into single selection,it could get right value. Like 'NZ' or 'AU',and not index num.
You can try by using following syntax to get value.
product.attribute.*.value
Where * is attribute placeholder.
Reference: http://www.owebia.com/os2/en/doc#formulas_variables_product
I am using Asp.net mvc3
In my viewmodel I have taken an IEnumerable attributelike:
public IEnumerable<EmployeeDetailsViewModel> EmployeeList{get;set;}
In my controller this EmployeeList is having a list of 2*2 matrix. i.e. I have passed an array to this list.
My array name is :employee
attributes:
First Name
Last Name
Task
after result it is displaying employee list in EmployeeList like EmployeeList[0]: values of First Name, Last Name,Task
now I want to store the list values in table in database.
My table also contains similar fields like my array attributes
I am using sql Server 2008 as backend
I want to do this in my controller.
how can I do this?
do I need to use foreach loop to assign list values to attributes of table or need to do something else?
please suggest me something...
I myself resolved the problem:
can use foreach loop here like:
foreach (ModelName item in viewModel.EmployeeList)
{
TableName a = new TableName();
a.Project1up = item.FirstName;
a.Employee1up = item.LastName;
a.Allocation = item.Task;
}
In MVC3 I have this code at my controller. It retrieves a List of IDs\Names from Installation table and creates a ViewBag
var vs = dba.Installation.OrderBy(q => q.InstName).ToList();
ViewBag.Vessels = new SelectList(vs, "InstId", "InstName");
Now, at my view. I want to render the list in a dropdown list. I used the Html helper that works fine...
#Html.DropDownList("InstId",(SelectList)ViewBag.Vessels, "- Select one -")
I need to set first item in the ViewBag List as a default selected value, instead of "- Select one -" text.
How can I do it?
Thanks in advance!
There's an overload for the SelectList constructor that takes 4 arguments. The last of which is the default selected object. E.g:
ViewBag.Vessels = new SelectList(vs, "InstId", "InstName", selectedValue);
Where selectedValue is an object of whatever type is in your list.
I need to set first item in the ViewBag List as a default selected
value, instead of "- Select one -" text.
Then you need to select the first item in your list (vs) and get it's id and use it as the selecedValue in the SelectList:
ViewBag.Vessels = new SelectList(vs, "InstId", "InstName",
vs.FirstOrDefault().InstId);
You can also create an Helper class for Dropdownlist
It will have a method to set the text as by default text for every dropdownlist in you solution