4

I have tried extending EntitiyFieldQuery to add a condition that would be able to match on a $user->data["somevar"], but to no avail. I am not sure this is even possible, as $user->data is serialised.

I have just the base class working, returing users with status set to 1:

class SubscriberEntityFieldQuery extends EntityFieldQuery {
  // define some defaults for the class
  public function __construct() {
    // now we don't need to define these over and over anymore
    $this
      ->entityCondition('entity_type', 'user')
      ->propertyCondition('status', 1);
  }
}

Would anyone have an idea if this is even possible? Or would I need to resort to using db_query for a case like this?

4

The data column contains a serialized PHP string, which MySQL doesn't understand. As far as MySQL is concerned the data is just a string like any other.

Because of that you won't be able to query the properties of the serialized objects in that column without doing some seriously nasty work with SUBSTRING/regex/insert-other-unreliable-method-here.

If possible you should add a field to the user entity instead that represents the same data, and use the EntityFieldQuery with that field. It might not be ideal but it's probably the easiest solution.

1
  • Yes. This was my first impression also. I avoided extra user fields and implemented as a data variable to prevent having extra profile fields for not-often accessed data. Thanks friend. – stefgosselin Aug 6 '12 at 22:22
1

May be you need fieldCondition like the one below

class SubscriberEntityFieldQuery extends EntityFieldQuery {
  // define some defaults for the class
  public function __construct() {
    // now we don't need to define these over and over anymore
    $this
      ->entityCondition('entity_type', 'user')
      ->propertyCondition('status', 1)
      ->fieldCondition('field_data_field_yourcustomuserfield', 'value', 'whatever-value-you-are-looking-for', '=')
  }
}
9
  • No, unfortunately this is not in a custom field. The variable is set in $user-­­>data, in a serialised state. – stefgosselin Aug 7 '12 at 2:17
  • Oh man variable_get does not match well with fields API. Is there a way you can attach that variable data into a custom field, into the user entity, programatically? These are all the apis available. – Minty Aug 7 '12 at 13:01
  • Custom fields add an extra table to user profile, these add quite a bit more overhead than $user->data . When your profile has a lot of bits of data associated to a profile, you will find $user->data to be a nice lightweight alternative. Unfortunately, you sometimes have to dive through hoops to get to it. – stefgosselin Aug 8 '12 at 0:53
  • What you are saying is correct. But since this is user specific task, it needs to live in the fields. If you have 100,000 users then doing variable_get will prove a bit expensive in the long run. – Minty Aug 8 '12 at 13:53
  • No, friend. variable_get() is not used to get at $user-­­­>data. It is available in $user object after a user_load(). Say you have 15 extra data units to associate with a user profile, having 15 extra tables to join on is overkill, so we chose to store the less frequently accessed ones in $user->data, that is somewhat what it is meant to be used for in the first place. – stefgosselin Aug 9 '12 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.