Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #842 from CakeDC/issue/839-fix-belongs-to
Avoid generating association if table object or database table exist
- Loading branch information
Showing
3 changed files
with
134 additions
and
61 deletions.
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', | ||
]; | ||
} |