Codeigniter Create a database and table outside of config/database.php - php

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.

Related

Drupal 7 Create Flood Table PHP

Because of a long series of events, I thought I lost a site completely due to my hosting company losing a server. Anyway, I'm trying to get back into a site sitting on a server with a cpanel I cant get to and a WHM I cant get to. I only have access to the root dir via ftp. I was able to reset my admin password using the method here https://www.drupal.org/node/1556488
I meant to also clear the flood table, but deleted it instead with db_drop_table('flood');
So, now I am getting a MYSQL error when I try to login. So, given I only have access to running php scripts to interact with the SQL database, can anyone help me figure out how to rebuild the flood table via the drupal db_create_table() method?
Much appreciated! This has been a nightmare.
I was able to rebuild my flood table with a php file ran from the drupal install directory with this in it: Just type in the url mysite.com/myphpfile.php to run it.
<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
require_once DRUPAL_ROOT . '/includes/password.inc';
$schema = array();
$schema['flood'] = array(
'fields' => array(
'fid' => array(
'type' => 'int',
'length' => 11,
'not null' => TRUE,
),
'event' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
),
'identifier' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'timestamp' => array(
'type' => 'int',
'length' => 11,
'not null' => TRUE,
),
'expiration' => array(
'type' => 'int',
'length' => 11,
'not null' => TRUE,
),
),
'primary key' => array('fid'),
);
db_create_table('flood', $schema['flood']);
print "Done. Please delete this file immediately!";
drupal_exit();
?>

Magento Category Flat Tables

I have added to the category entity a lot of custom attributes, now when I try to reindex I get the error
SQLSTATE[42000]: Syntax error or access violation: 1118 Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs
I Know that means that the flat tables are too large and indeed there are a lot of columns there. I don't need my custom attributes in that table so I can remove them, but how can i do that? I found that if I set the filterable and comparable to false they shouldn't be in the flat table. Any help will be appreciated
public function getDefaultEntities()
{
return array(
'catalog_category' => array(
'entity_model' => 'catalog/category',
'attribute_model' => 'catalog/resource_eav_attribute',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/category_attribute_collection',
'table' => 'catalog/category',
'attributes' => array(
'cat_type' => array(
'group' => 'General',
'label' => 'Category Type',
'type' => 'int',
'input' => 'select',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => 'eav/entity_attribute_source_cattype',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false
),
Edit:
I have deleted the attributes and it works then I ran the install script again, all of them have the filterable and comparable set to false as the example I posted below, It shows me the error again, What I'm missing?
Edit:
I checked the flat category table and I see that all the attributes are added them.
So the filterable and comparable just works for product attributes?
I thought that they will be the same values for category products.
Is there anyway to exclude my attributes from that table?
I can exclude them modifying the file that create the table, obviously in my local folder but I want to know which is the clean way to do this?
Check eav_attribute table and column named used_in_product_listing set it to 0 or false and it shouldn't be added to category product flat table.

File property in an drupal entity class?

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.

Can CakePHP generate my database tables from the _schema attribute on the Model?

A common task for me when I'm writing CakePHP applications is to type out an SQL file and write it into the database before running bake to generate some scaffolding. This is one of the very few gripes I have with CakePHP - doing this ties me into MySQL, and I'm wondering if there's a better way to do it through code. As an example, in some frameworks I can define the columns that my model uses along with the datatype and such, and then run a command through an admin interface to "build" the database based on what what's presented in the code. It will do this on whatever database is sitting behind the framework.
Is there a way for CakePHP 2.x can do something like this? I want to write out the database schema in my Model code and run a command like bake to automatically generate the tables and columns that I need. After diving into the cookbook docs, the _schema attribute seems to do what I want to do:
class Post{
public $_schema = array(
'title' => array('type'=>'text'),
'description' => array('type'=>'text'),
'author' => array('type'=>'text')
);
}
but there are no examples explaining what I would I do from there. Does the _schema attribute serve a different purpose? Any help would be appreciated!
not from your $_schema array itself. but creating and using a schema file schema.php in /APP/Config/Schema.
you can then run the bake command "cake schema create" which will then "Drop and create tables based on the schema file."
I might then look sth like this:
class YourSchema extends CakeSchema {
public $addresses = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 10, 'key' => 'primary'),
'contact_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 10),
'type' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 2),
'status' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 2),
'email' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 50, 'collate' => 'utf8_unicode_ci', 'comment' => 'redundant', 'charset' => 'utf8'),
'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_unicode_ci', 'engine' => 'MyISAM')
)
// more tables...
}

How to define and perform CRUD on custom entities in drupal

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

Categories