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
Avoid generating association if table object or database table exist #842
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,48 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace BakeTest\Model\Table; | ||
namespace BakeTest\Model\Entity; | ||
|
||
use Cake\ORM\Query; | ||
use Cake\ORM\RulesChecker; | ||
use Cake\ORM\Table; | ||
use Cake\Validation\Validator; | ||
use Cake\ORM\Entity; | ||
|
||
/** | ||
* Users Model | ||
* User Entity | ||
* | ||
* @property \BakeTest\Model\Table\CommentsTable&\Cake\ORM\Association\HasMany $Comments | ||
* @property \BakeTest\Model\Table\TodoItemsTable&\Cake\ORM\Association\HasMany $TodoItems | ||
* @property int $id | ||
* @property string|null $username | ||
* @property string|null $password | ||
* @property \Cake\I18n\FrozenTime|null $created | ||
* @property \Cake\I18n\FrozenTime|null $updated | ||
* | ||
* @method \BakeTest\Model\Entity\User newEmptyEntity() | ||
* @method \BakeTest\Model\Entity\User newEntity(array $data, array $options = []) | ||
* @method \BakeTest\Model\Entity\User[] newEntities(array $data, array $options = []) | ||
* @method \BakeTest\Model\Entity\User get($primaryKey, $options = []) | ||
* @method \BakeTest\Model\Entity\User findOrCreate($search, ?callable $callback = null, $options = []) | ||
* @method \BakeTest\Model\Entity\User patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) | ||
* @method \BakeTest\Model\Entity\User[] patchEntities(iterable $entities, array $data, array $options = []) | ||
* @method \BakeTest\Model\Entity\User|false save(\Cake\Datasource\EntityInterface $entity, $options = []) | ||
* @method \BakeTest\Model\Entity\User saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = []) | ||
* @method \BakeTest\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable $entities, $options = []) | ||
* @method \BakeTest\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = []) | ||
* @method \BakeTest\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = []) | ||
* @method \BakeTest\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = []) | ||
* | ||
* @mixin \Cake\ORM\Behavior\TimestampBehavior | ||
* @property \BakeTest\Model\Entity\Comment[] $comments | ||
* @property \BakeTest\Model\Entity\TodoItem[] $todo_items | ||
*/ | ||
class UsersTable extends Table | ||
class User extends Entity | ||
{ | ||
/** | ||
* Initialize method | ||
* Fields that can be mass assigned using newEntity() or patchEntity(). | ||
* | ||
* Note that when '*' is set to true, this allows all unspecified fields to | ||
* be mass assigned. For security purposes, it is advised to set '*' to false | ||
* (or remove it), and explicitly make individual fields accessible as needed. | ||
* | ||
* @param array $config The configuration for the Table. | ||
* @return void | ||
* @var array<string, bool> | ||
*/ | ||
public function initialize(array $config): void | ||
{ | ||
parent::initialize($config); | ||
|
||
$this->setTable('users'); | ||
$this->setDisplayField('id'); | ||
$this->setPrimaryKey('id'); | ||
|
||
$this->addBehavior('Timestamp'); | ||
|
||
$this->hasMany('Comments', [ | ||
'foreignKey' => 'user_id', | ||
'className' => 'BakeTest.Comments', | ||
]); | ||
$this->hasMany('TodoItems', [ | ||
'foreignKey' => 'user_id', | ||
'className' => 'BakeTest.TodoItems', | ||
]); | ||
} | ||
protected $_accessible = [ | ||
'username' => true, | ||
'password' => true, | ||
'created' => true, | ||
'updated' => true, | ||
'comments' => true, | ||
'todo_items' => true, | ||
]; | ||
|
||
/** | ||
* Returns a rules checker object that will be used for validating | ||
* application integrity. | ||
* Fields that are excluded from JSON versions of the entity. | ||
* | ||
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified. | ||
* @return \Cake\ORM\RulesChecker | ||
* @var array<string> | ||
*/ | ||
public function buildRules(RulesChecker $rules): RulesChecker | ||
{ | ||
$rules->add($rules->isUnique(['username']), ['errorField' => 'username']); | ||
|
||
return $rules; | ||
} | ||
protected $_hidden = [ | ||
'password', | ||
]; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to use
$this->_tables
here as it was used here before.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
listAll() returns the value of $_tables if it's already populated else fetches the list from the db. I think using the method is fine here. That way it doesn't need to rely on listAll() being called before this method is invoked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct. I initially used $this->_tables but found unit tests failed when findBelongsTo was called isolated. generateAssociations calls listAll as well as findHasMany and findBelongsToMany methods but findBelongsTo didn't.