For example I've been written a simple domain-class:
class Book{
String bookName
}
I've done some records of this instance. And now I wanna update one of them, to realize it, I've an idea to write a setter for bookName. But may be is more elegant method to do this via GORM-methods?
Here is an example. For example, in a service method.
Book bookInstance = Book.get(id) //id is the primary key
bookInstance.bookName = "new book name"
bookInstance.save(flush : true)
Grails uses the Groovy method - getter and setters are created for you and you can take it that they are available for your use on each property of a domain class that you create. See http://www.groovy-lang.org/style-guide.html section 7.
Alfredo suggested one way in which you can get one instance of the Book class, but to use this you need a mechanism in place to obtain the id so that you can use the get() method.
If you want to get an instance of book by its name, you can use dynamic finders. See 6.4.1 Dynamic Finders of http://grails.github.io/grails-doc/latest/guide/GORM.html#finders.
The simplest one to use would be to use this to get your book instance:
def bookInstance= Book.findByBookName('the book name that you want to change')
You can then update it as has already been described.
Related
I've just started to play with library looks very interesting for core data called Graph.
I just wondering if there is any way to work with real object and not just with Entity for example:
let elon: Entity = Entity(type: "Person")
elon["firstName"] = "Elon"
elon["lastName"] = "Musk"
I would like to work with Class\Struct called Person with 2 properties: firstName and LastName.
I know I can to create a Class \ Struct and create computed property and return the exact value, But I wondering if there is an elegant way to do this without so many boilerplate code.
Create a subclass of NSManagedObject called Person and give it a #NSManaged var firstName: String property. Then in you data model, create an entity called Person and set its class to be Person. Then add the firstname property to it.
Our book has more details on how to create properties, classes, etc., https://www.objc.io/books/core-data/ -- and the preview covers how to model data: https://www.objc.io/books/core-data/preview/
So the answer you received is the correct CoreData method to use properties directly. Your question though, regarding Graph, was not answered correctly with the above answer. The easiest method to accomplish what you like, is to create a wrapper class around the Graph Entity object you are managing.
This will give you all the CoreData features without the work, plus the elegance of using properties directly.
If you are going to use Graph, I would recommend to use it as it is, until you are comfortable. The reason being, is the additional features Graph offers, will be awkwardly available to you unless you know what you are doing, if you start creating wrappers everywhere.
We're creating some custom fields by adding new definition fields to category and product definition items in uCommerce.
When we retrieve an instance of the ctegory or prouduct from the uCommerce.Entitiesv2 we're having trouble getting the culture specific value for these fields when multilingual is selected?
There is a collection on the Product object called ProductDefinitionField but not sure whether .Value returns the culture specific version of whether we need to call another method (extention method maybe)
Has anyone got a code snipper for this?
When accessing or retrieving Multilingual properties on a uCommerce you can use the GetProperty method on a product.
It has two overloads, one taking name (string) and another one taking name (string) and culturecode (string).
If you want to retrieve the full collection of multilinqual properties you can use GetProperties which also have two overloads. One without parameters and the other with a string culturecode.
Depending on the version of uCommerce you're using some of them might be missing/not a part of the API.
Best regards Martin
I was wondering how to persist data to a hsqldb. For example I am trying to save a simple name to a database but I cant seem to figure out how to persist it.
The recommended approach is to create a domain class with a String name property. Then you can save it, and you're done. First, create the domain class:
$ grails create-domain-class com.foo.Person
Then edit the grails-app/domain/com/foo/Person.groovy:
package com.foo
class Person {
String name
}
In controller actions or service methods you can create, save, and retrieve data:
def heMan = new Person(name: 'He Man')
if ( !heMan.save() ) {
// Handle problems saving (e.g. constraint violations)
}
def h = Person.findByName('He Man')
println h.name
An alternative approach is to work with JDBC directly. You can have dataSource bean automatically injected into your controller, then use the groovy.sql.Sql class to query that dataSource. Check out this stackoverflow.com question.
Two ways:
GORM
raw JDBC (via spring's JdbcTemplate)
I would suggest starting with a good Grails tutorial such as this one at IBM or one of these. Learn to use GORM. It will make your life better.
Since Rails is using Ruby (dynamic language), would it be possible to create a very flexible
'configuration' class that has properties that you use throughout the website, AND have the ability to add new class properties (in the db for web modification) and then just use it in the code.
Each property would be of a specific type like a string, integer, bool etc.
You can't do this in a strongly typed language, but it must be possible with Ruby!
So say my class is like:
globalConfig.is_active
globalConfig.admin_email
I guess to make this work, I would loop through all the properties in the db, create properties in the class and assign the value right?
I actually have a settings plugin on GitHub you can use:
http://github.com/bellmyer/settings
It makes this easier for you. Right now it's not rails3-ready, so let me know if you need that. I also need to put in the time to roll it into a gem, instead of a plugin.
If you end up using it, let me know and I'll get it up to date. Otherwise, you can look at the code to see how I did things, and use that to help build your own custom solution.
I'm very new to grails (day 2).
First, I find it hard to find easily browsable resources (the documentation is very raw, and the tutorials are spread out and only show 'hello world' types of examples ).
I've set up my domain class with a relationship to other data types.
class ShopCategoryPage{
//some stuff
ProductProcedure procedure;
ProductCategory category;
//some stuff
}
In my controller, I am getting a category id and a procedure id as parameters, and I am trying to get the ShopCategoryPage associated with those parameters.
How do I "find" them? I tried passing the ids as procedureId or procedure_id, I tried passing a ProductProcedure object generated by findById ...
I'm not sure how to find by a property that is not of native type.
First, I find it hard to find easily browsable resources (the documentation is very raw,
and the tutorials are spread out and only show 'hello world' types of examples ).
In my opinion the documentation is great, perhaps we're using different docs. I use:
Grails Reference Documentation
Grails JavaDoc
GDK, i.e. methods Groovy adds to Java classes
If that still isn't satisfactory, I highly recommend the book "The Definitive Guide to Grails". I believe "Grails in Action" is also very good, but haven't read it. For learning Groovy, "Programming Groovy" is a great book (albeit a little out of date).
In my controller, I am getting a category id and a procedure id as parameters, and I am
trying to get the ShopCategoryPage associated with those parameters.
The easiest way (though not the most efficient) is to use the dynamic finders.
// First of all load the ProductProcedure and ProductCategory
// I'm assuming here the request params are named 'procedureId' and 'categoryId'
ProductProcedure productProcedure = ProductProcedure.get(params.procedureId.toLong())
ProductCategory productCategory = ProductCategory .get(params.categoryId.toLong())
// Now get the ShopCategoryPage associated with these. Replace 'find' with 'findAll'
// if there could be multiple associated ShopCategoryPages
ShopCategoryPage shopCategoryPage = ShopCategoryPage.findByProcedureAndCategory(productProcedure, productCategory)
A shortcoming of this approach is that it will cause 3 SELECT statements to be executed. If you're only interested in the shopCategoryPage returned by the last query, you could load this in "one shot" using HQL or a criteria query instead.
You should never use findById since it'll bypass the id-based 2nd-level cache and only use the query cache, which is a lot more volatile. Use get() instead.
Having said that, there's two ways to do this. One is to use get():
def shopCategoryPage = ShopCategoryPage.findByProcedureAndCategory(
ProductProcedure.get(params.procedureId),
ProductCategory.get(params.categoryId))
(use the appropriate param name for the two ids)
or using an HQL query (or a criteria query) to fetch the instance in one query instead of 3:
def shopCategoryPage = ShopCategoryPage.executeQuery(
'from ShopCategoryPage p where p.procedure.id=:procedureId and p.category=:categoryId',
[procedureId: params.procedureId.toLong(), categoryId: params.categoryId.toLong()])[0]
I agree, the easiest way is to use an dynamic finder.
In addition to 'The Definitive Guide to Grails' by Rocher and Brown, I suggest the IBM developerWorks track 'Mastering Grails' by Scott Davis.
http://www.ibm.com/developerworks/views/java/libraryview.jsp?site_id=1&contentarea_by=Java&sort_by=Date&sort_order=1&start=1&end=19&topic_by=&product_by=&type_by=All%20Types&show_abstract=true&search_by=mastering%20grails