dynamically adding derived EOAttribute

Jonathan Rochkind <[email protected]>
Newsgroups gmane.comp.web.webobjects.eof
Message-ID <[email protected]>
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 );
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.