本文整理汇总了PHP中Cake\Database\Schema\Table::primaryKey方法的典型用法代码示例。如果您正苦于以下问题:PHP Table::primaryKey方法的具体用法?PHP Table::primaryKey怎么用?PHP Table::primaryKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Database\Schema\Table
的用法示例。
在下文中一共展示了Table::primaryKey方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _generateRecords
/**
* Generate String representation of Records
*
* @param \Cake\Database\Schema\Table $table Table schema array
* @param int $recordCount The number of records to generate.
* @return array Array of records to use in the fixture.
*/
protected function _generateRecords(Table $table, $recordCount = 1)
{
$records = [];
for ($i = 0; $i < $recordCount; $i++) {
$record = [];
foreach ($table->columns() as $field) {
$fieldInfo = $table->column($field);
$insert = '';
switch ($fieldInfo['type']) {
case 'decimal':
$insert = $i + 1.5;
break;
case 'biginteger':
case 'integer':
case 'float':
$insert = $i + 1;
break;
case 'string':
case 'binary':
$isPrimary = in_array($field, $table->primaryKey());
if ($isPrimary) {
$insert = Text::uuid();
} else {
$insert = "Lorem ipsum dolor sit amet";
if (!empty($fieldInfo['length'])) {
$insert = substr($insert, 0, (int) $fieldInfo['length'] - 2);
}
}
break;
case 'timestamp':
$insert = time();
break;
case 'datetime':
$insert = date('Y-m-d H:i:s');
break;
case 'date':
$insert = date('Y-m-d');
break;
case 'time':
$insert = date('H:i:s');
break;
case 'boolean':
$insert = 1;
break;
case 'text':
$insert = "Lorem ipsum dolor sit amet, aliquet feugiat.";
$insert .= " Convallis morbi fringilla gravida,";
$insert .= " phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin";
$insert .= " venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla";
$insert .= " vestibulum massa neque ut et, id hendrerit sit,";
$insert .= " feugiat in taciti enim proin nibh, tempor dignissim, rhoncus";
$insert .= " duis vestibulum nunc mattis convallis.";
break;
case 'uuid':
$insert = Text::uuid();
break;
}
$record[$field] = $insert;
}
$records[] = $record;
}
return $records;
}
示例2: columnSql
/**
* {@inheritDoc}
*/
public function columnSql(Table $table, $name)
{
$data = $table->column($name);
$out = $this->_driver->quoteIdentifier($name);
$typeMap = ['boolean' => ' BOOLEAN', 'binary' => ' BYTEA', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'text' => ' TEXT', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' TIMESTAMP', 'timestamp' => ' TIMESTAMP', 'uuid' => ' UUID'];
if (isset($typeMap[$data['type']])) {
$out .= $typeMap[$data['type']];
}
if ($data['type'] === 'integer' || $data['type'] === 'biginteger') {
$type = $data['type'] === 'integer' ? ' INTEGER' : ' BIGINT';
if ([$name] === $table->primaryKey() || $data['autoIncrement'] === true) {
$type = $data['type'] === 'integer' ? ' SERIAL' : ' BIGSERIAL';
unset($data['null'], $data['default']);
}
$out .= $type;
}
if ($data['type'] === 'string') {
$isFixed = !empty($data['fixed']);
$type = ' VARCHAR';
if ($isFixed) {
$type = ' CHAR';
}
$out .= $type;
if (isset($data['length']) && $data['length'] != 36) {
$out .= '(' . (int) $data['length'] . ')';
}
}
if ($data['type'] === 'float' && isset($data['precision'])) {
$out .= '(' . (int) $data['precision'] . ')';
}
if ($data['type'] === 'decimal' && (isset($data['length']) || isset($data['precision']))) {
$out .= '(' . (int) $data['length'] . ',' . (int) $data['precision'] . ')';
}
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
if (isset($data['null']) && $data['null'] === true) {
$out .= ' DEFAULT NULL';
unset($data['default']);
}
if (isset($data['default']) && $data['type'] !== 'timestamp') {
$defaultValue = $data['default'];
if ($data['type'] === 'boolean') {
$defaultValue = (bool) $defaultValue;
}
$out .= ' DEFAULT ' . $this->_driver->schemaValue($defaultValue);
}
return $out;
}
示例3: columnSql
/**
* {@inheritDoc}
*
* @throws \Cake\Database\Exception when the column type is unknown
*/
public function columnSql(Table $table, $name)
{
$data = $table->column($name);
$typeMap = ['uuid' => ' CHAR(36)', 'string' => ' VARCHAR', 'integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BOOLEAN', 'binary' => ' BLOB', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'text' => ' TEXT', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' DATETIME', 'timestamp' => ' TIMESTAMP'];
if (!isset($typeMap[$data['type']])) {
throw new Exception(sprintf('Unknown column type for "%s"', $name));
}
$out = $this->_driver->quoteIdentifier($name);
$hasUnsigned = ['biginteger', 'integer', 'float', 'decimal'];
if (in_array($data['type'], $hasUnsigned, true) && isset($data['unsigned']) && $data['unsigned'] === true) {
$out .= ' UNSIGNED';
}
$out .= $typeMap[$data['type']];
$hasLength = ['integer', 'string'];
if (in_array($data['type'], $hasLength, true) && isset($data['length'])) {
$out .= '(' . (int) $data['length'] . ')';
}
$hasPrecision = ['float', 'decimal'];
if (in_array($data['type'], $hasPrecision, true) && (isset($data['length']) || isset($data['precision']))) {
$out .= '(' . (int) $data['length'] . ',' . (int) $data['precision'] . ')';
}
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
if ($data['type'] === 'integer' && [$name] === (array) $table->primaryKey()) {
$out .= ' PRIMARY KEY AUTOINCREMENT';
}
if (isset($data['null']) && $data['null'] === true) {
$out .= ' DEFAULT NULL';
unset($data['default']);
}
if (isset($data['default'])) {
$out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
}
return $out;
}
示例4: columnSql
/**
* {@inheritDoc}
*/
public function columnSql(Table $table, $name)
{
$data = $table->column($name);
$out = $this->_driver->quoteIdentifier($name);
$typeMap = ['integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BOOLEAN', 'binary' => ' LONGBLOB', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'text' => ' TEXT', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' DATETIME', 'timestamp' => ' TIMESTAMP', 'uuid' => ' CHAR(36)'];
$specialMap = ['string' => true];
if (isset($typeMap[$data['type']])) {
$out .= $typeMap[$data['type']];
}
if (isset($specialMap[$data['type']])) {
switch ($data['type']) {
case 'string':
$out .= !empty($data['fixed']) ? ' CHAR' : ' VARCHAR';
if (!isset($data['length'])) {
$data['length'] = 255;
}
break;
}
}
$hasLength = ['integer', 'string'];
if (in_array($data['type'], $hasLength, true) && isset($data['length'])) {
$out .= '(' . (int) $data['length'] . ')';
}
$hasPrecision = ['float', 'decimal'];
if (in_array($data['type'], $hasPrecision, true) && (isset($data['length']) || isset($data['precision']))) {
$out .= '(' . (int) $data['length'] . ',' . (int) $data['precision'] . ')';
}
$hasUnsigned = ['float', 'decimal', 'integer', 'biginteger'];
if (in_array($data['type'], $hasUnsigned, true) && isset($data['unsigned']) && $data['unsigned'] === true) {
$out .= ' UNSIGNED';
}
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
if (in_array($data['type'], ['integer', 'biginteger']) && ([$name] == (array) $table->primaryKey() || $data['autoIncrement'] === true)) {
$out .= ' AUTO_INCREMENT';
}
if (isset($data['null']) && $data['null'] === true) {
$out .= $data['type'] === 'timestamp' ? ' NULL' : ' DEFAULT NULL';
unset($data['default']);
}
if (isset($data['default']) && $data['type'] !== 'timestamp') {
$out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
}
if (isset($data['default']) && $data['type'] === 'timestamp' && strtolower($data['default']) === 'current_timestamp') {
$out .= ' DEFAULT CURRENT_TIMESTAMP';
}
if (isset($data['comment']) && $data['comment'] !== '') {
$out .= ' COMMENT ' . $this->_driver->schemaValue($data['comment']);
}
return $out;
}
示例5: columnSql
/**
* {@inheritDoc}
*/
public function columnSql(Table $table, $name)
{
$data = $table->column($name);
$out = $this->_driver->quoteIdentifier($name);
$typeMap = ['integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BIT', 'binary' => ' BINARY', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'text' => ' NVARCHAR(MAX)', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' DATETIME', 'timestamp' => ' DATETIME', 'uuid' => ' UNIQUEIDENTIFIER'];
if (isset($typeMap[$data['type']])) {
$out .= $typeMap[$data['type']];
}
if ($data['type'] === 'integer' || $data['type'] === 'biginteger') {
if ([$name] === $table->primaryKey() || $data['autoIncrement'] === true) {
unset($data['null'], $data['default']);
$out .= ' IDENTITY(1, 1)';
}
}
if ($data['type'] === 'string') {
$type = ' NVARCHAR';
if (!empty($data['fixed'])) {
$type = ' NCHAR';
}
if (!isset($data['length'])) {
$data['length'] = 255;
}
$out .= sprintf('%s(%d)', $type, $data['length']);
}
if ($data['type'] === 'float' && isset($data['precision'])) {
$out .= '(' . (int) $data['precision'] . ')';
}
if ($data['type'] === 'decimal' && (isset($data['length']) || isset($data['precision']))) {
$out .= '(' . (int) $data['length'] . ',' . (int) $data['precision'] . ')';
}
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
if (isset($data['null']) && $data['null'] === true) {
$out .= ' DEFAULT NULL';
unset($data['default']);
}
if (isset($data['default']) && $data['type'] !== 'datetime') {
$default = is_bool($data['default']) ? (int) $data['default'] : $this->_driver->schemaValue($data['default']);
$out .= ' DEFAULT ' . $default;
}
return $out;
}
示例6: truncateTableSql
/**
* {@inheritDoc}
*/
public function truncateTableSql(Table $table)
{
$name = $this->_driver->quoteIdentifier($table->name());
$queries = [sprintf('DELETE FROM %s', $name)];
// Restart identity sequences
$pk = $table->primaryKey();
if (count($pk) === 1) {
$column = $table->column($pk[0]);
if (in_array($column['type'], ['integer', 'biginteger'])) {
$queries[] = sprintf('DBCC CHECKIDENT(%s, RESEED, 0)', $name);
}
}
return $queries;
}
示例7: testPrimaryKey
/**
* Test getting the primary key.
*
* @return void
*/
public function testPrimaryKey()
{
$table = new Table('articles');
$table->addColumn('id', 'integer')->addColumn('title', 'string')->addColumn('author_id', 'integer')->addConstraint('author_idx', ['columns' => ['author_id'], 'type' => 'unique'])->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
$this->assertEquals(['id'], $table->primaryKey());
$table = new Table('articles');
$table->addColumn('id', 'integer')->addColumn('title', 'string')->addColumn('author_id', 'integer');
$this->assertEquals([], $table->primaryKey());
}
示例8: columnSql
/**
* {@inheritDoc}
*/
public function columnSql(Table $table, $name)
{
$data = $table->column($name);
$out = $this->_driver->quoteIdentifier($name);
$nativeJson = $this->_driver->supportsNativeJson();
$typeMap = ['integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BOOLEAN', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' DATETIME', 'timestamp' => ' TIMESTAMP', 'uuid' => ' CHAR(36)', 'json' => $nativeJson ? ' JSON' : ' LONGTEXT'];
$specialMap = ['string' => true, 'text' => true, 'binary' => true];
if (isset($typeMap[$data['type']])) {
$out .= $typeMap[$data['type']];
}
if (isset($specialMap[$data['type']])) {
switch ($data['type']) {
case 'string':
$out .= !empty($data['fixed']) ? ' CHAR' : ' VARCHAR';
if (!isset($data['length'])) {
$data['length'] = 255;
}
break;
case 'text':
$isKnownLength = in_array($data['length'], Table::$columnLengths);
if (empty($data['length']) || !$isKnownLength) {
$out .= ' TEXT';
break;
}
if ($isKnownLength) {
$length = array_search($data['length'], Table::$columnLengths);
$out .= ' ' . strtoupper($length) . 'TEXT';
}
break;
case 'binary':
$isKnownLength = in_array($data['length'], Table::$columnLengths);
if (empty($data['length']) || !$isKnownLength) {
$out .= ' BLOB';
break;
}
if ($isKnownLength) {
$length = array_search($data['length'], Table::$columnLengths);
$out .= ' ' . strtoupper($length) . 'BLOB';
}
break;
}
}
$hasLength = ['integer', 'string'];
if (in_array($data['type'], $hasLength, true) && isset($data['length'])) {
$out .= '(' . (int) $data['length'] . ')';
}
$hasPrecision = ['float', 'decimal'];
if (in_array($data['type'], $hasPrecision, true) && (isset($data['length']) || isset($data['precision']))) {
$out .= '(' . (int) $data['length'] . ',' . (int) $data['precision'] . ')';
}
$hasUnsigned = ['float', 'decimal', 'integer', 'biginteger'];
if (in_array($data['type'], $hasUnsigned, true) && isset($data['unsigned']) && $data['unsigned'] === true) {
$out .= ' UNSIGNED';
}
$hasCollate = ['text', 'string'];
if (in_array($data['type'], $hasCollate, true) && isset($data['collate']) && $data['collate'] !== '') {
$out .= ' COLLATE ' . $data['collate'];
}
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
$addAutoIncrement = [$name] == (array) $table->primaryKey() && !$table->hasAutoIncrement();
if (in_array($data['type'], ['integer', 'biginteger']) && ($data['autoIncrement'] === true || $addAutoIncrement)) {
$out .= ' AUTO_INCREMENT';
}
if (isset($data['null']) && $data['null'] === true && $data['type'] === 'timestamp') {
$out .= ' NULL';
unset($data['default']);
}
if (isset($data['default']) && in_array($data['type'], ['timestamp', 'datetime']) && strtolower($data['default']) === 'current_timestamp') {
$out .= ' DEFAULT CURRENT_TIMESTAMP';
unset($data['default']);
}
if (isset($data['default'])) {
$out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
unset($data['default']);
}
if (isset($data['comment']) && $data['comment'] !== '') {
$out .= ' COMMENT ' . $this->_driver->schemaValue($data['comment']);
}
return $out;
}