How get an parent id in HasMany field Laravel Nova - laravel-nova

i have Category resource which have a nested Items resources via HasMany field
public function fields(Request $request)
{
return [
new Panel('Items', [
HasMany::make('Items', 'items')
]),
];
}
I need to perform specific validation with Items: the Category have integer field max_default_items so when creating a new Item inside the category i need to check, does max_default_items is greater than the actual count of items with is_default flag. The problem is that i have no idea how to get the Category id inside Item
Is it possible to pass params to HasMany field and get it inside Items resource?
Please help!

Related

Replace List in Model vs. Add/Remove Items from List in Model

class Cart {
static hasMany = [list: Fruit]
}
Say we made a domain in Grails with a hasMany relationship with another model. So in our service, if we want to update the list for an object, is it better to replace the entire list with a new list, or is it better to individually add/remove the items from the list?

Grails domain List field not being persisted

I have a domain class
class Something {
String name
String email
List<String> friends = []
}
In the domain class itself I have a method for Something objects which populates the friends list. But for some reason I am not able to persist this list. and it gets made null on browsing away from the gsp relevant to the domain modifying action.
Any suggestions as to why this is happening?
I think you instead need to write your domain as:
class Something {
String name
String email
List friends
static hasMany = [ friends: String ]
}

Putting entries from a list property into a statically mapped joinTable

I have a domain class called FoapRequest. I want one of the properties called "approver" to be a list of integers. Order matters, so I've defined the class as described by http://grails.org/doc/latest/guide/GORM.html#sets,ListsAndMaps as a list:
class FoapRequest {
Integer requester
Integer subject
List approver
static hasMany = [foap:FOAP, newFoap:NewFoap, approver:Integer]
...
Just for clarification, FOAP and NewFoap are two other domain objects.
I need to map this class to a particular table in the Oracle database, so I also specify a static mapping with a join table:
static mapping = {
table 'OUR_SCHEMA.FOAP_REQUEST_TABLE
id column : 'ID', generator:'sequence', params: [sequence:'OUR_SCHEMA.FOAP_REQUEST_SEQ']
requester column : 'REQUESTER'
subject column : 'SUBJECT'
approver indexColumn: [name: "APPROVER_IDX"], generator:'sequence', params: [sequence:'OUR_SCHEMA.APPROVER_SEQ'],
joinTable: [name:'OUR_SCHEMA.APPROVER_TABLE',
key: 'ASSOCIATED_REQUEST',
column: 'APPROVER_PIDM',
type: "integer"
]
However, when I try to create a new instance of the FoapRequest object, I get the following error:
Invalid column type
The console displays the following:
Error 2012-08-01 12:29:31,619 [http-bio-8080-exec-9] ERROR errors.GrailsExceptionResolver - SQLException occurred when processing request: [POST] /FOAPauth/foapRequest/saveFoapRequests - parameters:
I am certain that the issue lies with the jointable. The domain model didn't include the joinTable originally- approver was just an Integer type (I realized too late that I was going to need to track multiple approvers).
Here's the SQL for creating the APPROVERS table:
CREATE TABLE "OUR_SCHEMA"."APPROVER_TABLE"
(
"APPROVER_IDX" NUMBER(*,0) NOT NULL ENABLE,
"ASSOCIATED_REQUEST" NUMBER(*,0) NOT NULL ENABLE,
"APPROVER_PIDM" NUMBER(8),
);
I'd prefer to avoid creating an Approver domain class if at all possible, since all I really need to keep track of are the integer identifiers.
So, after much janking with join tables, I determined that the best way to deal with my needs was to simply create an Approver object in my domain model.
class Approver {
Integer pidm
String approvalDecision
Date lastUpdated
Date dateCreated
static belongsTo = [foap: FOAP]
}
To be honest, I'm not really sure why I was trying so hard to avoid this. Possibly because my DBAs use a version control system for table definitions that I find a hair annoying. :)
Regardless, a simple one-to-many relationship between domain classes met all my needs, no join table required.
For those who are still burning to know, I did manage to get a statically mapped join table working using a Map, which was more appropriate for my needs (though not as appropriate for them as a new domain class, and not nearly as simple).
I ended up doing it in a different domain object- FOAP instead of FoapRequest:
import java.util.Map
class FOAP {
...
Map approvalData
...
static mapping = {
table 'OURSCHEMA.FOAP_TABLE'
id column : 'ID', generator:'jpl.hibernate.util.TriggerAssignedIdentityGenerator'
fund column : 'FUND'
org column : 'ORG'
chartOfAccounts column : 'CHART_OF_ACCOUNTS'
permissionType column: 'PERMISSION_TYPE'
foapRequest column: 'REQUEST_ID'
version column : 'VERSION'
approvalData joinTable: [name:'OURSCHEMA.FOAP_APPROVERS',
key: 'FOAP'
]
}
For the table definition, I used the column names similar to those in my original question.
CREATE TABLE "OUR_SCHEMA"."APPROVER_TABLE"
(
"FOAP" NUMBER(*,0) NOT NULL ENABLE,
"APPROVER_IDX" VARCHAR2(255),
"APPROVER_DLT" NUMBER(8),
);
The IDX column was the map object's key, the DLT column its value. I'd recommend against this approach, for anyone who can avoid it. Creating a new domain object is much simpler.

GORM/Grails - add extra column to a joinTable expression

I have a Domain Class setup similar to this
class NewsStory {
String headline
static hasMany = [channels:Channel]
static mapping = {
table 'NewsStory'
addresses joinTable:[name:'Article_Channel', key:'ArticleId', column:'ChannelId']
}
}
in the Article_Channel table i need to have an extra column populated called ArticleType say. Its value will always be the same e.g. 'news' for this domain class, but will be differnt for others e.g. 'blog'
Channel is just something like 'Security' etc
Is there a way?
Thanks
One option would be be to create your own many-to-many mapping class and add the field in there.
http://grails.org/Many-to-Many+Mapping+without+Hibernate+XML
So, for example:
class ArticleChannel {
NewsStory newsStory
Channel channel
String articleType
}
Then, your NewsStory and Channel classes would hasMany the ArticleChannel class.

drop down list in MVC

I am absolutely new to the MVC application.I want a dropdown list on my form I have done it this way
"<%= Html.DropDownList("Categories", (IEnumerable<SelectListItem>)ViewData["Categories"])%>"
I am sending the viewdata[Categories] from the controller class file.
this way
IList allCategories = _dropdownProvider.GetAllCategories();
ViewData["Categories"] = new SelectList(allCategories, "catid", "catname");
Now my requirement is that when the user selects a particular category from the dropdownlist its id should get inserted in the database,
The main problem is category id I want to insert the category id in the product table where the category Id is the foreign Key.
Please tell me how can I do this.
Normally you would do the following:-
On your view you would have...
Inherits="System.Web.Mvc.ViewPage<Product>" THIS IS A REFERENCE TO YOUR PRODUCT ENTITY
and in the page you can do this...
Category <%=Html.DropDownList("CatId") %>
you would also have the GET controller which defines the list
public ActionResult Add()
{
ViewData["CatId"] = new SelectList(allCategories, "catid", "catname");
then you can get the CatId from the product passed in to the Add method
e.g.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(Product product)
{
int catId = product.CatId
HTH. You ought to buy a book on the subject as MVC takes away alot of the pain of binding from you.
when you try to post the view, you'll have a "Categories" key in your querystring. you could convert it to long or the type you use in your table and set it to the product instance that you want to.
if this is not so clear, please send your code for a better explanation.
If i Understand right your question, the only thing you have to do, is inspect the POST for the "Categories" Key, and it will contain the selected value of the DropDownList.
var selectValue = System.Convert.ToInt16(Request.Form["name"]);
Or if you use ModelBinder and define a model that bind that value directly, you just have to Update the Model with
bool x = TryUpdateModel(YourModelNameHere);
This will automatically inspect the current ControllerĀ“s Value Provider and bind that value to the corresponding property in your model.
I recoment you to use the parameter FormCollection in your controller, and put a breakpoint, you can see all the values send within the POST. All that values are accessible through Request.Form["KEY"].

Resources