Skip to content
Permalink
Browse files

Changin the Table class to work with the new naming convention

  • Loading branch information...
lorenzo committed Nov 8, 2013
1 parent 6d92b13 commit f775c31a9873486f76744ef1827ee0b715e7cf01
@@ -19,6 +19,7 @@
use Cake\ORM\Entity;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
/**
* An Association is a relationship established between two tables and is used
@@ -167,10 +168,6 @@ public function __construct($name, array $options = []) {
$this->_name = $name;
$this->_options($options);
if (empty($this->_property)) {
$this->property($name);
}
if (!empty($options['strategy'])) {
$this->strategy($options['strategy']);
}
@@ -311,6 +308,9 @@ public function property($name = null) {
if ($name !== null) {
$this->_property = $name;
}
if ($name === null && !$this->_property) {
$this->_property = Inflector::underscore($this->_name);
}
return $this->_property;
}
@@ -68,6 +68,24 @@ public function cascadeDelete(Entity $entity, $options = []) {
return true;
}
/**
* Sets the property name that should be filled with data from the target table
* in the source table record.
* If no arguments are passed, currently configured type is returned.
*
* @param string $name
* @return string
*/
public function property($name = null) {
if ($name !== null) {
return parent::property($name);
}
if ($name === null && !$this->_property) {
$this->_property = Inflector::underscore(Inflector::singularize($this->_name));
}
return $this->_property;
}
/**
* Returns a single or multiple conditions to be appended to the generated join
* clause for getting the results on the target table.
@@ -64,6 +64,24 @@ public function foreignKey($key = null) {
return parent::foreignKey($key);
}
/**
* Sets the property name that should be filled with data from the target table
* in the source table record.
* If no arguments are passed, currently configured type is returned.
*
* @param string $name
* @return string
*/
public function property($name = null) {
if ($name !== null) {
return parent::property($name);
}
if ($name === null && !$this->_property) {
$this->_property = Inflector::underscore(Inflector::singularize($this->_name));
}
return $this->_property;
}
/**
* Returns a single or multiple conditions to be appended to the generated join
* clause for getting the results on the target table.
@@ -354,7 +354,7 @@ public function entityClass($name = null) {
return $this->_entityClass = $default;
}
$alias = substr(array_pop($parts), 0, -5);
$alias = Inflector::singularize(substr(array_pop($parts), 0, -5));
$name = implode('\\', array_slice($parts, 0, -1)) . '\Entity\\' . $alias;
if (!class_exists($name)) {
return $this->_entityClass = $default;
@@ -20,11 +20,9 @@
class ArticlesTable extends Table {
public function initialize(array $config) {
$this->belongsTo('author');
$this->belongsToMany('tag', [
'property' => 'tags'
]);
$this->hasMany('articlesTag');
$this->belongsTo('authors');
$this->belongsToMany('tags');
$this->hasMany('articlesTags');
}
}
@@ -20,8 +20,8 @@
class ArticlesTagsTable extends Table {
public function initialize(array $config) {
$this->belongsTo('article');
$this->belongsTo('tag');
$this->belongsTo('articles');
$this->belongsTo('tags');
}
}
@@ -20,7 +20,7 @@
class AuthorsTable extends Table {
public function initialize(array $config) {
$this->hasMany('article');
$this->hasMany('articles');
}
}
@@ -20,9 +20,9 @@
class TagsTable extends Table {
public function initialize(array $config) {
$this->belongsTo('author');
$this->belongsToMany('tag', ['property' => 'tags']);
$this->hasMany('articlesTag', ['property' => 'extraInfo']);
$this->belongsTo('authors');
$this->belongsToMany('tags');
$this->hasMany('articlesTags', ['property' => 'extraInfo']);
}
}
@@ -73,11 +73,11 @@ public function testTableMethod() {
$table = $this->getMockBuilder('\Cake\ORM\Table')
->setMethods(['find'])
->setMockClassName('SpecialThingTable')
->setMockClassName('SpecialThingsTable')
->getMock();
$this->assertEquals('special_things', $table->table());
$table = new Table(['alias' => 'LoveBoat']);
$table = new Table(['alias' => 'LoveBoats']);
$this->assertEquals('love_boats', $table->table());
$table->table('other');
@@ -842,7 +842,7 @@ public function testRepositoryClassNonExisting() {
* @return void
*/
public function testRepositoryClassConventionForAPP() {
$table = new \TestApp\Model\Repository\ArticleTable;
$table = new \TestApp\Model\Repository\ArticlesTable;
$this->assertEquals('TestApp\Model\Entity\Article', $table->entityClass());
}
@@ -865,10 +865,10 @@ public function testSetEntityClass() {
* @return void
*/
public function testReciprocalBelongsToLoading() {
$table = new \TestApp\Model\Repository\ArticleTable([
$table = new \TestApp\Model\Repository\ArticlesTable([
'connection' => $this->connection,
]);
$result = $table->find('all')->contain(['author'])->first();
$result = $table->find('all')->contain(['authors'])->first();
$this->assertInstanceOf('TestApp\Model\Entity\Author', $result->author);
}
@@ -879,12 +879,12 @@ public function testReciprocalBelongsToLoading() {
* @return void
*/
public function testReciprocalHasManyLoading() {
$table = new \TestApp\Model\Repository\ArticleTable([
$table = new \TestApp\Model\Repository\ArticlesTable([
'connection' => $this->connection,
]);
$result = $table->find('all')->contain(['author' => ['article']])->first();
$this->assertCount(2, $result->author->article);
foreach ($result->author->article as $article) {
$result = $table->find('all')->contain(['authors' => ['articles']])->first();
$this->assertCount(2, $result->author->articles);
foreach ($result->author->articles as $article) {
$this->assertInstanceOf('TestApp\Model\Entity\Article', $article);
}
}
@@ -896,10 +896,10 @@ public function testReciprocalHasManyLoading() {
* @return void
*/
public function testReciprocalBelongsToMany() {
$table = new \TestApp\Model\Repository\ArticleTable([
$table = new \TestApp\Model\Repository\ArticlesTable([
'connection' => $this->connection,
]);
$result = $table->find('all')->contain(['tag'])->first();
$result = $table->find('all')->contain(['tags'])->first();
$this->assertInstanceOf('TestApp\Model\Entity\Tag', $result->tags[0]);
$this->assertInstanceOf(
'TestApp\Model\Entity\ArticlesTag',
@@ -913,10 +913,10 @@ public function testReciprocalBelongsToMany() {
* @return void
*/
public function testFindCleanEntities() {
$table = new \TestApp\Model\Repository\ArticleTable([
$table = new \TestApp\Model\Repository\ArticlesTable([
'connection' => $this->connection,
]);
$results = $table->find('all')->contain(['tag', 'author'])->toArray();
$results = $table->find('all')->contain(['tags', 'authors'])->toArray();
$this->assertCount(3, $results);
foreach ($results as $article) {
$this->assertFalse($article->dirty('id'));
@@ -940,10 +940,10 @@ public function testFindCleanEntities() {
* @return void
*/
public function testFindPersistedEntities() {
$table = new \TestApp\Model\Repository\ArticleTable([
$table = new \TestApp\Model\Repository\ArticlesTable([
'connection' => $this->connection,
]);
$results = $table->find('all')->contain(['tag', 'author'])->toArray();
$results = $table->find('all')->contain(['tags', 'authors'])->toArray();
$this->assertCount(3, $results);
foreach ($results as $article) {
$this->assertFalse($article->isNew());
@@ -1011,7 +1011,7 @@ public function testCallBehaviorMethod() {
* @return void
*/
public function testCallBehaviorFinder() {
$table = TableRegistry::get('article');
$table = TableRegistry::get('articles');
$table->addBehavior('Sluggable');
$query = $table->noSlug();
@@ -1613,8 +1613,8 @@ public function testDelete() {
* @return void
*/
public function testDeleteDependent() {
$table = TableRegistry::get('author');
$table->hasOne('article', [
$table = TableRegistry::get('authors');
$table->hasOne('articles', [
'foreignKey' => 'author_id',
'dependent' => true,
]);
@@ -1623,7 +1623,7 @@ public function testDeleteDependent() {
$entity = $query->first();
$result = $table->delete($entity);
$articles = $table->association('article')->target();
$articles = $table->association('articles')->target();
$query = $articles->find('all', [
'conditions' => [
'author_id' => $entity->id
@@ -1638,7 +1638,7 @@ public function testDeleteDependent() {
* @return void
*/
public function testDeleteNoDependentNoCascade() {
$table = TableRegistry::get('author');
$table = TableRegistry::get('authors');
$table->hasMany('article', [
'foreignKey' => 'author_id',
'dependent' => false,
@@ -1648,7 +1648,7 @@ public function testDeleteNoDependentNoCascade() {
$entity = $query->first();
$result = $table->delete($entity);
$articles = $table->association('article')->target();
$articles = $table->association('articles')->target();
$query = $articles->find('all')->where(['author_id' => $entity->id]);
$this->assertCount(2, $query->execute(), 'Should find rows.');
}
@@ -1659,7 +1659,7 @@ public function testDeleteNoDependentNoCascade() {
* @return void
*/
public function testDeleteBelongsToMany() {
$table = TableRegistry::get('article');
$table = TableRegistry::get('articles');
$table->belongsToMany('tag', [
'foreignKey' => 'article_id',
'joinTable' => 'articles_tags'
@@ -1668,7 +1668,7 @@ public function testDeleteBelongsToMany() {
$entity = $query->first();
$table->delete($entity);
$pivot = $table->association('tag')->pivot();
$pivot = $table->association('tags')->pivot();
$query = $pivot->find('all')->where(['article_id' => 1]);
$this->assertNull($query->execute()->one(), 'Should not find any rows.');
}

0 comments on commit f775c31

Please sign in to comment.
You can’t perform that action at this time.