Skip to content
This repository has been archived by the owner on Mar 4, 2019. It is now read-only.

Commit

Permalink
Merge a462c62 into 9da4564
Browse files Browse the repository at this point in the history
  • Loading branch information
eberfreitas committed Oct 28, 2014
2 parents 9da4564 + a462c62 commit beac028
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
47 changes: 30 additions & 17 deletions ORM/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ class Table extends AppModel {

protected $_savedEntityStates = [];

/**
* Class constructor
*
* @param bool|int|string|array $id Set this ID for this model on startup.
* @param string $table Name of database table to use.
* @param string $ds DataSource connection name.
*/
public function __construct($id = false, $table = null, $ds = null) {
if (is_array($id)) {
$alias = Hash::get($id, 'alias') ?: (Hash::get($id, 'table') ?: $this->alias);
$id['alias'] = Inflector::singularize(preg_replace('/Table$/', '', $alias));
$this->name = $this->alias = $this->alias($id['alias']);

$schema = Hash::get($id, 'schema');

if ($schema !== null) {
$this->_schema = $schema;
}
Expand All @@ -33,6 +40,7 @@ public function __construct($id = false, $table = null, $ds = null) {
}
$table = Inflector::tableize(preg_replace('/Table$/', '', $this->alias));
}

parent::__construct($id, $table, $ds);
$this->entityClass(Inflector::singularize($this->name) . 'Entity');
$this->initialize([]);
Expand Down Expand Up @@ -98,7 +106,7 @@ public function table($table = null) {
/**
* Returns the table alias or sets a new one
*
* @param string $table the new table alias
* @param string $alias the new table alias
* @return string
*/
public function alias($alias = null) {
Expand Down Expand Up @@ -396,7 +404,7 @@ public function belongsToMany($associated, array $options = []) {

/**
* Table::_bindModel()
*
*
* @param mixed $type
* @param mixed $associated
* @param mixed $options
Expand Down Expand Up @@ -486,7 +494,7 @@ public function exists($conditions = null) {

/**
* Table::save()
*
*
* @param mixed $entity
* @param bool $validate
* @param mixed $fieldList
Expand Down Expand Up @@ -516,14 +524,19 @@ public function save($entity = null, $validate = true, $fieldList = []) {
return false;
}

$isNew = $entity->isNew();
$success = parent::save($entity, $validate, $fieldList);
$insertedId = $this->getInsertID();

if (!$success) {
return false;
}

$entity->isNew(false);
$entity->set($this->primaryKey(), $this->getInsertID());

if (!empty($insertedId)) {
$entity->set($this->primaryKey(), $this->getInsertID());
}

return $entity;
}

Expand Down Expand Up @@ -665,7 +678,7 @@ public function convertToEntity($data) {

/**
* Table::convertToEntities()
*
*
* @param mixed $list
* @return
*/
Expand All @@ -683,7 +696,7 @@ public function convertToEntities($list) {

/**
* Table::beforeFind()
*
*
* @param mixed $queryData
* @return
*/
Expand All @@ -699,7 +712,7 @@ public function beforeFind($queryData) {

/**
* Table::afterFind()
*
*
* @param mixed $results
* @param bool $primary
* @return
Expand All @@ -717,7 +730,7 @@ public function afterFind($results, $primary = false) {

/**
* Table::_saveEntityState()
*
*
* @return void
*/
protected function _saveEntityState() {
Expand All @@ -726,7 +739,7 @@ protected function _saveEntityState() {

/**
* Table::_restoreEntityState()
*
*
* @return void
*/
protected function _restoreEntityState() {
Expand All @@ -735,7 +748,7 @@ protected function _restoreEntityState() {

/**
* Table::_entityClassForData()
*
*
* @param mixed $data
* @return
*/
Expand All @@ -745,7 +758,7 @@ protected function _entityClassForData($data) {

/**
* Table::allEntities()
*
*
* @param mixed $params
* @return
*/
Expand All @@ -756,7 +769,7 @@ public function allEntities($params = []) {

/**
* Table::entities()
*
*
* @param mixed $params
* @return
*/
Expand All @@ -766,7 +779,7 @@ public function entities($params = []) {

/**
* Table::__call()
*
*
* @param mixed $method
* @param mixed $params
* @return
Expand All @@ -785,7 +798,7 @@ public function __call($method, $params) {

/**
* Table::_analyzeMethodName()
*
*
* @param mixed $method
* @return
*/
Expand Down Expand Up @@ -818,7 +831,7 @@ public function set($one, $two = null) {

/**
* Table::_EntityClassLocation()
*
*
* @return
*/
protected function _entityClassLocation() {
Expand Down
12 changes: 6 additions & 6 deletions Test/Case/ORM/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testSetOneParamWithSetter() {
$entity = $this->getMock('Entity', ['setName']);
$entity->expects($this->once())->method('setName')
->with('Jones')
->will($this->returnCallback(function($name) {
->will($this->returnCallback(function ($name) {
$this->assertEquals('Jones', $name);
return 'Dr. ' . $name;
}));
Expand All @@ -73,13 +73,13 @@ public function testMultipleWithSetter() {
$entity->accessible('*', true);
$entity->expects($this->once())->method('setName')
->with('Jones')
->will($this->returnCallback(function($name) {
->will($this->returnCallback( function($name) {
$this->assertEquals('Jones', $name);
return 'Dr. ' . $name;
}));
$entity->expects($this->once())->method('setStuff')
->with(['a', 'b'])
->will($this->returnCallback(function($stuff) {
->will($this->returnCallback(function ($stuff) {
$this->assertEquals(['a', 'b'], $stuff);
return ['c', 'd'];
}));
Expand Down Expand Up @@ -169,7 +169,7 @@ public function testGetCustomGetters() {
$entity = $this->getMock('Entity', ['getName']);
$entity->expects($this->once())->method('getName')
->with('Jones')
->will($this->returnCallback(function($name) {
->will($this->returnCallback(function ($name) {
$this->assertSame('Jones', $name);
return 'Dr. ' . $name;
}));
Expand Down Expand Up @@ -199,7 +199,7 @@ public function testMagicSetWithSetter() {
$entity = $this->getMock('Entity', ['setName']);
$entity->expects($this->once())->method('setName')
->with('Jones')
->will($this->returnCallback(function($name) {
->will($this->returnCallback(function ($name) {
$this->assertEquals('Jones', $name);
return 'Dr. ' . $name;
}));
Expand All @@ -216,7 +216,7 @@ public function testMagicGetWithGetter() {
$entity = $this->getMock('Entity', ['getName']);
$entity->expects($this->once())->method('getName')
->with('Jones')
->will($this->returnCallback(function($name) {
->will($this->returnCallback(function ($name) {
$this->assertSame('Jones', $name);
return 'Dr. ' . $name;
}));
Expand Down
3 changes: 2 additions & 1 deletion Test/Case/ORM/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class TableTest extends CakeTestCase {
* Handy variable containing the next primary key that will be inserted in the
* users table
*
* @var integer
* @var int
*/
public static $nextUserId = 5;

Expand Down Expand Up @@ -403,6 +403,7 @@ public function testDisplaySet() {
*/
public function testFindPersistedEntities() {
$table = TableRegistry::get('users');
$table->entity = true;
$results = $table->find('all');
$this->assertCount(4, $results);
foreach ($results as $article) {
Expand Down

0 comments on commit beac028

Please sign in to comment.