本文整理汇总了PHP中Cake\Controller\Controller::loadModel方法的典型用法代码示例。如果您正苦于以下问题:PHP Controller::loadModel方法的具体用法?PHP Controller::loadModel怎么用?PHP Controller::loadModel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Controller\Controller
的用法示例。
在下文中一共展示了Controller::loadModel方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setLanguage
/**
* Set the language for the user.
*
* @return void
*/
public function setLanguage()
{
if ($this->_controller->Auth->user()) {
//The user has already a valid language defined in the database.
if ($this->_session->read('Auth.User.language') && isset($this->_locales[$this->_session->read('Auth.User.language')])) {
//If the user has not the cookie, we set the cookie.
if (!$this->_cookie->check('language') || $this->_cookie->read('language') != $this->_session->read('Auth.User.language')) {
$this->_cookie->write('language', $this->_session->read('Auth.User.language'));
}
//Stock the locale of the user.
$this->_locale = $this->_session->read('Auth.User.language');
}
} else {
//The user has a valid cookie.
if ($this->_cookie->check('language') && isset($this->_locales[$this->_cookie->read('language')])) {
$this->_locale = $this->_cookie->read('language');
}
}
//The user want to change his language.
if (isset($this->_controller->request->params['lang']) && isset($this->_locales[$this->_controller->request->params['lang']])) {
//If the user is connected, we need to save the new language in the database and refresh his session.
if ($this->_controller->Auth->user()) {
$this->_controller->loadModel('Users');
$user = $this->_controller->Users->find()->where(['id' => $this->_session->read('Auth.User.id')])->first();
$user->language = $this->_controller->request->params['lang'];
$this->_controller->Users->save($user);
$this->_session->write('Auth.User.language', $this->_controller->request->params['lang']);
}
//Save the new language in the cookie.
$this->_cookie->write('language', $this->_controller->request->params['lang']);
$this->_locale = $this->_controller->request->params['lang'];
}
//Set the locale.
I18n::locale($this->_locale);
}
示例2: testPaginate
/**
* @return void
*/
public function testPaginate()
{
Configure::write('Paginator.limit', 2);
$ToolsUser = TableRegistry::get('ToolsUsers');
$count = $ToolsUser->find('count');
$this->assertTrue($count > 3);
$this->Controller->loadModel('ToolsUsers');
$result = $this->Controller->paginate('ToolsUsers');
$this->assertSame(2, count($result->toArray()));
}
示例3: _isCurrencyValid
/**
* Check if the currency match with the offer currency.
*
* @param array $custom The custom data passed to Paypal.
* @param string $mcCurrency The currency to check.
*
* @return bool
*/
protected function _isCurrencyValid(array $custom, $mcCurrency)
{
$this->_controller->loadModel('PremiumOffers');
$offer = $this->_controller->PremiumOffers->find('offerByIdAndPeriod', ['id' => $custom['offer_id'], 'period' => $custom['period']]);
if ($offer->currency_code != $mcCurrency) {
Log::error(__('The currency offer {0} does not match with the Paypal currency {1}.', $offer->currency, $mcCurrency), 'paypal');
return false;
}
return true;
}
示例4: testLoadModel
/**
* testLoadModel method
*
* @return void
*/
public function testLoadModel()
{
$request = new Request('controller_posts/index');
$response = $this->getMockBuilder('Cake\\Network\\Response')->getMock();
$Controller = new Controller($request, $response);
$this->assertFalse(isset($Controller->Articles));
$result = $Controller->loadModel('Articles');
$this->assertInstanceOf('TestApp\\Model\\Table\\ArticlesTable', $result);
$this->assertInstanceOf('TestApp\\Model\\Table\\ArticlesTable', $Controller->Articles);
}
示例5: useModel
/**
* Sets the model class to be used during the action execution.
*
* @param string $modelName The name of the model to load.
* @return void
*/
public function useModel($modelName)
{
$this->_controller->loadModel($modelName);
$this->_modelName = $this->_model->name;
}
示例6: useModel
/**
* Sets the model class to be used during the action execution.
*
* @param string $modelName The name of the model to load.
* @return void
*/
public function useModel($modelName)
{
$this->_controller->loadModel($modelName);
list(, $this->_modelName) = pluginSplit($modelName);
}
示例7: _createValidator
/**
* Creates a validation object on the fly.
*
* @return \Cake\Validation\Validator
*/
protected function _createValidator()
{
$config = $this->config();
if ($config['validator'] instanceof Validator) {
return $config['validator'];
}
$this->_controller->loadModel('Comment.Comments');
if ($this->_controller->request->is('userLoggedIn')) {
// logged user posting
$validator = $this->_controller->Comments->validationDefault(new Validator());
$validator->requirePresence('user_id')->notEmpty('user_id', __d('comment', 'Invalid user.'))->add('user_id', 'checkUserId', ['rule' => function ($value, $context) {
if (!empty($value)) {
$valid = TableRegistry::get('User.Users')->find()->where(['Users.id' => $value, 'Users.status' => 1])->count() === 1;
return $valid;
}
return false;
}, 'message' => __d('comment', 'Invalid user, please try again.'), 'provider' => 'table']);
} elseif ($this->config('settings.allow_anonymous')) {
// anonymous user posting
$validator = $this->_controller->Comments->validator('anonymous');
} else {
// other case
$validator = new Validator();
}
if ($this->config('settings.use_captcha')) {
$validator->add('body', 'humanCheck', ['rule' => function ($value, $context) {
return CaptchaManager::adapter()->validate($this->_controller->request);
}, 'message' => __d('comment', 'We were not able to verify you as human. Please try again.'), 'provider' => 'table']);
}
return $validator;
}