Re: Retrieve data from DB
Re: Retrieve data from DB
- Subject: Re: Retrieve data from DB
- From: "John Bruce" <email@hidden>
- Date: Tue, 5 Dec 2006 12:45:14 +1100
Just to add to what Chuck has already said. Casting to User will work
but only if you are using a custom Class for the "User" entity in your
model. If you are using EOGenericRecord you can retrive the value for
the admin attribute (the equivalent of a database column) through
Key-Value coding eg:
Object adminValue = aUser.valueForKey("admin")
However if you have set this column to be an Integer or boolean or
even a string you can cast the returned value so another option maybe:
Number adminValue = (Number)aUser.valueForKey("admin")
then if (adminValue.intValue() == 1) { do admin stuff... }
Also the commons-lang library has useful methods for converting
strings to booleans in addition to the JDK. For example if the admin
sttribute is a String which stores the value as "yes" or "no" then you
could do:
if (org.apache.commons.lang.BooleanUtils.toBoolean((String)aUser.valueForKey("admin"))
{
do admin stuff...
}
HTH
John
On 12/5/06, Chuck Hill <email@hidden> wrote:
On Dec 4, 2006, at 5:26 PM, email@hidden wrote:
> Hi *,
>
> I'm totally new to both, WO and this list
Welcome!
> and am unfortunately about to make a rather unglamorous debut with
> a complete noobie question.
> I've tried to google, tried to find the information in the apple-
> developer-docs...all to no avail. :(
Thank you for trying, it is appreciated.
> Here goes:
>
> Inside the logic of my little login algorithm i do:
>
> EOEnterpriseObject aUser = EOUtilities.objectMatchingValues(ec,
> "user", bindings);
>
> This is all working fine and i can login when the user exists and
> the correct password has been given.
> But I also would like to check, whether or not the user logging in
> is a site admin or not. For that, my 'user' entity has a third
> attribute (besides "username" and "password") that is "admin".
> Now...from what I've read I could apparently fetch all objects from
> my db that have "admin = 1" and then see if my username appears
> anywhere in that list but this seems insanely wrong since I already
> have the object of interest in 'aUser'!?
> So my noobness boils down to this question:
>
> How do I get the value of the column "admin" for the
> EOEnterpriseObject that's stored in 'aUser'?
>
> Thank you all for your patience with noobs like me.
We tend to be pretty friendly.
I think you will like the answer to your question when you see it.
The problem you are running into is that
EOUtilities.objectMatchingValues returns an EOEnterpriseObject, not a
User. EOEnterpriseObject obviously knows nothing about user names,
password, or admin flags. The solution is simply to cast the result
so that the Java compiler knows what kind of object you have:
User aUser = (User) EOUtilities.objectMatchingValues(ec, "user",
bindings);
And then, ask it about admin
if (aUser.admin().booleanValue())
{
// code for admin users here
}
if the admin column is a character you will need something like this:
if (aUser.admin().equals("Y"))
{
// code for admin users here
}
Also, it is customary to make entity names start with an upper case
letter, so if you update your model, your code would say
User aUser = (User) EOUtilities.objectMatchingValues(ec, "User",
bindings);
Chuck
--
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems.
http://www.global-village.net/products/practical_webobjects
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden