I'm working with some module and I must add new column to my database.
1) I modified edit form:
$fieldset->addField("temporary", "select", array(
"label" => Mage::helper("my_helper")->__("Temporarily unavailable"),
"name" => "temporary",
'values' =>
Mage::getSingleton('adminhtml/system_config_source_yesno')->toOptionArray(),
));
2) I add new column to grid:
$this->addColumn("temporary", array(
"header" => Mage::helper("my_helper")->__("Temporarily unavailable"),
"index" => "temporary",
"type" => "options",
"options" => Mage::getSingleton('adminhtml/system_config_source_yesno')->toArray(),
));
At the end I run my script na update extension version in config.xml
Mage::log(__FILE__ . '::start');
$installer = $this;
$installer->startSetup();
$tableName = $installer->getTable('my_extension/designer');
$installer->getConnection()
->addColumn($tableName, 'temporary', array(
'type' => Varien_Db_Ddl_Table::TYPE_BOOLEAN,
'default' => 0,
'length' => null,
'nullable' => false,
'comment' => 'temporary'
));
$installer->endSetup();
Mage::log(__FILE__ . '::end');
After that everything works perfectly until, I want to change temporary value "temporary". If I check it once as "Yes" i can't change it back to "No".
Any Ideas
It might be a cache problem.
You need to clear cache through admin System -> Cache Management (Flush Magento Cache and Flush Cache Storage buttons). Magento likes to cache previous queries so adding new fields without clearing the cache can cause problems with new columns not being saved.
Related
I have created a module that adds a field to the user profile. I used field_create_field and field_create_instance to do this. When I disable the module, I want the field to no longer show up on the user profile, but I do not want to destroy it. I want to be able to enable the module and have the field show up and the data to still be there as originally entered. Can anyone tell me how to accomplish this?
Here is the code I used to create the field:
$field = array(
'field_name' => $field_name,
'type' => 'text',
'visibility' => 1,
'category' => 'API',
);
$field = field_create_field($field);
$field_instance = array(
'field_name' => $field_name,
'entity_type' => 'user',
'bundle' => 'user',
'label' => t('API Token'),
'cardinality' => 1,
'translatable' => 0,
'description' => t('By using this API token, you agree to the site Terms and Conditions and to acknowledge that your submission does not include protected health information or personal identifiers.'),
'widget' => array(
'type' => 'text_textfield',
'weight' => 10,
),
'formatter' => array(
'label' => t('field formatter label'),
'format' => 'text_default'
),
'settings' => array(
),
);
When you have created field using drupal entity like user, node etc then on that entity crud operation automatically apply.
As you have used api to "field_create_field" field then it automatically create field using api of entity vise verse its delete field when you uninstalled module.
First tell me when you uninstall your custom module then your custom field deleted from profile.? If yes then it's difficult to handle your use case. If no then in system table of Drupal you get status of your module whether it's disable or enable if status is 0 then used form alter hook of user profile and hide field
I was not able to accomplish exactly what I wanted, but I ended up installing the field extra widgets module and hiding the field completely on the edit form. Then, I used hook menu alter to create a local task tabs and I display the field on that tab.
in CakePHP, when applying the debug function on a User model :
debug($user);
we get a result that hide the login or password.
'User' => array(
'login' => '*****',
'id' => (int) 2,
'pwd' => 'fjiogjfdlmgjdomngdjm',
'avatar' => null,
'prenom' => 'Fake',
'nom' => 'Admin',
'email' => 'blabla#domain.fr',
'i18n_code_appli' => '',
'numtel' => ''
),
How could we configure what model has that field hidden or not ?
Like you can see in the example below, as password field named 'pwd', it is not hidden, but login is.
And this is my actual case. I would want to toggle this to view 'login' and hide 'pwd' in debug mode.
As agreed in the comments of OP, I'll add my comment as an answer.
Just use var_dump() instead of debug() if you have to check the password hash. I have had the same problem a long while ago and I thought I'd used var_dump then. I guess you aren't keeping the call to debug() in the code on release so it shouldn't do any harm.
Or you could use the CakePHP Debug Kit. It has a list of all the set variables which you can collapse and extend. That way you have a clear overview of all the data in a deep multidimensional array.
I have created a custom module in drupal with entities. I have installed the entity api module. I have created my database schema with just two columns (employee_id, first_name) through the help of employee_management.install file (where as employee_management is my custom module name) and employee is my entity name.
I have also written the requisite functions employee_management.module but still it shows me the error , Whenever i tried to add a new entity in the admin/structure/employee it shows me the following error: "Not Found".
The requested URL drupal/employee/add/ was not found on this server.
function employee_management_entity_info() {
$employee_info['employee'] = array(
// A human readable label to identify our entity.
'label' => t('Employee Entity'),
// The controller for our Entity - extends the Drupal core controller.
'controller class' => 'EmployeeController',
// The table defined in hook_schema()
'base table' => 'employee',
// Returns the uri elements of an entity
'uri callback' => 'employee',
// Fieldable that we can attach fields to it - the core functionality will
// do the heavy lifting here.
'fieldable' => TRUE,
// The unique key of our base table.
'entity keys' => array(
'id' => 'employee_id',
),
// FALSE disables caching - caching functionality is handled by Drupal core
'static cache' => TRUE,
// Attach bundles - i.e. alternative configurations of fields associated with a main entity.
'bundles' => array(
'employee' => array(
'label' => 'Employee',
// Information below is used by the Field UI - they "attach" themselves here and lets us
// do the standard field management that all the core entities enjoy.
'admin' => array(
'path' => 'admin/structure/employee/add',
'access arguments' => array('administer employee entities'),
),
),
),
// View modes allow entities to be displayed differently based on context. We simply have one option
// here but an alternative would be to have a Full and Teaser mode akin to node.
'view modes' => array(
'full' => array(
'label' => t('Full'),
'custom settings' => FALSE,
),
)
);
return $employee_info;
}
EDIT
function employee_uri($employee) {
return array(
'path' => 'employee/' . $employee->employee_id,
);
}
And here is the complete list of function in the file employee_management.module
You don't automagically get the route and form to create your entity, you'll have to implement that yourself. See hook_menu and this guide.
So, what I have is a page which displays a post. Posts are gathered into projects. And, on this page, in addition to the post, I'ld to display the previous and next post of the same project. So, I have this in my controller :
$this->set("post", $this->Post->find("first", array("conditions" => array("Post.id" => $id), "contain" => array("User", "Project", "Project.Post" => array("id", "image", "title"), "Comment", "Comment.User" => array("id", "username", "mail"), "Comment.User.Post" => array("id", "image", "title")))));
$this->set("neighbors", $this->Post->find("neighbors", array("field" => "id", "value" => $id)));
The problem is that it get the previous and next post from all the post, not only the ones of the same project.
So, if you could helping me a bit :)
I'm assuming Post belongsTo Project and Project hasMany Post.
You would just need to add an extra condition:
$post = $this->Post->find("first", ...); // this is your first find call, assign it to a var instead
$neighbors = $this->Post->find('neighbors', array(
'field' => 'id',
'value' => $id,
'conditions' => array(
'Post.project_id' => $post['Post']['project_id'],
),
));
$this->set(compact('post', 'neighbors'));
I haven't tried it, but looking at the source, findNeighbors does honor the conditions key.
I have been able to use CakePHP's saveAll method to simultaneously create 'Members' and enroll them in an 'Event' (creating the HABTM link record), which is awesome. For example, this code creates two new 'Members' and adds a record for each of them to the 'EventsMember' table, enrolling them 'Event' 10:
$data = array(
'0' => array(
'Member' => array('email' => 'nobody#nowhere.com'),
'Event' => array('id' => 10)
),
'1' => array(
'Member' => array('email' => 'somebody#nowhere.com'),
'Event' => array('id' => 10)
)
);
$this->Member->saveAll($data);
However, the record in the 'EventsMember' table also has a field called 'role' that holds something like "Presenter" or "Host" or "Attendee" and I would like to save that data when I create the relationship. I tried this and it does not work (the 'EventsMember' 'role' field is always blank):
$data = array(
'0' => array(
'Member' => array('email' => 'nobody#nowhere.com'),
'Event' => array('id' => 10),
'EventsMember' => array('role' => 'Host')
),
'1' => array(
'Member' => array('email' => 'somebody#nowhere.com'),
'Event' => array('id' => 10),
'EventsMember' => array('role' => 'Attendee')
)
);
$this->Member->saveAll($data);
I'm wondering if this is even possible, and if maybe I have to use some kind of callback like beforeSave or afterSave to get this done? I've read that there are some problems with these callbacks when using saveAll, so I'm looking for any tips on what would be the best practice here.
Thanks!
EDIT: I took Adam's advice and made the following changes to my models:
// Event.php
var $hasMany = array('EventsMember');
// Member.php
var $hasMany = array('EventsMember');
// EventsMember.php
var $belongsTo = array('Event', 'Member');
Then in my controller, my code looked almost identical to my second example, except I called the saveAll() method from the EventsMember model, as described in the documentation:
$data = array(
'0' => array(
'Member' => array('email' => 'nobody#nowhere.com'),
'Event' => array('id' => 10),
'EventsMember' => array('role' => 'Host')
),
'1' => array(
'Member' => array('email' => 'somebody#nowhere.com'),
'Event' => array('id' => 10),
'EventsMember' => array('role' => 'Attendee')
)
);
$this->EventsMember->saveAll($data);
The result was no Member or EventsMember records were saved at all. I tried triggering the save from the Member model ($this->Member->saveAll($data);) and this saved the Member records, but not the joining EventsMember records.
I tried removing the pre-existing HABTM associations, and it made no difference. The beforeSave method of the EventsMember model is getting triggered when I call $this->EventsMember->saveAll($data); but it looks like it won't actually save anything.
I'm stymied.
UPDATE: It turns out that no records were created because the joining records were all being created with Event ids and Member ids of 0, which goes against a unique key I have on those two fields combined (that is, no Member can enroll in an Event twice).
Does this suggest that the join model saveAll functionality is not working as documented, since the Member record isn't getting created (meaning there is no Member id to use in the joining record), and the existing Event id is not being passed to the joining EventsMember record either?
VERDICT: I changed the controller to loop on each record and attempt to $this->EventsMember->saveAll($data) for each index of the array, instead of passing the entire array at once. It worked, but was significantly slower than my first example (at the very top). Mind you, I am using transactions, so perhaps using the atomic => false; option would speed things up, while still allowing me to recover from any invalid records.
Bottom line, if you need to save extra data in join table records, you have to process them one at a time. If not, use the method at the top for the best performance.
You can use The Join Model in this case.