Comments

dpi’s picture

+1

Using the example from the project page:

Object.module

$node = new drupalNode();
$node->load(1);
$node->title = 'Test Title';
$node->save();

or
$user = new drupalUser(212);
$user->name = 'new username';
$user->save();

Entity.module

$node = entity_create('node');
$node->title = 'Test Title';
entity_save($node);
randallknutson’s picture

dpi,

Thanks for the question. The main difference between the two is that the entity module does not actually turn entities into true objects. Your example from the entity module even shows this. Instead of creating an object, you called procedural code of "entity_create" to create the entity and then another procedural function, "entity_save," to save it. I'm wrapping all the functions up into the object as well to make them truly objects with functions.

You no longer need all the procedural functions and pass the standard object into them, you only have to call the save() function on the object.

randallknutson’s picture

Let me give you another example. Can you do the following with the Entity module without using procedural code:

$account = new drupalUser();
$account->load_by_name('myusername');
if ($account->access('access awesome message') {
  drupal_set_message('This message is awesome');
}
$account->set_field('field_option', 'blue', 0, 'value');
$account->pass = 'New Password';
$account->save();

Here is another example to create a new page node:

$node = new drupalNode('page');
$node->title = 'Test Title';
$node->set_field('field_option', 'blue', 0, 'value');
$node->set_field('body', 'This is the body text', 0, 'value');
$node->save();
randallknutson’s picture

Version: » 7.x-1.x-dev

mikeytown2,

Thanks for the links. I'm well aware of the entity module and looked it over to see if it did what I wanted it to do before I started writing this module. If it does what I want to do then it is the best guarded secret in Drupal right now.

All of the documentation and code that I've read about the entity API module show how to create new entity types so that they interact with fields in core and to provide CRUD functionality to those new entity types. This is NOT what this module does.

This module is about creating objects around core entity (and non entities). Look over my second set of examples. Does the entity API module allow me to do this? If it does then I am very glad to hear it but I've never seen any code nor heard anyone talking about it.

I'd like to avoid ever having to populate the $edit array when doing a user_save again and remember to do a node_submit before a node_save when creating new nodes. If the entity module does this then I'm all for it but I can't tell that it does.

amitaibu’s picture

Sounds like what your looking for is Entity's wrapper:

$wrapper = entity_metadata_wrapper('node', 1);
$wrapper->field_foo->set('bar');
// Get the node's author.
$account = $wrapper->author->value();
// Save node.
$wrapper->save();
randallknutson’s picture

Amitaibu,

I had no idea that existed. I am rethinking my approach and will post again soon.