Ejemplo n.º 1
0
 /**
  * Returns the fields to serialize
  * @return array Array with field names
  */
 public function __sleep()
 {
     $fields = parent::__sleep();
     if ($this->label) {
         $fields[] = 'label';
     }
     if ($this->isLocalized) {
         $fields[] = 'isLocalized';
     }
     if ($this->validators) {
         $fields[] = 'validators';
     }
     return $fields;
 }
Ejemplo n.º 2
0
 public function testGetPrimaryKeys()
 {
     $table = new Table('table');
     $field1 = new Field('field1', 'type');
     $field1->setIsPrimaryKey(true);
     $field2 = new Field('field2', 'type');
     $field2->setIsPrimaryKey(true);
     $field3 = new Field('field3', 'type');
     $table->addField($field1);
     $table->addField($field2);
     $table->addField($field3);
     $primaryKeys = $table->getPrimaryKeys();
     $this->assertEquals(array('field1' => $field1, 'field2' => $field2), $primaryKeys);
 }
Ejemplo n.º 3
0
 /**
  * Gets the default value of a field
  * @param Field $field
  * @return string SQL of the default value
  */
 protected function getDefaultValue(Field $field)
 {
     $default = $field->getDefaultValue();
     if ($default == null || strtoupper($default) == 'NULL') {
         return 'NULL';
     }
     return $this->connection->quoteValue($default);
 }
Ejemplo n.º 4
0
 /**
  * Translates a database layer's field type to a mysql field type
  * @param string|zibo\library\database\definition\Field $field field can be a database layer's type or a Field object
  * @return string mysql field type
  * @throws zibo\library\database\exception\DatabaseException when no type found for the provided field or type
  */
 protected function getFieldType($field)
 {
     $fieldTypes = $this->getFieldTypes();
     if ($field instanceof Field) {
         $fieldType = $field->getType();
         if (!isset($fieldTypes[$fieldType])) {
             throw new DatabaseException('No database type found for type ' . $fieldType);
         }
         return $fieldTypes[$fieldType];
     }
     $type = array_search($field, $fieldTypes);
     if ($type === false) {
         throw new DatabaseException('No type found for database type ' . $field);
     }
     return $type;
 }
Ejemplo n.º 5
0
 /**
  * Sets or adds a new field definition
  * @param Field $field Definition of the field to set or add
  * @return null
  */
 public function setField(Field $field)
 {
     $this->fields[$field->getName()] = $field;
 }
Ejemplo n.º 6
0
 /**
  * Gets the foreign keys of a table
  * @param string $name Name of the table
  * @return array Array with ForeignKey objects
  */
 protected function getTableFields($name)
 {
     $tableName = $this->connection->quoteIdentifier($name);
     $sql = 'SHOW FIELDS FROM ' . $tableName;
     $result = $this->connection->execute($sql);
     if (!$result) {
         throw new MysqlException('Could not find any fields for table ' . $name . '. Does it exist?');
     }
     $fields = array();
     foreach ($result as $data) {
         $field = new Field($data['Field'], $data['Type']);
         $field->setDefaultValue($data['Default']);
         $fields[] = $field;
         if (!$data['Key']) {
             continue;
         }
         if ($data['Key'] == 'PRI') {
             $field->setIsPrimaryKey(true);
             if ($data['Extra']) {
                 $field->setIsAutoNumbering(true);
                 $field->setDefaultValue(0);
             }
         } elseif ($data['Key'] == 'UNI') {
             $field->setIsUnique(true);
         }
     }
     return $fields;
 }
Ejemplo n.º 7
0
 /**
  * @expectedException zibo\library\database\exception\DatabaseException
  */
 public function testSetIsIndexedThrowsExceptionWhenNoBooleanPassed()
 {
     $field = new Field('name', 'type');
     $field->setIsIndexed('test');
 }