Any help would be brilliant.
function request_gold_pack_schema() {
$schema['request_gold_pack_customer_details'] = array(
'description' => 'Table to store all customer details.',
'fields' => array(
'rid' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'auto increment' => TRUE
),
'title' => array(
'type' => 'varchar',
'length' => 10,
'not null' => TRUE,
'default' => ''
),
'first_name' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''
),
'last_name' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''
),
'house_name_no' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''
),
'street' => array(
'type' => 'varchar',
'length' => 160,
'not null' => TRUE,
'default' => ''
),
'town' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''
),
'county' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''
),
'telephone' => array(
'type' => 'int',
'length' => 12,
'not null' => TRUE,
'default' => ''
),
'email' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => ''
),
'date_registered' => array(
'mysql_type' => 'DATETIME',
'not null' => TRUE
),
'primary' => array(
'rid'
)
)
);
return $schema;
}
which gives me the following errors
Notice: Undefined index: type in DatabaseSchema_mysql->processField() (line 205 of /Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/mysql/schema.inc).
Notice: Undefined index: :normal in DatabaseSchema_mysql->processField() (line 205 of /Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/mysql/schema.inc).
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'Table to stor' at line 13: CREATE TABLE {request_gold_pack_customer_details} ( rid INT NOT NULL DEFAULT 0, title VARCHAR(10) NOT NULL DEFAULT '', first_name VARCHAR(50) NOT NULL DEFAULT '', last_name VARCHAR(50) NOT NULL DEFAULT '', house_name_no VARCHAR(50) NOT NULL DEFAULT '', street VARCHAR(160) NOT NULL DEFAULT '', town VARCHAR(50) NOT NULL DEFAULT '', county VARCHAR(50) NOT NULL DEFAULT '', telephone INT NOT NULL DEFAULT '', email VARCHAR(255) NOT NULL DEFAULT '', date_registered DATETIME NOT NULL, primary DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'Table to store all customer details.'; Array ( ) in db_create_table() (line 2688 of /Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/database.inc).
Been trying to find a solution for hours.
Thanks.
It is an issue with your primary key. You hve it listed in the fields array (it should not be) and it should be referenced as 'primary key' and not 'primary', like so:
function request_gold_pack_schema(){
$schema['request_gold_pack_customer_details'] = array(
'description' => 'Table to store all customer details.',
'fields' => array(
// Your field definitions
),
'primary key' => array(
'rid'
)
);
return $schema;
}
Check out the schema api documentation on drupal.org.
I would also recommend setting the rid field to type serial and leaving off the auto increment parameter (Drupal will handle that). So the 'rid' field definitions would be as such:
'rid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
)
You have to specify the size in drupal 7 for type 'int'...i.e
'rid' => array(
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
'auto increment' => TRUE,
),
the size can be normal,tiny,big
check drupal 7 datatypes.
Related
Could someone tell me if I've missed something in this? It keeps throwing the error mentioned at the bottom after the code block.
function license_update_7101() {
$schema['license_agreements'] = array(
'description' => 'License User Agreements',
'fields' => array(
'license_id' => array(
'description' => 'Primary key for license records',
'type' => 'serial',
'not null' => TRUE,
'unsigned' => TRUE
),
'user_id' => array(
'description' => 'License agreed to by User ID',
'type' => 'int',
'not null' => FALSE,
'unsigned' => TRUE,
'default' => '0'
),
'product_id' => array(
'description' => 'Product ID that represents the product this agreement was assigned to.',
'type' => 'int',
'not null' => FALSE,
'unsigned' => TRUE,
'default' => '0'
),
'session' => array(
'description' => 'Session created during account creation.',
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
'default' => ''
),
'agreed' => array(
'description' => 'Represents if the license was agreed to or not',
'type' => 'int',
'size' => 'tiny',
'not null' => FALSE,
'unsigned' => TRUE,
'default' => '0'
),
'agreement_date' => array(
'description' => 'Date this license was agreed to by user.',
'type' => 'datetime',
'mysql_type' => 'DATETIME',
'disp-width' => '11',
'not null' => FALSE,
'default' => NULL
),
'blog_name' => array(
'description' => 'URL for actual product license belongs to.',
'type' => 'varchar',
'length' => '128',
'not null' => FALSE,
'default' => ''
),
'primary key' => array('license_id')
)
);
db_create_table('license_agreements', $schema['license_agreements']);
}
ERROR:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB [error]
server version for the right syntax to use near 'DEFAULT NULL
) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'License' at line 9
Really would love to get this solved.
Thanks!
So, courtesy of having another dev come help me try to find what was blowing things up, I managed to discover the issue as we worked through it.
'primary key' => array('license_id')
I ended up noticing that that was embedded within the fields array and needed to be outside/after it.
So, the fix was as simple as moving that line down as an array element of the license_agreements array. Problem solved!
I'm new to CakePHP and PHPUnit and I want to test one of my controllers. So far I have this method I like to test:
public function getAlternatives($id = null){
// ID parameter given or is NaN?
if (!$id || !is_numeric($id)) {
throw new BadRequestException();
}
// Error if object not found
if (!$this->Designitem->exists($id)) {
throw new NotFoundException();
}
$alternatives = $this->Designitem->getItemsWithFunritures($id);
$this->set(array(
'alternatives' => $alternatives,
'_serialize' => array('alternatives'),
'_jsonp' => true));
}
Now I want to test whether the getItemsWithFurnitures is called on my model Designitem. To test this I implemented this test:
public function testGetAlternativesWithValidID() {
$id = 1;
$url = array('controller' => 'Designitems', 'action' => 'getAlternatives', $id . '.json');
$expected = array(
'Designitem' =>
array(
'id' => 1,
'design_id' => 1,
'furniture_id' => 1,
'template_id' => 1,
),
);
$designitems = $this->generate('Designitems', array(
'methods' => array(
'getAlternatives',
),
'models' => array(
'Designitem' => array('getItemsWithFunritures'),
),
));
$designitems->Designitem
->expects($this->once())
->method('getItemsWithFunritures')
->will($this->returnValue($expected));
$result = $this->testAction($url);
$this->assertEquals(json_encode($expected), $result);
}
EDIT: My Designitems Fixture contains one stubbed entry. It looks like this:
class DesignitemFixture extends CakeTestFixture {
public $fields = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false, 'key' => 'primary'),
'design_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false),
'furniture_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false),
'template_id' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'templateitem_id' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'amount' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 5, 'unsigned' => false),
'feedback' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 20, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'clicks' => array('type' => 'integer', 'null' => true, 'default' => '0', 'unsigned' => false),
'ilike' => array('type' => 'boolean', 'null' => false, 'default' => null),
'dislike' => array('type' => 'boolean', 'null' => false, 'default' => null),
'dislike_reason' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 100, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'dislike_comment' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 2000, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'parent_id' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'has_alternatives' => array('type' => 'boolean', 'null' => true, 'default' => null),
'is_selected' => array('type' => 'boolean', 'null' => true, 'default' => null),
'is_recommendation' => array('type' => 'boolean', 'null' => true, 'default' => null),
'deleted' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 4, 'unsigned' => false),
'deleted_date' => array('type' => 'datetime', 'null' => true, 'default' => null),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1)
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);
public $records = array(
array(
'id' => 1,
'design_id' => 1,
'furniture_id' => 1,
'template_id' => 1,
'templateitem_id' => 1,
'amount' => 1,
'feedback' => 'Lorem ipsum dolor ',
'clicks' => 1,
'ilike' => 1,
'dislike' => 1,
'dislike_reason' => 'Lorem ipsum dolor sit amet',
'dislike_comment' => 'Lorem ipsum dolor sit amet',
'parent_id' => 1,
'has_alternatives' => 1,
'is_selected' => 1,
'is_recommendation' => 1,
'deleted' => 0,
'deleted_date' => '2016-07-14 12:05:39',
'created' => '2016-07-14 12:05:39',
'modified' => '2016-07-14 12:05:39'
),
);
}
When I run the test, I get an error that the getItemsWithFurnitures returned null:
Test result
Can anyone tell me what I'm doing wrong? Thank you!
I found a solution to the problem. My problem was that not the getItemsWithFurnitures returned null but the tested method getAlternatives isn't returning any value (-> null). So I have to check if the set value $this->set(...); is the same as the expected array. To achieve this I have to change the return method of the testAction(...) by passing array('return' => 'contents'). It will then return the produced content instead of the methods return value. The working test looks like this:
public function testGetAlternativesWithValidID() {
$id = 1;
$url = array('controller' => 'Designitems', 'action' => 'getAlternatives', $id . '.json');
$this->_generateMockWithAuthUserId('Designitems', 1);
$expected = array(
'Designitem' =>
array(
'id' => 1,
'design_id' => 1,
'furniture_id' => 1,
'template_id' => 1,
),
);
$data = $this->getMockForModel('Designitem', array('getItemsWithFunritures'));
$data->expects($this->once())
->method('getItemsWithFunritures')
->will($this->returnValue($expected));
$result = $this->testAction($url, array('return' => 'contents'));
// Delete all whitespaces from the result.
$result = preg_replace('/\s+/', '', $result);
// Add the surrounding alternatives array to the expected and convert to json-format.
$expected = json_encode(array('alternatives' => $expected));
$this->assertEquals($expected, $result);
}
Because I add these items into another array 'alternatives' while setting it to the view, I have to place the expected array in this 'alternatives'-array. I also had to replace all whitespaces because the json encoding methods used are different. In order to compare them they have to be exactly the same so I removed the whitespaces and the test passes.
I hope someone can use this info.
Hi i am very new in drupal. i just create a module that create a form with some inputs. I wrote .install file for the same but it is not creating table. While i am installing this module it is neither showing error nor install table.
function mycontact_schema() {
$schema['mycontact'] = array(
'description' => t('This table for mycontact.'),
'fields' => array(
'mycontctid' => array(
'description' => t('The primary identifier for a mycontact.'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE),
'vid' => array(
'description' => t('The current {mycontact_revisions}.vid version identifier.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'name' => array(
'description' => t('The {mycontact_name} of this mycontact.'),
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => ''),
'email' => array(
'description' => t('The name of this contact, always treated a non-markup plain text.'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => ''),
),
'comments' => array(
'description' => t('The comments of this contact, always treated a non-markup plain text.'),
'type' => 'text',
'not null' => TRUE,
'default' => ''),
'primary key' => array('mycontctid'),
);
return $schema;
}
It is not showing any error and any warning.
There is nothing wrong in the hook_install implementation.
I followed these steps to get it working:
Created a directory called mycontact inside sites/all/modules.
Created a the files mycontact.info, mycontact.module, and mycontact.install.
Inside mycontact.info I added the following lines:
name = my contact
core = 7.x
And in the mycontact.install file, I copied all the code from the question as it is.
Left the mycontact.module file empty. Note: Drupal needs this empty file.
Enabled the module. And it worked!
What you could do now is, disable the module -> uninstall it -> follow the above steps to install the module; and you should have your database table created for you.
I want to display the commentairs on the page of the article, my comments are submitted by users.
Currently I am able to display the comments but not the users associated with comments.
SQL:
posts
CREATE TABLE `posts` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
`slug` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
`content` longtext CHARACTER SET utf8 NOT NULL,
`created` datetime NOT NULL,
`active` tinyint(1) DEFAULT '0',
`picture` tinyint(1) DEFAULT NULL,
`type` enum('Article','News','Page') CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
post_comments
CREATE TABLE `post_comments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`post_id` int(11) DEFAULT NULL,
`user_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL DEFAULT '',
`content` text NOT NULL,
`created` datetime NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
--
$post = $this->Post->find('first', array(
'conditions' => array('Post.slug' => $slug, 'Post.active' => '1'),
'contain' => array('PostComment')
));
--
debug($post);
array(
'Post' => array(
'id' => '2',
'name' => 'azeaeaez',
'slug' => 'azeaeaez1',
'content' => 'aeaeaeaze',
'created' => '2013-10-21 12:33:30',
'active' => true,
'picture' => null,
'type' => 'News',
'user_id' => null
),
'PostComment' => array(
(int) 0 => array(
'id' => '4',
'post_id' => '2',
'user_id' => '6',
'title' => 'Super comme article',
'content' => 'Merci c'est cool j'en prend note !',
'created' => '2013-10-29 08:58:07',
'status' => false
),
(int) 1 => array(
'id' => '3',
'post_id' => '2',
'user_id' => '31',
'title' => 'Super cool',
'content' => 'merci les loulous',
'created' => '2013-10-26 17:09:21',
'status' => false
)
)
)
the desired result
array(
'Post' => array(
'id' => '2',
'name' => 'azeaeaez',
'slug' => 'azeaeaez1',
'content' => 'aeaeaeaze',
'created' => '2013-10-21 12:33:30',
'active' => true,
'picture' => null,
'type' => 'News',
'user_id' => null
),
'PostComment' => array(
(int) 0 => array(
'id' => '4',
'post_id' => '2',
'user' => array(
'user_name' => 'toto',
'email' => tot#email.com,
...
),
'title' => 'Super comme article',
'content' => 'Merci c'est cool j'en prend note !',
'created' => '2013-10-29 08:58:07',
'status' => false
),
(int) 1 => array(
'id' => '3',
'post_id' => '2',
'user' => array(
'user_name' => 'toto',
'email' => tot#email.com,
...
),
'title' => 'Super cool',
'content' => 'merci les loulous',
'created' => '2013-10-26 17:09:21',
'status' => false
)
)
)
try
'contain' => array('PostComment', 'PostComment.User')
(I assume that you have set the relationship between PostComment and User)
You'll need
$post = $this->Post->find('first', array(
'conditions' => array('Post.slug' => $slug, 'Post.active' => '1'),
'contain' => array('PostComment' => 'User')
));
And you need to set up the proper association between the User and PostComment models.
I'm having issues with using the schema module and a table I created. I used schema to create the schema and then when I tried to install the module logo_management, I get this error:
Invalid default value for 'logoid':
CREATE TABLE {logo} ( `logoid` INT unsigned NULL auto_increment DEFAULT
0 COMMENT 'Unique Logo ID', `date_created` NULL DEFAULT 0 COMMENT
'The date the logo was added.', `category` VARCHAR(50) NULL DEFAULT
'OTHER' COMMENT 'The category the logo should be in.', `no_of_lines`
INT NULL DEFAULT 1 COMMENT 'Number of lines(1 or 2)',
`line_1` VARCHAR(100) NULL DEFAULT NULL COMMENT 'Line 1 for Logo',
`line_2` VARCHAR(100) NULL DEFAULT NULL COMMENT 'Line 2 for Logo',
`image_path` VARCHAR(100) NULL DEFAULT NULL COMMENT
'Path and filename for image', `activate`
INT NULL DEFAULT 0 COMMENT 'Boolean if the logo should be active or not.',
PRIMARY KEY (`logoid`) ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT
Here is the .install schema I have. I'm assuming it's something wrong with the Invalid default value for 'logoid', however I tried putting the default value and it still gives me the same error.
function logo_management_schema() {
$schema['logo'] = array(
'description' => 'Logo Management Module',
'fields' => array(
'logoid' => array(
'description' => 'Unique Logo ID',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'date_created' => array(
'description' => 'The date the logo was added.',
'type' => 'date',
'not null' => TRUE,
),
'category' => array(
'description' => 'The category the logo should be in.',
'type' => 'varchar',
'length' => '50',
'not null' => TRUE,
'default' => 'OTHER',
),
'no_of_lines' => array(
'description' => 'Number of lines(1 or 2)',
'type' => 'int',
'not null' => TRUE,
'default' => 1,
),
'line_1' => array(
'description' => 'Line 1 for Logo',
'type' => 'varchar',
'length' => '100',
'not null' => TRUE,
),
'line_2' => array(
'description' => 'Line 2 for Logo',
'type' => 'varchar',
'length' => '100',
'not null' => FALSE,
),
'image_path' => array(
'description' => 'Path and filename for image',
'type' => 'varchar',
'length' => '100',
'not null' => TRUE,
),
'activate' => array(
'description' => 'Boolean if the logo should be active or not.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('logoid'),
);
return $schema;
}
Skill and determination fixed this problem! Changed the type to int.
$schema['logo'] = array(
'description' => 'Logo Management Module',
'fields' => array(
'logoid' => array(
'description' => 'Unique Logo ID',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
)
Then I ran into a date problem and changed it to this:
'date_created' => array(
'description' => 'The date the logo was added.',
'type' => 'datetime',
'mysql_type' => 'DATETIME',
'not null' => TRUE,
),
Now my module installs!