I have a list of resources that have managers which are also resources in my system. I want to sort the list of resource by their names. What I am finding is that Grails is sorting the list by the resource Id and not the field that I am specifying.
<g:sortableColumn property="orgRole" title="Org Role" />
<g:each in="${resourceInstanceList}" status="i" var="resourceInstance">
<td>${fieldValue(bean: resourceInstance, field: "manager")}</td>
<!--<td> ${resourceInstance.manager.toString()} </td>-->
</g:each>
I have defined the static mapping on my resource to sort by my 'name' attribute and I have a toString method that I have tried calling that also returns the 'name' of the object as the value. Still seeing the list sorted by the Integer 'id' attribute that is tied to the sequence in the database.
The precision needs to be defined on the sortableColumn and not on the td. I should have used this:<g:sortableColumn property="orgRole.name" title="Org Role" /> this explicitly tells Grails which field I want to use, otherwise it relies on the default id element of the object.
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 need to get value field2 from list values in field1. Field1 is relation many2many with field in another model.
I tried to use domain for it but everytime I received error.
class filial_page_products(models.Model):
gallery_rstamp_products_ids = fields.Many2many('product.template',
'gallery_rstamp_products_rel',
'gallery_rstamp_products_ids', 'filial_page_new_rstamp_products_ids',
'Gallery products')
default_gallery_product_id = fields.Many2one('product.template','Default maket', domain="[(default_gallery_product_id, 'in', 'filial_page_gallery_rstamp_products_ids')]")
class product(models.Model):
_inherit = 'product.template'
filial_page_gallery_rstamp_products_ids = fields.Many2many('product.template',
'gallery_rstamp_products_rel',
'filial_page_recovery_rstamp_products_ids', 'gallery_rstamp_products_ids',
'Gallery list')
filial_page_default_maket_product_ids = fields.One2many('pr_filials.filial_page_products',
'default_gallery_product_id',
'Linked page products')
How can I use domain to select only those values that are specified in the gallery_rstamp_products_ids field?
of course, I can set default_gallery_product_id from all products but I don't like it.
Your domain doesn't look quite right. The left operand should be quoted and the right side should not be quoted (unless it's actually supposed to be evaluated as a string).
domain="[('default_gallery_product_id', 'in', filial_page_gallery_rstamp_products_ids)]"
Note, there's a special format required for filtering against x2many fields (one2many or many2many). You may need to use this (below), however, there have been reports of issues using this in newer versions.
domain="[('default_gallery_product_id', 'in', filial_page_gallery_rstamp_products_ids[0][2])]"
Here's some documentation on domains.
I have the following structure
lookups --> Object
lookups.CATEGORIES --> array of category objects
lookups.TIMEZONES --> array of timezone objects
I would like to add new object, which is a lookup object which has lookup_type property. It could be either 'CATEGORY' or 'TIMEZONE'.
Depending on lookup_type, the newly added object has to be added either to CATEGORIES or TIMEZONES object. How this could be achieved?
The structure of lookups object
lookups: {CATEGORIES:[{obj1}, {obj2}...], TIMEZONES:[{obj1}, {obj2}, {obj3}...]}
You can use spread on the nested object or array too:
return {
...state,
[action.lookupType]: [
...state[action.lookupType],
action.value,
]
};
That would add a new item to Categories or Timezone, if you want to replace a value or insert it at an index etc then you should construct the new array how you want it just above the return and pass that instead. Also note that array spread is ES7.
You probably want to pass your lookup object as the payload of an action, which your lookup reducer handles. In the lookup reducer check for the value action.payload.lookup_type and return the state with a new CATEGORIES or TIMEZONES array containing the old values of that array with the lookup object insterted. You should probably check out some redux examples first, if you are unsure how to work with it.
I have a bean class named AnnouncementDetails having attributes
Now on submit i want to get the values of the selected users in selectedUsers Arraylist. Any idea how to do that? ..............................................
Change datatype to String array and then convert to arraylist by Arrays.asList(selectedUsers).
String [] selectedUsers
I'm coding an API and I'm doing the create method. I'm doing the following without needing a form:
$params = array('title' => 'test', 'parent_id' => 781);
// bind data
$place = new Place();
$place->bind($params);
// validate params
$errors = $this->validator->validate($place);
I need to check that parent_id is a correct value (its object exist - i know how to do this) and after that, I need to set some values dependent on the parent. So at the end the Place object will have the fields: title, parent_id, level, country_id for example.
How would you do this? On the validation? How? If not, how to avoid calling two times the DB to get the parent object?
You should first validate & then set any additional values afterward. Anything that modifies the value does not belong in the validator.
If your using doctrine, it should load the parent object into memory when you first access it, so it won't need to actually query the database again when you access the parent object a second time.