Created
November 11, 2018 03:20
-
-
Save markstory/ef5e817c02097a9bba5e1324232c8fd1 to your computer and use it in GitHub Desktop.
CakePHP Issue 12485 repo
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
<?php | |
namespace App\Shell; | |
use Cake\Console\Shell; | |
use Cake\ORM\TableRegistry; | |
class BugShell extends Shell | |
{ | |
public function main() | |
{ | |
// Simulate a request based on what was in the issue. | |
$data = [ | |
'id' => '1', | |
'email' => '[email protected]', | |
'calendars' => [ | |
[ | |
'title' => 'First calendar', | |
'_joinData' => [ | |
'subscribed' => ['year' => 2018, 'month' => 10, 'day' => 10] | |
] | |
] | |
] | |
]; | |
$users = TableRegistry::get('Users'); | |
$user = $users->get(1, ['contain' => ['Calendars']]); | |
$user = $users->patchEntity($user, $data, ['associated' => ['Calendars._joinData']]); | |
debug($user); | |
} | |
} |
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
<?php | |
namespace App\Model\Entity; | |
use Cake\ORM\Entity; | |
class Calendar extends Entity | |
{ | |
protected $_accessible = [ | |
'title' => true, | |
'users' => true | |
]; | |
} |
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
<?php | |
namespace App\Model\Table; | |
use Cake\ORM\Query; | |
use Cake\ORM\RulesChecker; | |
use Cake\ORM\Table; | |
use Cake\Validation\Validator; | |
class CalendarsTable extends Table | |
{ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); | |
$this->setTable('calendars'); | |
$this->setDisplayField('title'); | |
$this->setPrimaryKey('id'); | |
$this->belongsToMany('Users', [ | |
'foreignKey' => 'calendar_id', | |
'targetForeignKey' => 'user_id', | |
'joinTable' => 'calendars_users', | |
'through' => 'CalendarsUsers', | |
]); | |
} | |
public function validationDefault(Validator $validator) | |
{ | |
$validator | |
->integer('id') | |
->allowEmpty('id', 'create'); | |
$validator | |
->scalar('title') | |
->maxLength('title', 255) | |
->allowEmpty('title'); | |
return $validator; | |
} | |
} |
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
<?php | |
namespace App\Model\Entity; | |
use Cake\ORM\Entity; | |
class CalendarsUser extends Entity | |
{ | |
protected $_accessible = [ | |
'calendar_id' => true, | |
'user_id' => true, | |
'subscribed' => true, | |
'calendar' => true, | |
'user' => true | |
]; | |
} |
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
<?php | |
namespace App\Model\Table; | |
use Cake\ORM\Query; | |
use Cake\ORM\RulesChecker; | |
use Cake\ORM\Table; | |
use Cake\Validation\Validator; | |
class CalendarsUsersTable extends Table | |
{ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); | |
$this->setTable('calendars_users'); | |
$this->setDisplayField('id'); | |
$this->setPrimaryKey('id'); | |
$this->belongsTo('Calendars', [ | |
'foreignKey' => 'calendar_id', | |
'joinType' => 'INNER' | |
]); | |
$this->belongsTo('Users', [ | |
'foreignKey' => 'user_id', | |
'joinType' => 'INNER' | |
]); | |
} | |
public function validationDefault(Validator $validator) | |
{ | |
$validator | |
->integer('id') | |
->allowEmpty('id', 'create'); | |
$validator | |
->date('subscribed') | |
->allowEmpty('subscribed'); | |
return $validator; | |
} | |
public function buildRules(RulesChecker $rules) | |
{ | |
$rules->add($rules->existsIn(['calendar_id'], 'Calendars')); | |
$rules->add($rules->existsIn(['user_id'], 'Users')); | |
return $rules; | |
} | |
} |
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
########## DEBUG ########## | |
object(App\Model\Entity\User) { | |
'id' => (int) 1, | |
'email' => '[email protected]', | |
'password' => '$2y$10$0EBEDr/ANH.MKKz4.y6xOemorJOO2jyLFO.LS/mmSxgWhPX4xsu4S', | |
'api_key' => '4a836c41d75317c9cea7b261084efc2f', | |
'dob' => null, | |
'created' => object(Cake\I18n\FrozenTime) { | |
'time' => '2014-10-05T03:27:39+00:00', | |
'timezone' => 'UTC', | |
'fixedNowTime' => false | |
}, | |
'updated' => null, | |
'calendars' => [ | |
(int) 0 => object(App\Model\Entity\Calendar) { | |
'title' => 'First calendar', | |
'_joinData' => object(App\Model\Entity\CalendarsUser) { | |
'subscribed' => object(Cake\I18n\FrozenDate) { | |
'time' => '2018-10-10T00:00:00+00:00', | |
'timezone' => 'UTC', | |
'fixedNowTime' => false | |
}, | |
'[new]' => true, | |
'[accessible]' => [ | |
'calendar_id' => true, | |
'user_id' => true, | |
'subscribed' => true, | |
'calendar' => true, | |
'user' => true | |
], | |
'[dirty]' => [ | |
'subscribed' => true | |
], | |
'[original]' => [], | |
'[virtual]' => [], | |
'[errors]' => [], | |
'[invalid]' => [], | |
'[repository]' => 'CalendarsUsers' | |
}, | |
'[new]' => true, | |
'[accessible]' => [ | |
'title' => true, | |
'users' => true | |
], | |
'[dirty]' => [ | |
'title' => true, | |
'_joinData' => true | |
], | |
'[original]' => [], | |
'[virtual]' => [], | |
'[errors]' => [], | |
'[invalid]' => [], | |
'[repository]' => 'Calendars' | |
} | |
], | |
'[new]' => false, | |
'[accessible]' => [ | |
'email' => true, | |
'password' => true, | |
'bookmarks' => true, | |
'calendars' => true, | |
'profile' => true, | |
'digest_hash' => true | |
], | |
'[dirty]' => [ | |
'email' => true, | |
'calendars' => true | |
], | |
'[original]' => [ | |
'email' => '[email protected]', | |
'calendars' => [] | |
], | |
'[virtual]' => [], | |
'[errors]' => [], | |
'[invalid]' => [], | |
'[repository]' => 'Users' | |
} | |
########################### |
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
CREATE TABLE `calendars` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`title` varchar(255) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
); | |
CREATE TABLE `users` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`email` varchar(255) NOT NULL, | |
`password` varchar(255) NOT NULL, | |
`created` datetime DEFAULT NULL, | |
`updated` datetime DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
); | |
CREATE TABLE `calendars_users` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`calendar_id` int(11) NOT NULL, | |
`user_id` int(11) NOT NULL, | |
`subscribed` date DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
); |
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
<?php | |
namespace App\Model\Table; | |
use Cake\Auth\DigestAuthenticate; | |
use Cake\Auth\DefaultPasswordHasher; | |
use Cake\ORM\Query; | |
use Cake\ORM\Table; | |
use Cake\Validation\Validator; | |
class UsersTable extends Table | |
{ | |
public function initialize(array $config) | |
{ | |
$this->setTable('users'); | |
$this->setDisplayField('id'); | |
$this->setPrimaryKey('id'); | |
$this->addBehavior('Timestamp'); | |
$this->belongsToMany('Calendars', [ | |
'joinTable' => 'calendars_users', | |
'foreignKey' => 'user_id', | |
'targetForeignKey' => 'calendar_id', | |
'through' => 'CalendarsUsers', | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment