How can I store a file in a drupal entity? I have a plublic key to associate to an user so I have created an APIuser entity but I don't know what kind of field give to the public key property
function api_user_schema() {
$schema['api_user'] = array(
'description' => 'The base table for api_user.',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for an artwork.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'public_key' => array(
'description' => 'The primary identifier for the public key.',
'type' => ???,
'unsigned' => TRUE,
'not null' => TRUE,
)
'created' => array(
'description' =>
'The Unix timestamp when the api_user was created.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'changed' => array(
'description' =>
'The Unix timestamp when the api_user was most recently saved.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'unique keys' => array(
'id' => array('id')
),
'primary key' => array('id'),
);
return $schema;
}
What you've got there is the definition of a single database table; Drupal offers no layer on top of that for files, so if you want to store a file you'll have to do so manually.
The best example you can take is that of the core user entity. It defines the picture property, which is an ID referencing an entry in the file_managed table (incidentally this is how all permanent file storage is handled by Drupal core by default).
This is the schema definition for that db column (from user_schema()):
'picture' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => "Foreign key: {file_managed}.fid of user's picture.",
)
Which is very similar to what your definition will need to look like.
From there, have a look at the user_account_form() function (which defines the form element for the picture property), and the user_validate_picture() function, which will show you how to perform the file upload, save the file in the file_managed table, and change the submitted value for the picture field to the relevant file ID (so that it automatically gets saved against the entity).
You'll mostly be replicating the code from those two functions so it won't be that tricky.
Related
I am attempting to create a database and table for each customer registered from my codeigniter site, i am looking at the dbforge documentation and tried below code.
but error appeared, i want to make it dynamic for each customer so putting it on config/database.php is not available is there a way to prevent this?
i am new with codeigniter not sure if below code is also correct.
You have specified an invalid database connection group
(customer_LYWA) in your config/database.php file.
public function create_customer_database($code){
$database = $this->dbutil->database_exists('customer_'.$code);
if($database){
return "";
}else{
$this->dbforge->create_database('customer_'.$code);
$fields = array(
'blog_id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'blog_title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'blog_author' => array(
'type' =>'VARCHAR',
'constraint' => '100',
'default' => 'King of Town',
),
'blog_description' => array(
'type' => 'TEXT',
'null' => TRUE,
),
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('blog_id', TRUE);
// gives PRIMARY KEY (blog_id)
$this->dbforge->add_key('blog_title');
// gives KEY (blog_title)
$otherdb = $this->load->database('customer_'.$code, TRUE);
$this->dbforge->create_table('blog');
return "created";
}
}
I think you are trying to create a table for each customer.
With that, I think you are confusing databases with tables. databases contain tables, so you will need to connect to the correct database in config/database.php, and use your script to create tables inside that database.
You don't need to create a new database for each customer. That would make things 100 times harder than they should be, and will be very hard to maintain.
How can i add a new field in prestashop's back office?
Specific, i want to insert a text field in the BO: Orders->Statuses->Add New Order Status under the status name. Which files i have to modify in order to do that? Can anyone describes the full procedure?
Thanks
I am using Prestashop version 1.6.1.2 and added one text field using following steps. You need to make changes in core files. You have to add field in one table in database and do some changes in class and controller file.
Here are the steps to do the same. I have adde field 'my_custom_field'.
Add one field in order_state table
ALTER TABLE {YOUR_DB_PREFIX}order_state ADD my_custom_field VARCHAR(50) NOT NULL;
Change class file of order state. You need to define your field in file "classes/order/OrderState.php"
After code
public $deleted = 0;
add this code snipet
public $my_custom_field;
After code
'deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
add this code snipet
'my_custom_field' => array('type' => self::TYPE_STRING),
open "controllers/admin/AdminStatusesController.php" file and do following changes
in function initOrderStatutsList()
after this code
'name' => array(
'title' => $this->l('Name'),
'width' => 'auto',
'color' => 'color'
),
add this code
'my_custom_field' => array(
'title' => $this->l('My Custom Field'),
'width' => 'auto',
),
in function renderForm()
after this code
array(
'type' => 'text',
'label' => $this->l('Status name'),
'name' => 'name',
'lang' => true,
'required' => true,
'hint' => array(
$this->l('Order status (e.g. \'Pending\').'),
$this->l('Invalid characters: numbers and').' !<>,;?=+()##"{}_$%:'
)
),
add this code
array(
'type' => 'text',
'label' => $this->l('My Custom field'),
'name' => 'my_custom_field',
),
Do changes suggested here. Hope this helps you :)
I'm in charge of creating a small payment module. The configuration has to be managed with a simple CRUD, and I use the HelperList class to display a table with the records stored in the database.
One of the tables database structure is similar to this
'CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'MODULE_ITEM
(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`active` VARCHAR(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;'
So, the list_fields value is like this
array(
'id' => array(
title' => $this->l('Id'),
//'width' => 140,
'type' => 'text',
'align' => 'center'
),
'name' => array(
'title' => $this->l('Name'),
//'width' => 140,
'type' => 'text',
'align' => 'center'
),
'active' => array(
'title' => $this->l('Status'),
//'width' => 140,
'active' => 'statusItem',
'type' => 'boolean',
'align' => 'center',
'ajax'=> true
)
);
As I intend to enable or disable the item via a button I use the 'active' and 'ajax' options for this specific field, and when rendered in the module configuration page the link generated for the column in question is something like: index.php?controller=AdminModules&configure=Example&item_id=4&statusItem&action=statusItem&ajax=1&(...). Please notice that statusItem is the name of the action.
On the other hand, I wrote this function in the module main file, which should change the item status.
public function ajaxProcessStatusItem()
{
$id=(int)Tools::getValue('item_id');
$value=(int) Db::getInstance()->executeS($this->createSelectQuery('module_item','item_id',$id))[active];
Db::getInstance()->update('module_item', array('active' => !$value), 'item_id='.$id);
die();
}
I've been using this article of the official documentation to create the list, but no matter what name I use ('ajaxProcess', 'ajaxProcessSatusItem', 'statusItem', and every caps variation I could think of) all I get is a blank page in response, and no change in the status. I had a look at the source code and there is no comment in the HelperList class regarding how the function should be called.
Any help will be appreciated.
If you use ObjectModel class for your data object, you can autmatically generate toggle button just by adding one line:
AdminProductTabController.php or when defining fields somwehre else
and calling HelperList->generate()
'active' => array(
'title' => 'Active',
'active' => 'status',
'filter_key' => '!active',
'type' => 'bool',
'width' => 'auto',
'orderby' => false,
'search' => false,
)
Line 'active' => 'status', doesn't refer to any field names. Add this line to your list definition (if you're defining list field properties in Admin{YourObjectModel}Controller or calling HelperList from somewhere else).
An excerpt from my ObjectModel:
ProductTab.php
class ProductTab extends ObjectModel {
.......
public static $definition = array(
..........
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool',),
I looked up my code and I noticed that I actually called a special processing function:
AdminProductTabController.php
public function initProcess()
{
$id_product_tab = (int)Tools::getValue('id_product_tab');
$product_tab = new ProductTab($id_product_tab);
$isStatusAction = Tools::getIsset('status'.$this->table);
if ($isStatusAction)
{
$product_tab->toggleStatus();
Tools::redirectAdmin($this->href_back);
}
}
Hope this will help you out.
I've built an application to run on the client side (JavaScript & HTML) which needs to access and updated data on a server. It has a schema which consists of 5 tables. I've defined exactly what they should look like in JSON. I want these to be available as a JSON service served from a Drupal module. I understand how to use drupal_json_output to provide the results. I just can't find a simple way to ensure the database table is created for them and then to add and remove items from it. I'd like to maintain Drupal's independence from the underlying database. I don't need any search functionality, forms functionality etc. I just want to use Drupal's Database Abstraction.
At the moment I've tried the following in my install file:
/**
* Implements hook_schema
*/
function rcsarooms_schema(){
$schema = array();
$schema['rcsarooms'] = array(
'description' => 'Stores the structured information about the rooms and staircases.',
'fields' => array(
'ID' => array(
'type' => 'varchar',
'length' => 10,
'not null' => TRUE,
'description' => 'Primary Key: used in the URLs to identify the entity.'
),'Name' => array(
'type' => 'varchar',
'length' => 200,
'not null' => TRUE,
'description' => 'The name used for links in the navigation menues.'
),'ParentID' => array(
'type' => 'varchar',
'length' => 10,
'not null' => TRUE,
'description' => 'The ID of the parent element or "Root" if this is a root element'
),'Type' => array(
'type' => 'varchar',
'length' => 15,
'not null' => TRUE,
'description' => 'page, staircase, house, room or special'
),'BathroomSharing' => array(
'type' => 'int',
'description' => 'The number of people the bathroom is shared with (0 for unknown)'
),'RentBand' => array(
'type' => 'int',
'description' => 'The ID of the rent band the room is in.'
),'Floor' => array(
'type' => 'int',
'description' => 'The floor number (0 is courtyard level).'
)
),
'primary key' => array('ID')
);
return $schema;
}
And the following in my module file:
/**
* Implements hook_menu
*/
function rcsarooms_menu(){
$items['rcsarooms'] = array(
'page callback' => 'rcsarooms_callback',
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
return $items;
}
function rcsarooms_callback(){
drupal_json_output(db_query("SELECT * FROM {rcsarooms}"));
drupal_exit();
return;
}
This gives the following error when I attempt to navigate to rcsarooms:
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.rcsarooms' doesn't exist: SELECT * FROM {rcsarooms}; Array ( ) in rcsarooms_callback()
You're probably looking for hook_schema() which Drupal will use to create your custom tables when you install your module. It goes in the mymodule.install file.
The Schema API will tell you everything you need to know about data types etc.
For adding/removing items from the database use the db_insert(), db_update() and db_merge() functions
I've created a custom node type in Drupal 7, using the hook_node_info method in the install file:
// declare the new node type
function foo_node_info ( ) {
return array(
'foo' => array(
'name' => t('Foo entry'),
'base' => 'node_content',
'description' => t('For use to store foo entries.'),
));
} // END function foo_node_info
and I'm trying to save that type in the module file using the following code:
// INSERT the stuff
node_save(node_submit((object)array(
'type' => 'foo',
'is_new' => true,
'uid' => 1,
'title' => 'Title, blah blah blah',
'url' => 'url here, just pretend',
'body' => '<p>test</p>',
)));
My issue, is that the url, and body fields aren't saving. Any idea what I'm doing wrong?
So, after a ton of digging, it turns out that the way I was entering the custom fields in the node_save was wrong. The node_save needs to look like the following:
node_save(node_submit((object)array(
'type' => 'foo',
'is_new' => true,
'uid' => 1,
'title' => 'the title',
'url' => array(
'und' => array(array(
'summary' => '',
'value' => 'url value',
'format' => 2,
))),
'body' => array(
'und' => array(array(
'summary' => '',
'value' => 'the body goes here',
'format' => 2,
))),
)));
Notice that for the custom fields, the array structure has to match what was previously going on with CCK (pretty much exactly). The first key in the array describing the field value is the language for the content.
I've used 'und' here only because that's what I saw going into the database when entering the data through a form.