Re: dynamically adding derived EOAttribute
Jonathan Rochkind <[email protected]>
| Newsgroups | gmane.comp.web.webobjects.eof |
|---|---|
| Message-ID | <[email protected]> |
I solved this. In order for the definition to 'stick', you need to create
the EOAttribute with the constructor that takes an EOEntity. It has to
already know it's entity to take a definition.
This works:
EOAttribute countAtt = new EOAttribute(new
NSMutableDictionary(), entity);
countAtt.setName("_derivedCount");
countAtt.setColumnName("");
countAtt.setDefinition("count(*)");
countAtt.setExternalType("int");
countAtt.setValueClassName("NSNumber");
countAtt.setValueType("i");
Questions:
1) Is this a bug? Either in documentation or in effect? Should I file it?
2) I feel like altering an EOEntity at run-time like this, I should be
worried about locking/synchronization. Anyone else altering EOEntitys at
run-time? What's the appropriate kind of locking to do? I'm not sure.
EOEntity has no lock() method. ?.
At 12:04 PM 9/26/2002 -0500, you wrote:
>In the past, in order to execute a count(*) query, I've added an attribute
>to my EOModels such that it's a non-class property, derived attribute,
>defined as count(*). Now you can create an EOQualifier, create an
>EOFetchSpecification, set it up to fetch raw rows, including only the
>counting attribute, and you've easily executed a count(*) query.
>
>So far so good, but I thought the next logical step would be writing code
>to automatically add this 'count' attribute on demand at runtime, so I
>could execute count(*) queries on any entity I want, without having to add
>this attribute to the EOModel for each entity.
>
>I didn't think this should be too hard; I just create the EOAttribute at
>run-time when needed, and add it to the relevant EOEntity. I looked in the
>.plist file for one of the entities where I had added such an attribute
>in-model, and copied exactly the key/values in there to figure out how I
>wanted to define my new EOAttribute in code. The problem is, when I try to
>execute this code, I get a weird exception from EOF:
>
>sqlStringForAttribute: attempt to generate SQL for attribute
>'_derivedCount' on entity 'ConcreteStoredRequest' with undefined column
>name. You must define a column name for this attribute before attempting a
>database operation.
>
>What? But it's a derived attribute. It shouldn't need a column name. My
>in-model attribute works fine without a column name in it's .plist
>definition. Why isn't it working when I create it in code at
>run-time? Below is the code I use to create the EOAttribute. Further
>investigation reveals that the setDefinition() method does not appear to
>be "sticking". No matter what I call with setDefinition(), the result of
>definition() is null. Huh? Why can't I set the definition? Is this a bug?
>Is anyone else doing anything like this?
>
> EOAttribute countAtt = new EOAttribute();
> countAtt.setName("_derivedCount");
> countAtt.setDefinition("count(*)");
> countAtt.setExternalType("int");
> countAtt.setValueClassName("NSNumber");
> countAtt.setValueType("i");
> entity.addAttribute( countAtt );
>_______________________________________________
>EOF mailing list
>[email protected]
>http://www.omnigroup.com/mailman/listinfo/eof