Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readonly column property for generated/virtual columns #5728

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/Doctrine/ORM/Mapping/Builder/FieldBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ public function scale($s)
return $this;
}

/**
* Sets readOnly.
*
* @param bool $flag
*
* @return FieldBuilder
*/
public function readOnly($flag = true)
{
$this->mapping['readOnly'] = (bool) $flag;

return $this;
}

/**
* Sets field as primary key.
*
Expand Down
21 changes: 21 additions & 0 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@ class ClassMetadataInfo implements ClassMetadata
* - <b>nullable</b> (boolean, optional)
* Whether the column is nullable. Defaults to FALSE.
*
* - <b>readOnly</b> (boolean, optional)
* Whether the column is readOnly. Defaults to FALSE.
*
* - <b>columnDefinition</b> (string, optional, schema-only)
* The SQL fragment that is used when generating the DDL for the column.
*
Expand Down Expand Up @@ -1203,6 +1206,24 @@ public function isNullable($fieldName)
return false;
}

/**
* Checks if the field is readOnly.
*
* @param string $fieldName The field name.
*
* @return boolean TRUE if the field is readOnly, FALSE otherwise.
*/
public function isReadOnly($fieldName)
{
$mapping = $this->getFieldMapping($fieldName);

if ($mapping !== false) {
return isset($mapping['readOnly']) && $mapping['readOnly'] == true;
}

return false;
}

/**
* Gets a column name for a field name.
* If the column name for the field cannot be found, the given field name
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ORM/Mapping/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ final class Column implements Annotation
*/
public $nullable = false;

/**
* @var boolean
*/
public $readOnly = false;

/**
* @var array
*/
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ private function columnToArray($fieldName, Column $column)
'length' => $column->length,
'unique' => $column->unique,
'nullable' => $column->nullable,
'precision' => $column->precision
'precision' => $column->precision,
'readOnly' => $column->readOnly
);

if ($column->options) {
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,10 @@ private function columnToArray(SimpleXMLElement $fieldMapping)
$mapping['nullable'] = $this->evaluateBoolean($fieldMapping['nullable']);
}

if (isset($fieldMapping['readOnly'])) {
$mapping['readOnly'] = $this->evaluateBoolean($fieldMapping['readOnly']);
}

if (isset($fieldMapping['version']) && $fieldMapping['version']) {
$mapping['version'] = $this->evaluateBoolean($fieldMapping['version']);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ private function columnToArray($fieldName, $column)
$mapping['nullable'] = $column['nullable'];
}

if (isset($column['readOnly'])) {
$mapping['readOnly'] = $column['readOnly'];
}

if (isset($column['version']) && $column['version']) {
$mapping['version'] = $column['version'];
}
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ protected function prepareUpdateData($entity)
continue;
}

if ($this->class->hasField($field) && $this->class->isReadOnly($field)) {
continue;
}

$newVal = $change[1];

if ( ! isset($this->class->associationMappings[$field])) {
Expand Down Expand Up @@ -1430,6 +1434,10 @@ protected function getInsertColumnList()
continue;
}

if ($this->class->hasField($name) && $this->class->isReadOnly($name)) {
continue;
}

if (isset($this->class->associationMappings[$name])) {
$assoc = $this->class->associationMappings[$name];

Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)
if (isset($field['nullable'])) {
$fieldXml->addAttribute('nullable', $field['nullable'] ? 'true' : 'false');
}

if (isset($field['readOnly'])) {
$fieldXml->addAttribute('readOnly', $field['readOnly'] ? 'true' : 'false');
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Doctrine/Tests/Models/ReadOnlyColumn/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Doctrine\Tests\Models\ReadOnlyColumn;

/**
* @Entity
* @Table(name="readonly_column")
*/
class Item
{
/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
public $id;

/**
* @Column(type="string")
*/
public $label;

/**
* @Column(type="string", readOnly=false)
*/
public $content;

/**
* @Column(type="string", readOnly=true)
*/
public $generatedString;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Doctrine\Tests\ORM\Persisters;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister;
use Doctrine\Tests\Models\ReadOnlyColumn\Item;

class BasicEntityPersisterReadOnlyColumnTest extends \Doctrine\Tests\OrmTestCase
{
/**
* @var BasicEntityPersister
*/
protected $_persister;

/**
* @var \Doctrine\ORM\EntityManager
*/
protected $_em;

/**
* {@inheritDoc}
*/
protected function setUp()
{
parent::setUp();

$this->_em = $this->_getTestEntityManager();

$this->_persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata('Doctrine\Tests\Models\ReadOnlyColumn\Item'));
}

public function testGetSelectSQLUsesReadOnlyColumn()
{
$method = new \ReflectionMethod($this->_persister, 'getSelectSQL');
$method->setAccessible(true);

$sql = $method->invoke($this->_persister, new Criteria());

$this->assertGreaterThan(0, stripos($sql, '.generatedString'));
}

public function testGetInsertSQLUsesReadOnlyColumn()
{
$method = new \ReflectionMethod($this->_persister, 'getInsertSQL');
$method->setAccessible(true);

$sql = $method->invoke($this->_persister);

$this->assertEquals('INSERT INTO readonly_column (label, content) VALUES (?, ?)', $sql);
}

public function testGetUpdateSQLUsesReadOnlyColumn()
{
$item = new Item();
$this->_em->persist($item);
$this->_em->flush();

$item->label = 'foo';
$item->generatedString = 'bar';

$this->_em->getUnitOfWork()->computeChangeSets();

$method = new \ReflectionMethod($this->_persister, 'update');
$method->setAccessible(true);
$method->invoke($this->_persister, $item);

$updates = $this->_em->getConnection()->getExecuteUpdates();
$lastUpdate = end($updates);

$this->assertNotEmpty($lastUpdate);

$sql = $lastUpdate['query'];
$updateParams = $lastUpdate['params'];

$this->assertContains('label', $sql, '', true);
$this->assertContains('foo', $updateParams, '', true, true, true);

$this->assertNotContains('generatedString', $sql, '', true);
$this->assertNotContains('bar', $updateParams, '', true, true, true);
}
}